diff --git a/src/Rector/v9/v4/UseClassSchemaInsteadReflectionServiceMethodsRector.php b/src/Rector/v9/v4/UseClassSchemaInsteadReflectionServiceMethodsRector.php index f48e61b4f..e204f4994 100644 --- a/src/Rector/v9/v4/UseClassSchemaInsteadReflectionServiceMethodsRector.php +++ b/src/Rector/v9/v4/UseClassSchemaInsteadReflectionServiceMethodsRector.php @@ -6,26 +6,13 @@ use PhpParser\Node; use PhpParser\Node\Expr\ArrayDimFetch; -use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\BinaryOp\Coalesce; -use PhpParser\Node\Expr\BinaryOp\Identical; -use PhpParser\Node\Expr\Cast\Bool_; -use PhpParser\Node\Expr\Closure; -use PhpParser\Node\Expr\ClosureUse; -use PhpParser\Node\Expr\Empty_; -use PhpParser\Node\Expr\Isset_; use PhpParser\Node\Expr\MethodCall; -use PhpParser\Node\Expr\Ternary; -use PhpParser\Node\Expr\Variable; use PhpParser\Node\Param; use PhpParser\Node\Scalar\String_; -use PhpParser\Node\Stmt\Expression; -use PhpParser\Node\Stmt\Return_; use PHPStan\Type\ObjectType; use Rector\Core\Rector\AbstractRector; use Rector\Core\ValueObject\PhpVersionFeature; -use Rector\NodeTypeResolver\Node\AttributeKey; -use Rector\PostRector\Collector\NodesToAddCollector; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -46,16 +33,6 @@ final class UseClassSchemaInsteadReflectionServiceMethodsRector extends Abstract */ private const TAGS = 'tags'; - /** - * @readonly - */ - public NodesToAddCollector $nodesToAddCollector; - - public function __construct(NodesToAddCollector $nodesToAddCollector) - { - $this->nodesToAddCollector = $nodesToAddCollector; - } - /** * @codeCoverageIgnore */ @@ -123,15 +100,12 @@ public function refactor(Node $node): ?Node if (! $this->isNames($node->name, [ 'getClassPropertyNames', - 'getPropertyTagsValues', 'getPropertyTagValues', 'getClassTagsValues', 'getClassTagValues', 'getMethodTagsValues', self::HAS_METHOD, 'getMethodParameters', - 'isClassTaggedWith', - 'isPropertyTaggedWith', ])) { return null; } @@ -150,10 +124,6 @@ public function refactor(Node $node): ?Node return $this->refactorGetClassPropertyNamesMethod($node); } - if ($nodeName === 'getPropertyTagsValues') { - return $this->refactorGetPropertyTagsValuesMethod($node); - } - if ($nodeName === 'getPropertyTagValues') { return $this->refactorGetPropertyTagValuesMethod($node); } @@ -174,15 +144,7 @@ public function refactor(Node $node): ?Node return $this->refactorHasMethod($node); } - if ($nodeName === 'getMethodParameters') { - return $this->refactorGetMethodParameters($node); - } - - if ($nodeName === 'isClassTaggedWith') { - return $this->refactorIsClassTaggedWith($node); - } - - return $this->refactorIsPropertyTaggedWith($node); + return $this->refactorGetMethodParameters($node); } public function provideMinPhpVersion(): int @@ -190,23 +152,6 @@ public function provideMinPhpVersion(): int return PhpVersionFeature::NULL_COALESCE; } - private function refactorGetPropertyTagsValuesMethod(MethodCall $methodCall): ?Node - { - if (! isset($methodCall->args[1])) { - return null; - } - - $propertyTagsValuesVariable = new Variable('propertyTagsValues'); - $propertyTagsAssignExpression = new Expression(new Assign($propertyTagsValuesVariable, new Coalesce( - $this->createArrayDimFetchTags($methodCall), - $this->nodeFactory->createArray([]) - ))); - - $this->nodesToAddCollector->addNodeBeforeNode($propertyTagsAssignExpression, $methodCall); - - return $propertyTagsValuesVariable; - } - private function refactorGetClassPropertyNamesMethod(MethodCall $methodCall): Node { return $this->nodeFactory->createFuncCall( @@ -305,63 +250,6 @@ private function refactorGetMethodParameters(MethodCall $methodCall): ?Node ), $this->nodeFactory->createArray([])); } - private function refactorIsPropertyTaggedWith(MethodCall $methodCall): ?Node - { - if (! isset($methodCall->args[1], $methodCall->args[2])) { - return null; - } - - $propertyVariable = new Variable('propertyReflectionService'); - $propertyAssignExpression = new Expression(new Assign($propertyVariable, $this->nodeFactory->createMethodCall( - $this->nodeFactory->createMethodCall($methodCall->var, 'getClassSchema', [$methodCall->args[0]->value]), - 'getProperty', - [$methodCall->args[1]->value] - ))); - - $this->nodesToAddCollector->addNodeBeforeNode($propertyAssignExpression, $methodCall); - - return new Ternary( - new Empty_($propertyVariable), - $this->nodeFactory->createFalse(), - new Isset_( - [ - new ArrayDimFetch(new ArrayDimFetch($propertyVariable, new String_( - self::TAGS - )), $methodCall->args[2]->value), - ] - ) - ); - } - - private function refactorIsClassTaggedWith(MethodCall $methodCall): ?Node - { - if (! isset($methodCall->args[1])) { - return null; - } - - $tagValue = $methodCall->args[1]->value; - $closureUse = $tagValue instanceof Variable ? $tagValue : new Variable('tag'); - if (! $tagValue instanceof Variable) { - $tempVarAssignExpression = new Expression(new Assign($closureUse, $tagValue)); - $this->nodesToAddCollector->addNodeBeforeNode( - $tempVarAssignExpression, - $methodCall->getAttribute(AttributeKey::PARENT_NODE) - ); - } - - $anonymousFunction = new Closure(); - $anonymousFunction->uses[] = new ClosureUse($closureUse); - $anonymousFunction->params = [new Param(new Variable('tagName'))]; - $anonymousFunction->stmts[] = new Return_(new Identical(new Variable('tagName'), $closureUse)); - - return new Bool_($this->nodeFactory->createFuncCall('count', [ - $this->nodeFactory->createFuncCall('array_filter', [ - $this->nodeFactory->createFuncCall('array_keys', [$this->refactorGetClassTagsValues($methodCall)]), - $anonymousFunction, - ]), - ])); - } - private function createClassSchema(MethodCall $methodCall): MethodCall { return $this->nodeFactory->createMethodCall($methodCall->var, 'getClassSchema', [$methodCall->args[0]->value]); diff --git a/tests/Rector/v9/v4/UseClassSchemaInsteadReflectionServiceMethodsRector/Fixture/class.php.inc b/tests/Rector/v9/v4/UseClassSchemaInsteadReflectionServiceMethodsRector/Fixture/class.php.inc index 0f71989fb..c661e0fa7 100644 --- a/tests/Rector/v9/v4/UseClassSchemaInsteadReflectionServiceMethodsRector/Fixture/class.php.inc +++ b/tests/Rector/v9/v4/UseClassSchemaInsteadReflectionServiceMethodsRector/Fixture/class.php.inc @@ -46,10 +46,7 @@ class MyClassService { $classTagValues = $this->reflectionService->getClassSchema(stdClass::class)->getTags()['tag'] ?? []; $classTagsValues = $this->reflectionService->getClassSchema(stdClass::class)->getTags(); - $tag = 'tag'; - if ((bool) count(array_filter(array_keys($this->reflectionService->getClassSchema(stdClass::class)->getTags()), function ($tagName) use ($tag) { - return $tagName === $tag; - }))) { + if ($this->reflectionService->isClassTaggedWith(stdClass::class, 'tag')) { } } diff --git a/tests/Rector/v9/v4/UseClassSchemaInsteadReflectionServiceMethodsRector/Fixture/properties.php.inc b/tests/Rector/v9/v4/UseClassSchemaInsteadReflectionServiceMethodsRector/Fixture/properties.php.inc index 97602af6d..a43a53985 100644 --- a/tests/Rector/v9/v4/UseClassSchemaInsteadReflectionServiceMethodsRector/Fixture/properties.php.inc +++ b/tests/Rector/v9/v4/UseClassSchemaInsteadReflectionServiceMethodsRector/Fixture/properties.php.inc @@ -16,30 +16,6 @@ class MyService public function init(): void { $properties = $this->reflectionService->getClassPropertyNames(\stdClass::class); - foreach ($properties as $property) { - $tags = $this->reflectionService->getPropertyTagsValues(\stdClass::class, $property); - $tag = $this->reflectionService->getPropertyTagValues(\stdClass::class, $property, 'tag'); - } - } - - public function fooBarBaz() - { - if ( ! empty($this->reflectionService->getPropertyTagsValues(\stdClass::class, 'property'))) { - // Do something here - } - } - - public function getProperty($property) - { - return $this->reflectionService->getPropertyTagsValues(\stdClass::class, $property); - } - - public function isPropertyTaggedWith($property) - { - if ($this->reflectionService->isPropertyTaggedWith(\stdClass::class, $property, 'tag')) { - // Do something here - } - return $this->reflectionService->isPropertyTaggedWith(\stdClass::class, $property, 'tag'); } } @@ -63,35 +39,6 @@ class MyService public function init(): void { $properties = array_keys($this->reflectionService->getClassSchema(\stdClass::class)->getProperties()); - foreach ($properties as $property) { - $propertyTagsValues = $this->reflectionService->getClassSchema(\stdClass::class)->getProperty($property)['tags'] ?? []; - $tags = $propertyTagsValues; - $tag = $this->reflectionService->getClassSchema(\stdClass::class)->getProperty($property)['tags']['tag'] ?? []; - } - } - - public function fooBarBaz() - { - $propertyTagsValues = $this->reflectionService->getClassSchema(\stdClass::class)->getProperty('property')['tags'] ?? []; - if ( ! empty($propertyTagsValues)) { - // Do something here - } - } - - public function getProperty($property) - { - $propertyTagsValues = $this->reflectionService->getClassSchema(\stdClass::class)->getProperty($property)['tags'] ?? []; - return $propertyTagsValues; - } - - public function isPropertyTaggedWith($property) - { - $propertyReflectionService = $this->reflectionService->getClassSchema(\stdClass::class)->getProperty($property); - if (empty($propertyReflectionService) ? false : isset($propertyReflectionService['tags']['tag'])) { - // Do something here - } - $propertyReflectionService = $this->reflectionService->getClassSchema(\stdClass::class)->getProperty($property); - return empty($propertyReflectionService) ? false : isset($propertyReflectionService['tags']['tag']); } }