Skip to content

Commit

Permalink
[TypeDeclaration] Skip controller render method on @var to type decla…
Browse files Browse the repository at this point in the history
…ratoin (#1576)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Dec 27, 2021
1 parent 889a682 commit 68dde1a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector\Fixture;

use Nette\Application\UI\Presenter;

final class SkipNetteRenderMethod extends Presenter
{
/**
* @param int $id
*/
public function render($id)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclaration\NodeAnalyzer;

use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Symplify\Astral\Naming\SimpleNameResolver;

final class ControllerRenderMethodAnalyzer
{
public function __construct(
private readonly SimpleNameResolver $simpleNameResolver,
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer
) {
}

public function isRenderMethod(ClassMethod $classMethod, Scope $scope): bool
{
// nette one?
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

if ($this->isNetteRenderMethod($classReflection, $classMethod)) {
return true;
}

return $this->isSymfonyRenderMethod($classReflection, $classMethod);
}

private function isNetteRenderMethod(ClassReflection $classReflection, ClassMethod $classMethod): bool
{
if (! $classReflection->isSubclassOf('Nette\Application\UI\Control')) {
return false;
}

if (! $classMethod->isPublic()) {
return false;
}

return $this->simpleNameResolver->isNames($classMethod->name, ['render*', 'handle*', 'action*']);
}

private function isSymfonyRenderMethod(ClassReflection $classReflection, ClassMethod $classMethod): bool
{
if (! $classReflection->isSubclassOf(
'Symfony\Bundle\FrameworkBundle\Controller\Controller'
) && ! $classReflection->isSubclassOf('Symfony\Bundle\FrameworkBundle\Controller\AbstractController')) {
return false;
}

if (! $classMethod->isPublic()) {
return false;
}

if ($this->simpleNameResolver->isNames($classMethod->name, ['__invoke', '*render'])) {
return true;
}

return $this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, 'Symfony\Component\Routing\Annotation\Route');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Interface_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\UnionType;
Expand All @@ -19,6 +20,7 @@
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\StaticTypeMapper\ValueObject\Type\NonExistingObjectType;
use Rector\TypeDeclaration\NodeAnalyzer\ControllerRenderMethodAnalyzer;
use Rector\TypeDeclaration\NodeTypeAnalyzer\TraitTypeAnalyzer;
use Rector\TypeDeclaration\TypeInferer\ParamTypeInferer;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
Expand Down Expand Up @@ -46,7 +48,8 @@ public function __construct(
private readonly ParamTypeInferer $paramTypeInferer,
private readonly TraitTypeAnalyzer $traitTypeAnalyzer,
private readonly ParamTagRemover $paramTagRemover,
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard,
private readonly ControllerRenderMethodAnalyzer $controllerRenderMethodAnalyzer,
) {
}

Expand Down Expand Up @@ -135,6 +138,14 @@ public function refactor(Node $node): ?Node
return null;
}

$scope = $node->getAttribute(AttributeKey::SCOPE);
if ($node instanceof ClassMethod && $scope instanceof Scope && $this->controllerRenderMethodAnalyzer->isRenderMethod(
$node,
$scope
)) {
return null;
}

foreach ($node->params as $position => $param) {
$this->refactorParam($param, $position, $node);
}
Expand Down
2 changes: 1 addition & 1 deletion stubs/Nette/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
return;
}

class Presenter
class Presenter extends Control
{
}

0 comments on commit 68dde1a

Please sign in to comment.