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

[DX] Remove isInClassNamed() #5914

Merged
merged 3 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 1 addition & 50 deletions packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassLike;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
Expand All @@ -24,7 +21,6 @@
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
use Rector\NodeNameResolver\Regex\RegexPatternDetector;
use Rector\NodeTypeResolver\FileSystem\CurrentFileInfoProvider;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\SmartFileSystem\SmartFileInfo;

final class NodeNameResolver
Expand Down Expand Up @@ -59,11 +55,6 @@ final class NodeNameResolver
*/
private $betterStandardPrinter;

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

/**
* @param NodeNameResolverInterface[] $nodeNameResolvers
*/
Expand All @@ -72,15 +63,13 @@ public function __construct(
BetterStandardPrinter $betterStandardPrinter,
CurrentFileInfoProvider $currentFileInfoProvider,
ClassNaming $classNaming,
ReflectionProvider $reflectionProvider,
array $nodeNameResolvers = []
) {
$this->regexPatternDetector = $regexPatternDetector;
$this->nodeNameResolvers = $nodeNameResolvers;
$this->currentFileInfoProvider = $currentFileInfoProvider;
$this->betterStandardPrinter = $betterStandardPrinter;
$this->classNaming = $classNaming;
$this->reflectionProvider = $reflectionProvider;
}

/**
Expand All @@ -102,7 +91,7 @@ public function isNames(Node $node, array $names): bool
*/
public function isName($node, string $name): bool
{
if ($node instanceof MethodCall) {
if ($node instanceof MethodCall || $node instanceof StaticCall) {
$message = sprintf(
'Name called on "%s" is not possible. Use $this->getName($node->name) instead',
get_class($node)
Expand Down Expand Up @@ -212,44 +201,6 @@ public function endsWith(string $currentName, string $expectedName): bool
return (bool) Strings::match($currentName, $suffixNamePattern);
}

/**
* @param ObjectType[] $desiredObjectTypes
*/
public function isInClassNames(Node $node, array $desiredObjectTypes): bool
{
$classNode = $node->getAttribute(AttributeKey::CLASS_NODE);
if ($classNode === null) {
return false;
}

foreach ($desiredObjectTypes as $desiredObjectType) {
if ($this->isInClassNamed($classNode, $desiredObjectType)) {
return true;
}
}

return false;
}

public function isInClassNamed(Node $node, ObjectType $objectType): bool
{
$className = $node->getAttribute(AttributeKey::CLASS_NAME);
if ($className === null) {
return false;
}

if (! $this->reflectionProvider->hasClass($className)) {
return false;
}

$classReflection = $this->reflectionProvider->getClass($className);
if ($classReflection->getName() === $objectType->getClassName()) {
return true;
}

return $classReflection->isSubclassOf($objectType->getClassName());
}

/**
* @param string|Name|Identifier|ClassLike $name
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use Rector\Core\Configuration\Option;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -74,8 +77,18 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
/** @var Scope $scope */
$scope = $node->getAttribute(AttributeKey::SCOPE);

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return null;
}

$classObjectType = new ObjectType($classReflection->getName());

foreach ($this->staticObjectTypes as $staticObjectType) {
if (! $this->nodeNameResolver->isInClassNamed($node, $staticObjectType)) {
if (! $staticObjectType->isSuperTypeOf($classObjectType)->yes()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use Rector\Core\Configuration\Option;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -83,9 +85,19 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
/** @var Scope $scope */
$scope = $node->getAttribute(AttributeKey::SCOPE);

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return null;
}

$classObjectType = new ObjectType($classReflection->getName());

// A. remove local fetch
foreach ($this->staticObjectTypes as $staticObjectType) {
if (! $this->nodeNameResolver->isInClassNamed($node, $staticObjectType)) {
if (! $staticObjectType->isSuperTypeOf($classObjectType)->yes()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
Expand Down Expand Up @@ -155,7 +157,7 @@ public function refactor(Node $node): ?Node
}

/**
* @param array<string, mixed[]> $configuration
* @param array<string, InferParamFromClassMethodReturn[]> $configuration
*/
public function configure(array $configuration): void
{
Expand All @@ -169,10 +171,17 @@ private function matchReturnClassMethod(
ClassMethod $classMethod,
InferParamFromClassMethodReturn $inferParamFromClassMethodReturn
): ?ClassMethod {
if (! $this->nodeNameResolver->isInClassNamed(
$classMethod,
$inferParamFromClassMethodReturn->getObjectType()
)) {
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return null;
}

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return null;
}

if (! $classReflection->isSubclassOf($inferParamFromClassMethodReturn->getClass())) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Rector\Restoration\ValueObject;

use PHPStan\Type\ObjectType;

final class InferParamFromClassMethodReturn
{
/**
Expand All @@ -30,9 +28,9 @@ public function __construct(string $class, string $paramMethod, string $returnMe
$this->returnMethod = $returnMethod;
}

public function getObjectType(): ObjectType
public function getClass(): string
{
return new ObjectType($this->class);
return $this->class;
}

public function getParamMethod(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\Rector\ClassSyncerRectorInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class AssignNewDocParserRector extends AbstractRector implements ClassSyncerRectorInterface
{
/**
* @return array<class-string<\PhpParser\Node>>
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
Expand All @@ -29,10 +31,15 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->nodeNameResolver->isInClassNamed(
$node,
new ObjectType('Doctrine\Common\Annotations\AnnotationReader')
)) {
/** @var Scope $scope */
$scope = $node->getAttribute(AttributeKey::SCOPE);

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return null;
}

if ($classReflection->getName() !== 'Doctrine\Common\Annotations\AnnotationReader') {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectType;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\Rector\ClassSyncerRectorInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -29,10 +31,15 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->nodeNameResolver->isInClassNamed(
$node,
new ObjectType('Doctrine\Common\Annotations\AnnotationReader')
)) {
/** @var Scope $scope */
$scope = $node->getAttribute(AttributeKey::SCOPE);

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return null;
}

if ($classReflection->getName() !== 'Doctrine\Common\Annotations\AnnotationReader') {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\ObjectType;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Rector\AbstractRector;
use Rector\DoctrineAnnotationGenerated\DataCollector\ResolvedConstantStaticCollector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\Rector\ClassSyncerRectorInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -35,7 +37,15 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->nodeNameResolver->isInClassNamed($node, new ObjectType('Doctrine\Common\Annotations\DocParser'))) {
/** @var Scope $scope */
$scope = $node->getAttribute(AttributeKey::SCOPE);

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return null;
}

if ($classReflection->getName() !== 'Doctrine\Common\Annotations\DocParser') {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Utils\DoctrineAnnotationParserSyncer\Contract\Rector\ClassSyncerRectorInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -53,12 +56,19 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$desiredObjectTypes = [
new ObjectType('Doctrine\Common\Annotations\DocParser'),
new ObjectType('Doctrine\Common\Annotations\AnnotationReader'),
];
$scope = $node->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return null;
}

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return null;
}

if (! $this->nodeNameResolver->isInClassNames($node, $desiredObjectTypes)) {
if (! $classReflection->isSubclassOf(
'Doctrine\Common\Annotations\DocParser'
) && ! $classReflection->isSubclassOf('Doctrine\Common\Annotations\AnnotationReader')) {
return null;
}

Expand Down