-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SymfonyCmfRoutingInClassMethodSignatureRule
- Loading branch information
Showing
4 changed files
with
177 additions
and
2 deletions.
There are no files selected for viewing
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
121 changes: 121 additions & 0 deletions
121
src/Rules/Deprecations/SymfonyCmfRoutingInClassMethodSignatureRule.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,121 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Rules\Deprecations; | ||
|
||
use mglaman\PHPStanDrupal\Internal\DeprecatedScopeCheck; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassMethodNode; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\ObjectType; | ||
|
||
final class SymfonyCmfRoutingInClassMethodSignatureRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassMethodNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
assert($node instanceof InClassMethodNode); | ||
if (DeprecatedScopeCheck::inDeprecatedScope($scope)) { | ||
return []; | ||
} | ||
[$major, $minor] = explode('.', \Drupal::VERSION, 3); | ||
if ($major !== '9' && (int) $minor > 1) { | ||
return []; | ||
} | ||
$method = $scope->getFunction(); | ||
if (!$method instanceof MethodReflection) { | ||
throw new \PHPStan\ShouldNotHappenException(); | ||
} | ||
|
||
$cmfRouteObjectInterfaceType = new ObjectType(\Symfony\Cmf\Component\Routing\RouteObjectInterface::class); | ||
$cmfRouteProviderInterfaceType = new ObjectType(\Symfony\Cmf\Component\Routing\RouteProviderInterface::class); | ||
$cmfLazyRouteCollectionType = new ObjectType(\Symfony\Cmf\Component\Routing\LazyRouteCollection::class); | ||
|
||
$methodSignature = ParametersAcceptorSelector::selectSingle($method->getVariants()); | ||
|
||
$errors = []; | ||
$errorMessage = 'Parameter $%s of method %s() uses deprecated %s and removed in Drupal 10. Use %s instead.'; | ||
foreach ($methodSignature->getParameters() as $parameter) { | ||
foreach ($parameter->getType()->getReferencedClasses() as $referencedClass) { | ||
$referencedClassType = new ObjectType($referencedClass); | ||
if ($referencedClassType->isSuperTypeOf($cmfRouteObjectInterfaceType)->yes()) { | ||
$errors[] = RuleErrorBuilder::message( | ||
sprintf( | ||
$errorMessage, | ||
$parameter->getName(), | ||
$method->getName(), | ||
$referencedClass, | ||
'\Drupal\Core\Routing\RouteObjectInterface' | ||
) | ||
)->tip('Change record: https://www.drupal.org/node/3151009')->build(); | ||
} elseif ($referencedClassType->isSuperTypeOf($cmfRouteProviderInterfaceType)->yes()) { | ||
$errors[] = RuleErrorBuilder::message( | ||
sprintf( | ||
$errorMessage, | ||
$parameter->getName(), | ||
$method->getName(), | ||
$referencedClass, | ||
'\Drupal\Core\Routing\RouteProviderInterface' | ||
) | ||
)->tip('Change record: https://www.drupal.org/node/3151009')->build(); | ||
} elseif ($referencedClassType->isSuperTypeOf($cmfLazyRouteCollectionType)->yes()) { | ||
$errors[] = RuleErrorBuilder::message( | ||
sprintf( | ||
$errorMessage, | ||
$parameter->getName(), | ||
$method->getName(), | ||
$referencedClass, | ||
'\Drupal\Core\Routing\LazyRouteCollection' | ||
) | ||
)->tip('Change record: https://www.drupal.org/node/3151009')->build(); | ||
} | ||
} | ||
} | ||
|
||
$errorMessage = 'Return type of method %s::%s() has typehint with deprecated %s and is removed in Drupal 10. Use %s instead.'; | ||
$returnClasses = $methodSignature->getReturnType()->getReferencedClasses(); | ||
foreach ($returnClasses as $returnClass) { | ||
$returnType = new ObjectType($returnClass); | ||
if ($returnType->isSuperTypeOf($cmfRouteObjectInterfaceType)->yes()) { | ||
$errors[] = RuleErrorBuilder::message( | ||
sprintf( | ||
$errorMessage, | ||
$method->getDeclaringClass()->getName(), | ||
$method->getName(), | ||
$returnClass, | ||
'\Drupal\Core\Routing\RouteObjectInterface' | ||
) | ||
)->tip('Change record: https://www.drupal.org/node/3151009')->build(); | ||
} elseif ($returnType->isSuperTypeOf($cmfRouteProviderInterfaceType)->yes()) { | ||
$errors[] = RuleErrorBuilder::message( | ||
sprintf( | ||
$errorMessage, | ||
$method->getDeclaringClass()->getName(), | ||
$method->getName(), | ||
$returnClass, | ||
'\Drupal\Core\Routing\RouteProviderInterface' | ||
) | ||
)->tip('Change record: https://www.drupal.org/node/3151009')->build(); | ||
} elseif ($returnType->isSuperTypeOf($cmfLazyRouteCollectionType)->yes()) { | ||
$errors[] = RuleErrorBuilder::message( | ||
sprintf( | ||
$errorMessage, | ||
$method->getDeclaringClass()->getName(), | ||
$method->getName(), | ||
$returnClass, | ||
'\Drupal\Core\Routing\LazyRouteCollection' | ||
) | ||
)->tip('Change record: https://www.drupal.org/node/3151009')->build(); | ||
} | ||
} | ||
return $errors; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
tests/src/Rules/SymfonyCmfRoutingInClassMethodSignatureRuleTest.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,53 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Tests\Rules; | ||
|
||
use mglaman\PHPStanDrupal\Rules\Deprecations\SymfonyCmfRoutingInClassMethodSignatureRule; | ||
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase; | ||
|
||
final class SymfonyCmfRoutingInClassMethodSignatureRuleTest extends DrupalRuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new SymfonyCmfRoutingInClassMethodSignatureRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
[$version] = explode('.', \Drupal::VERSION, 2); | ||
if ($version === '8') { | ||
$this->analyse([__DIR__.'/data/symfony-cmf-routing.php'], []); | ||
} elseif ($version === '10') { | ||
self::markTestSkipped('Not tested on 10.x.x'); | ||
} else { | ||
$this->analyse( | ||
[__DIR__.'/data/symfony-cmf-routing.php'], | ||
[ | ||
[ | ||
'Parameter $object of method a() uses deprecated Symfony\Cmf\Component\Routing\RouteObjectInterface and removed in Drupal 10. Use \Drupal\Core\Routing\RouteObjectInterface instead.', | ||
10, | ||
'Change record: https://www.drupal.org/node/3151009' | ||
], | ||
[ | ||
'Parameter $provider of method b() uses deprecated Symfony\Cmf\Component\Routing\RouteProviderInterface and removed in Drupal 10. Use \Drupal\Core\Routing\RouteProviderInterface instead.', | ||
13, | ||
'Change record: https://www.drupal.org/node/3151009' | ||
], | ||
[ | ||
'Return type of method SymfonyCmfRoutingUsage\Foo::b() has typehint with deprecated Symfony\Cmf\Component\Routing\RouteObjectInterface and is removed in Drupal 10. Use \Drupal\Core\Routing\RouteObjectInterface instead.', | ||
13, | ||
'Change record: https://www.drupal.org/node/3151009' | ||
], | ||
[ | ||
'Parameter $collection of method c() uses deprecated Symfony\Cmf\Component\Routing\LazyRouteCollection and removed in Drupal 10. Use \Drupal\Core\Routing\LazyRouteCollection instead.', | ||
16, | ||
'Change record: https://www.drupal.org/node/3151009' | ||
], | ||
] | ||
); | ||
} | ||
} | ||
|
||
} |
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