Skip to content

Commit

Permalink
TASK: Remove some very rare cases from UseClassSchemaInsteadReflectio…
Browse files Browse the repository at this point in the history
…nServiceMethodsRector (#4009)
  • Loading branch information
sabbelasichon authored Jan 9, 2024
1 parent 4247cd7 commit 88ec054
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
Expand All @@ -174,39 +144,14 @@ 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
{
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(
Expand Down Expand Up @@ -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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand All @@ -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']);
}
}

Expand Down

0 comments on commit 88ec054

Please sign in to comment.