-
-
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.
Merge pull request #445 from mglaman/gh440
Add SymfonyCmfRouteObjectInterfaceConstantsRule
- Loading branch information
Showing
8 changed files
with
349 additions
and
11 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
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,22 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Internal; | ||
|
||
use PHPStan\Analyser\Scope; | ||
|
||
final class DeprecatedScopeCheck | ||
{ | ||
public static function inDeprecatedScope(Scope $scope): bool | ||
{ | ||
$class = $scope->getClassReflection(); | ||
if ($class !== null && $class->isDeprecated()) { | ||
return true; | ||
} | ||
$trait = $scope->getTraitReflection(); | ||
if ($trait !== null && $trait->isDeprecated()) { | ||
return true; | ||
} | ||
$function = $scope->getFunction(); | ||
return $function !== null && $function->isDeprecated()->yes(); | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
src/Rules/Deprecations/SymfonyCmfRouteObjectInterfaceConstantsRule.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,63 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Rules\Deprecations; | ||
|
||
use Drupal\Core\Routing\RouteObjectInterface; | ||
use mglaman\PHPStanDrupal\Internal\DeprecatedScopeCheck; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\ObjectType; | ||
|
||
final class SymfonyCmfRouteObjectInterfaceConstantsRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\ClassConstFetch::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
assert($node instanceof Node\Expr\ClassConstFetch); | ||
if (!$node->name instanceof Node\Identifier) { | ||
return []; | ||
} | ||
if (!$node->class instanceof Node\Name) { | ||
return []; | ||
} | ||
$constantName = $node->name->name; | ||
$className = $node->class; | ||
$classType = $scope->resolveTypeByName($className); | ||
if (!$classType->hasConstant($constantName)->yes()) { | ||
return []; | ||
} | ||
if (DeprecatedScopeCheck::inDeprecatedScope($scope)) { | ||
return []; | ||
} | ||
[$major, $minor] = explode('.', \Drupal::VERSION, 3); | ||
if ($major !== '9' && (int) $minor > 1) { | ||
return []; | ||
} | ||
$cmfRouteObjectInterfaceType = new ObjectType(\Symfony\Cmf\Component\Routing\RouteObjectInterface::class); | ||
if (!$classType->isSuperTypeOf($cmfRouteObjectInterfaceType)->yes()) { | ||
return []; | ||
} | ||
|
||
$coreRouteObjectInterfaceType = new ObjectType(RouteObjectInterface::class); | ||
if (!$coreRouteObjectInterfaceType->hasConstant($constantName)->yes()) { | ||
return [ | ||
RuleErrorBuilder::message( | ||
sprintf('The core dependency symfony-cmf/routing is deprecated and %s::%s is not supported.', $className, $constantName) | ||
)->tip('Change record: https://www.drupal.org/node/3151009')->build(), | ||
]; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message( | ||
sprintf('%s::%s is deprecated and removed in Drupal 10. Use \Drupal\Core\Routing\RouteObjectInterface::%2$s instead.', $className, $constantName) | ||
)->tip('Change record: https://www.drupal.org/node/3151009')->build(), | ||
]; | ||
} | ||
} |
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/SymfonyCmfRouteObjectInterfaceConstantsRuleTest.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\SymfonyCmfRouteObjectInterfaceConstantsRule; | ||
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase; | ||
|
||
final class SymfonyCmfRouteObjectInterfaceConstantsRuleTest extends DrupalRuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new SymfonyCmfRouteObjectInterfaceConstantsRule(); | ||
} | ||
|
||
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'], | ||
[ | ||
[ | ||
'Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_NAME is deprecated and removed in Drupal 10. Use \Drupal\Core\Routing\RouteObjectInterface::ROUTE_NAME instead.', | ||
6, | ||
'Change record: https://www.drupal.org/node/3151009' | ||
], | ||
[ | ||
'Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT is deprecated and removed in Drupal 10. Use \Drupal\Core\Routing\RouteObjectInterface::ROUTE_OBJECT instead.', | ||
7, | ||
'Change record: https://www.drupal.org/node/3151009' | ||
], | ||
[ | ||
'Symfony\Cmf\Component\Routing\RouteObjectInterface::CONTROLLER_NAME is deprecated and removed in Drupal 10. Use \Drupal\Core\Routing\RouteObjectInterface::CONTROLLER_NAME instead.', | ||
8, | ||
'Change record: https://www.drupal.org/node/3151009' | ||
], | ||
[ | ||
'The core dependency symfony-cmf/routing is deprecated and Symfony\Cmf\Component\Routing\RouteObjectInterface::TEMPLATE_NAME is not supported.', | ||
9, | ||
'Change record: https://www.drupal.org/node/3151009' | ||
], | ||
] | ||
); | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace SymfonyCmfRoutingUsage; | ||
|
||
class Foo { | ||
public const NAME = \Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_NAME; | ||
public const OBJECT = \Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT; | ||
public const CONTROLLER = \Symfony\Cmf\Component\Routing\RouteObjectInterface::CONTROLLER_NAME; | ||
public const UNMAPPED = \Symfony\Cmf\Component\Routing\RouteObjectInterface::TEMPLATE_NAME; | ||
public function a(\Symfony\Cmf\Component\Routing\RouteObjectInterface $object) { | ||
|
||
} | ||
public function b(\Symfony\Cmf\Component\Routing\RouteProviderInterface $provider): \Symfony\Cmf\Component\Routing\RouteObjectInterface { | ||
|
||
} | ||
public function c(\Symfony\Cmf\Component\Routing\LazyRouteCollection $collection) { | ||
|
||
} | ||
} | ||
class Bar { | ||
public const NAME = \Drupal\Core\Routing\RouteObjectInterface::ROUTE_NAME; | ||
public const OBJECT = \Drupal\Core\Routing\RouteObjectInterface::ROUTE_OBJECT; | ||
public const CONTROLLER = \Drupal\Core\Routing\RouteObjectInterface::CONTROLLER_NAME; | ||
public function a(\Drupal\Core\Routing\RouteObjectInterface $object) { | ||
|
||
} | ||
public function b(\Drupal\Core\Routing\RouteProviderInterface $provider): \Drupal\Core\Routing\RouteObjectInterface { | ||
|
||
} | ||
public function c(\Drupal\Core\Routing\LazyRouteCollection $collection) { | ||
|
||
} | ||
} |