-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaration] Skip controller render method on @var to type decla…
…ratoin (#1576) Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
889a682
commit 68dde1a
Showing
4 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
...n/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/skip_nette_render_method.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
rules/TypeDeclaration/NodeAnalyzer/ControllerRenderMethodAnalyzer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,6 @@ | |
return; | ||
} | ||
|
||
class Presenter | ||
class Presenter extends Control | ||
{ | ||
} |