Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of static reflection from PHPStan #5665

Merged
merged 21 commits into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 6 additions & 1 deletion .github/workflows/rector_ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ jobs:
fail-fast: false
matrix:
directories:
- rules
#- rules
- rules/naming
- rules/privatization
- rules/code-quality
- rules/php74
- packages
- src
- tests
Expand Down Expand Up @@ -54,6 +58,7 @@ jobs:

## First run Rector - here can't be --dry-run !!! it would stop the job with it and not commit anything in the future
- run: bin/rector rectify ${{ matrix.directories }} --ansi --no-progress-bar

- run: vendor/bin/ecs check --match-git-diff --fix --ansi

# see https://github.com/EndBug/add-and-commit
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"nette/utils": "^3.2",
"nikic/php-parser": "^4.10.4",
"phpstan/phpdoc-parser": "^0.4.9",
"phpstan/phpstan": "^0.12.76",
"phpstan/phpstan": "^0.12.79",
"phpstan/phpstan-phpunit": "^0.12.17",
"psr/simple-cache": "^1.0",
"sebastian/diff": "^4.0.4",
Expand Down Expand Up @@ -186,12 +186,11 @@
"rules/autodiscovery/tests/Rector/FileNode/MoveServicesBySuffixToDirectoryRector/Expected",
"rules/cakephp/tests/Rector/FileWithoutNamespace/ImplicitShortClassNameUseStatementRector/Source",
"rules/cakephp/tests/Rector/Namespace_/AppUsesStaticCallToUseStatementRector/Source",
"rules/psr4/tests/Rector/Namespace_/MultipleClassFileToPsr4ClassesRector/Source",
"rules/renaming/tests/Rector/FileWithoutNamespace/PseudoNamespaceToNamespaceRector/Source",
"rules/symfony4/tests/Rector/MethodCall/ContainerGetToConstructorInjectionRector/Source"
],
"files": [
"rules/type-declaration/tests/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Source/external_bool_function.php",
"vendor/nette/forms/src/Forms/Controls/SubmitButton.php",
"rules/restoration/tests/Rector/Use_/RestoreFullyQualifiedNameRector/Source/ShortClassOnly.php",
"rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/AnotherClass.php",
"rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/Foo.php",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\DoctrineAnnotationGenerated\PhpDocNode\ConstantReferenceIdentifierRestorer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\ClassExistenceStaticHelper;
use Rector\NodeTypeResolver\Node\AttributeKey;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use Throwable;
Expand All @@ -41,14 +43,21 @@ final class NodeAnnotationReader
*/
private $constantReferenceIdentifierRestorer;

/**
* @var ReflectionProvider
*/
private $reflectionProvider;

public function __construct(
ConstantReferenceIdentifierRestorer $constantReferenceIdentifierRestorer,
NodeNameResolver $nodeNameResolver,
Reader $reader
Reader $reader,
ReflectionProvider $reflectionProvider
) {
$this->reader = $reader;
$this->nodeNameResolver = $nodeNameResolver;
$this->constantReferenceIdentifierRestorer = $constantReferenceIdentifierRestorer;
$this->reflectionProvider = $reflectionProvider;
}

public function readAnnotation(Node $node, string $annotationClass): ?object
Expand All @@ -71,12 +80,13 @@ public function readAnnotation(Node $node, string $annotationClass): ?object
public function readClassAnnotation(Class_ $class, string $annotationClassName): ?object
{
$classReflection = $this->createClassReflectionFromNode($class);
$nativeClassReflection = $classReflection->getNativeReflection();
TomasVotruba marked this conversation as resolved.
Show resolved Hide resolved

try {
// covers cases like https://github.com/rectorphp/rector/issues/3046

/** @var object[] $classAnnotations */
$classAnnotations = $this->reader->getClassAnnotations($classReflection);
$classAnnotations = $this->reader->getClassAnnotations($nativeClassReflection);
return $this->matchNextAnnotation($classAnnotations, $annotationClassName, $class);
} catch (AnnotationException $annotationException) {
// unable to load
Expand All @@ -86,16 +96,16 @@ public function readClassAnnotation(Class_ $class, string $annotationClassName):

public function readPropertyAnnotation(Property $property, string $annotationClassName): ?object
{
$propertyReflection = $this->createPropertyReflectionFromPropertyNode($property);
if (! $propertyReflection instanceof ReflectionProperty) {
return null;
$reflectionProperty = $this->getNativePropertyReflection($property);
if (! $reflectionProperty instanceof ReflectionProperty) {
throw new ShouldNotHappenException();
}

try {
// covers cases like https://github.com/rectorphp/rector/issues/3046

/** @var object[] $propertyAnnotations */
$propertyAnnotations = $this->reader->getPropertyAnnotations($propertyReflection);
$propertyAnnotations = $this->reader->getPropertyAnnotations($reflectionProperty);
return $this->matchNextAnnotation($propertyAnnotations, $annotationClassName, $property);
} catch (AnnotationException $annotationException) {
// unable to load
Expand All @@ -111,13 +121,14 @@ private function readMethodAnnotation(ClassMethod $classMethod, string $annotati
/** @var string $methodName */
$methodName = $this->nodeNameResolver->getName($classMethod);

$reflectionMethod = new ReflectionMethod($className, $methodName);
$reflectionMethod = $this->resolveNativeClassMethodReflection($className, $methodName);

try {
// covers cases like https://github.com/rectorphp/rector/issues/3046

/** @var object[] $methodAnnotations */
$methodAnnotations = $this->reader->getMethodAnnotations($reflectionMethod);

foreach ($methodAnnotations as $methodAnnotation) {
if (! is_a($methodAnnotation, $annotationClassName, true)) {
continue;
Expand All @@ -141,14 +152,13 @@ private function readMethodAnnotation(ClassMethod $classMethod, string $annotati
return null;
}

private function createClassReflectionFromNode(Class_ $class): ReflectionClass
private function createClassReflectionFromNode(Class_ $class): ClassReflection
{
/** @var string $className */
$className = $this->nodeNameResolver->getName($class);

// covers cases like https://github.com/rectorphp/rector/issues/3230#issuecomment-683317288

return new ReflectionClass($className);
return $this->reflectionProvider->getClass($className);
}

/**
Expand All @@ -175,7 +185,7 @@ private function matchNextAnnotation(array $annotations, string $annotationClass
return null;
}

private function createPropertyReflectionFromPropertyNode(Property $property): ?ReflectionProperty
private function getNativePropertyReflection(Property $property): ?ReflectionProperty
{
/** @var string $propertyName */
$propertyName = $this->nodeNameResolver->getName($property);
Expand All @@ -186,16 +196,35 @@ private function createPropertyReflectionFromPropertyNode(Property $property): ?
// probably fresh node
return null;
}
if (! ClassExistenceStaticHelper::doesClassLikeExist($className)) {

if (! $this->reflectionProvider->hasClass($className)) {
// probably fresh node
return null;
}

try {
return new ReflectionProperty($className, $propertyName);
$classReflection = $this->reflectionProvider->getClass($className);
$scope = $property->getAttribute(AttributeKey::SCOPE);

$propertyReflection = $classReflection->getProperty($propertyName, $scope);
if ($propertyReflection instanceof PhpPropertyReflection) {
return $propertyReflection->getNativeReflection();
}
} catch (Throwable $throwable) {
// in case of PHPUnit property or just-added property
return null;
}
}

private function resolveNativeClassMethodReflection(string $className, string $methodName): ReflectionMethod
{
if (! $this->reflectionProvider->hasClass($className)) {
throw new ShouldNotHappenException();
}

$classReflection = $this->reflectionProvider->getClass($className);
$reflectionClass = $classReflection->getNativeReflection();

return $reflectionClass->getMethod($methodName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nette\Utils\Strings;
use PhpParser\Node;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\Annotation\AnnotationItemsResolver;
use Rector\BetterPhpDocParser\AnnotationReader\NodeAnnotationReader;
Expand Down Expand Up @@ -56,19 +57,26 @@ abstract class AbstractPhpDocNodeFactory
*/
private $objectTypeSpecifier;

/**
* @var ReflectionProvider
*/
private $reflectionProvider;

/**
* @required
*/
public function autowireAbstractPhpDocNodeFactory(
NodeAnnotationReader $nodeAnnotationReader,
AnnotationContentResolver $annotationContentResolver,
AnnotationItemsResolver $annotationItemsResolver,
ObjectTypeSpecifier $objectTypeSpecifier
ObjectTypeSpecifier $objectTypeSpecifier,
ReflectionProvider $reflectionProvider
): void {
$this->nodeAnnotationReader = $nodeAnnotationReader;
$this->annotationContentResolver = $annotationContentResolver;
$this->annotationItemsResolver = $annotationItemsResolver;
$this->objectTypeSpecifier = $objectTypeSpecifier;
$this->reflectionProvider = $reflectionProvider;
}

protected function resolveContentFromTokenIterator(TokenIterator $tokenIterator): string
Expand All @@ -79,12 +87,12 @@ protected function resolveContentFromTokenIterator(TokenIterator $tokenIterator)
protected function resolveFqnTargetEntity(string $targetEntity, Node $node): string
{
$targetEntity = $this->getCleanedUpTargetEntity($targetEntity);
if (class_exists($targetEntity)) {
if ($this->reflectionProvider->hasClass($targetEntity)) {
return $targetEntity;
}

$namespacedTargetEntity = $node->getAttribute(AttributeKey::NAMESPACE_NAME) . '\\' . $targetEntity;
if (class_exists($namespacedTargetEntity)) {
if ($this->reflectionProvider->hasClass($namespacedTargetEntity)) {
return $namespacedTargetEntity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use PHPStan\Reflection\ReflectionProvider;
use Rector\NodeTypeResolver\Node\AttributeKey;

/**
Expand All @@ -20,6 +21,16 @@ final class ClassAnnotationMatcher
*/
private $fullyQualifiedNameByHash = [];

/**
* @var ReflectionProvider
*/
private $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}

public function resolveTagFullyQualifiedName(string $tag, Node $node): string
{
$uniqueHash = $tag . spl_object_hash($node);
Expand Down Expand Up @@ -48,7 +59,7 @@ private function resolveFullyQualifiedClass(array $uses, Node $node, string $tag
$namespace = $node->getAttribute(AttributeKey::NAMESPACE_NAME);
if ($namespace !== null) {
$namespacedTag = $namespace . '\\' . $tag;
if (class_exists($namespacedTag)) {
if ($this->reflectionProvider->hasClass($namespacedTag)) {
return $namespacedTag;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ final class MultilineSpaceFormatPreserver

public function resolveCurrentPhpDocNodeText(Node $node): ?string
{
if ($node instanceof PhpDocTagNode &&
property_exists($node->value, 'description')
) {
if ($node instanceof PhpDocTagNode && property_exists($node->value, 'description')) {
return $node->value->description;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ abstract class AbstractTagValueNode implements AttributeAwareNodeInterface, PhpD
protected $tagValueNodeConfiguration;

/**
* @var ArrayPartPhpDocTagPrinter
* @var TagValueNodePrinter
*/
protected $arrayPartPhpDocTagPrinter;
protected $tagValueNodePrinter;

/**
* @var TagValueNodePrinter
* @var ArrayPartPhpDocTagPrinter
*/
protected $tagValueNodePrinter;
private $arrayPartPhpDocTagPrinter;

/**
* @param array<string, mixed> $items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Rector\BetterPhpDocParser\Contract\PhpDocNode\SilentKeyNodeInterface;
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\AbstractTagValueNode;
use Rector\PhpAttribute\Contract\PhpAttributableTagNodeInterface;
use Symfony\Component\Routing\Annotation\Route;

/**
* @see \Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint\TagValueNodeReprintTest
Expand All @@ -19,7 +18,7 @@ final class SymfonyRouteTagValueNode extends AbstractTagValueNode implements Sho
/**
* @var string
*/
public const CLASS_NAME = Route::class;
public const CLASS_NAME = 'Symfony\Component\Routing\Annotation\Route';

/**
* @var string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,5 @@ class ApiFilter
{
public function __construct($options = [])
{
if(! class_exists($options['value'])) {
throw new ShouldNotHappenException();
}
}
}
Loading