Skip to content

Commit

Permalink
remove isInClassNamed()
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Mar 20, 2021
1 parent 3586606 commit 805bd1a
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 100 deletions.
46 changes: 0 additions & 46 deletions packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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 +23,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 +57,6 @@ final class NodeNameResolver
*/
private $betterStandardPrinter;

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

/**
* @param NodeNameResolverInterface[] $nodeNameResolvers
*/
Expand All @@ -80,7 +73,6 @@ public function __construct(
$this->currentFileInfoProvider = $currentFileInfoProvider;
$this->betterStandardPrinter = $betterStandardPrinter;
$this->classNaming = $classNaming;
$this->reflectionProvider = $reflectionProvider;
}

/**
Expand Down Expand Up @@ -212,44 +204,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
25 changes: 0 additions & 25 deletions rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
namespace Rector\Php70\Rector\ClassMethod;

use PhpParser\Node;
<<<<<<< HEAD
use PhpParser\Node\Expr;
=======
>>>>>>> cd14c85316... cleanup
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
Expand Down Expand Up @@ -210,7 +207,6 @@ private function resolveParentClassName(StaticCall $staticCall): ?string
return $parentClassReflection->getName();
}

<<<<<<< HEAD
private function isLocalMethodCallNamed(Expr $expr, string $name): bool
{
if (! $expr instanceof MethodCall) {
Expand All @@ -230,26 +226,5 @@ private function isLocalMethodCallNamed(Expr $expr, string $name): bool
}

return $this->isName($expr->name, $name);
=======
private function isLocalMethodCallNamed(Node $node, string $name): bool
{
if (! $node instanceof MethodCall) {
return false;
}

if ($node->var instanceof StaticCall) {
return false;
}

if ($node->var instanceof MethodCall) {
return false;
}

if (! $this->isName($node->var, 'this')) {
return false;
}

return $this->isName($node->name, $name);
>>>>>>> cd14c85316... cleanup
}
}
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

0 comments on commit 805bd1a

Please sign in to comment.