Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Symfony\Tests\Symfony61\Rector\StaticPropertyFetch\ErrorNamesPropertyToConstantRector\Fixture;

use Symfony\Component\Validator\Constraints\NotBlank;

final class ClassProperty extends NotBlank
{
/**
* @var string[]
*/
protected static $errorNames = [];
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony61\Rector\StaticPropertyFetch\ErrorNamesPropertyToConstantRector\Fixture;

use Symfony\Component\Validator\Constraints\NotBlank;

final class ClassProperty extends NotBlank
{
/**
* @var string[]
*/
protected const ERROR_NAMES = [];
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@

namespace Rector\Symfony\Symfony61\Rector\StaticPropertyFetch;

use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Const_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\Symfony\Enum\SymfonyClass;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -61,21 +68,52 @@ class SomeClass
*/
public function getNodeTypes(): array
{
return [StaticPropertyFetch::class];
return [StaticPropertyFetch::class, Class_::class];
}

/**
* @param StaticPropertyFetch $node
* @param StaticPropertyFetch|Class_ $node
*/
public function refactor(Node $node): ?Node
{
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}
if (! $classReflection->is('Symfony\Component\Validator\Constraint')) {

if (! $classReflection->is(SymfonyClass::VALIDATOR_CONSTRAINT)) {
return null;
}

if ($node instanceof StaticPropertyFetch) {
return $this->refactorStaticPropertyFetch($node, $classReflection);
}

foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Property) {
continue;
}

if (! $stmt->isStatic()) {
continue;
}

if (! $this->isName($stmt->props[0], 'errorNames')) {
continue;
}

$node->stmts[$key] = $this->createClassConst($stmt, $stmt);

return $node;
}

return null;
}

private function refactorStaticPropertyFetch(
StaticPropertyFetch $node,
ClassReflection $classReflection
): ?ClassConstFetch {
if (! $this->isName($node->name, 'errorNames')) {
return null;
}
Expand All @@ -87,4 +125,17 @@ public function refactor(Node $node): ?Node

return $this->nodeFactory->createClassConstFetch($parentClass->getName(), 'ERROR_NAMES');
}

private function createClassConst(Property $property, Property $stmt): ClassConst
{
$propertyItem = $property->props[0];

$const = new Const_('ERROR_NAMES', $propertyItem->default);

$classConst = new ClassConst([$const], $stmt->flags & ~Modifiers::STATIC);

$classConst->setDocComment($property->getDocComment());

return $classConst;
}
}
5 changes: 5 additions & 0 deletions src/Enum/SymfonyClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ final class SymfonyClass
*/
public const SYMFONY_VALIDATOR_CONSTRAINTS_COLLECTION = 'Symfony\Component\Validator\Constraints\Collection';

/**
* @var string
*/
public const VALIDATOR_CONSTRAINT = 'Symfony\Component\Validator\Constraint';

/**
* @var string
*/
Expand Down