2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\Class_;
|
2019-05-26 19:21:31 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PHPStan\Reflection\ClassReflection;
|
|
|
|
use PHPStan\Reflection\ReflectionProvider;
|
|
|
|
use PHPStan\Type\ObjectType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\CodeQuality\NodeAnalyzer\ClassLikeAnalyzer;
|
|
|
|
use Rector\CodeQuality\NodeAnalyzer\LocalPropertyAnalyzer;
|
|
|
|
use Rector\CodeQuality\NodeFactory\MissingPropertiesFactory;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\NodeAnalyzer\ClassAnalyzer;
|
|
|
|
use Rector\NodeAnalyzer\PropertyPresenceChecker;
|
2023-02-06 15:51:07 +00:00
|
|
|
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\PostRector\ValueObject\PropertyMetadata;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-05-26 19:21:31 +00:00
|
|
|
/**
|
2022-05-12 19:17:07 +00:00
|
|
|
* @changelog https://3v4l.org/GL6II
|
|
|
|
* @changelog https://3v4l.org/eTrhZ
|
|
|
|
* @changelog https://3v4l.org/C554W
|
2019-09-06 10:30:58 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector\CompleteDynamicPropertiesRectorTest
|
2019-05-26 19:21:31 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class CompleteDynamicPropertiesRector extends AbstractRector
|
2019-05-26 19:21:31 +00:00
|
|
|
{
|
2019-07-10 07:23:37 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodeQuality\NodeFactory\MissingPropertiesFactory
|
2019-05-26 19:21:31 +00:00
|
|
|
*/
|
2020-10-20 20:27:12 +00:00
|
|
|
private $missingPropertiesFactory;
|
2020-10-20 18:35:35 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodeQuality\NodeAnalyzer\LocalPropertyAnalyzer
|
2020-10-20 18:35:35 +00:00
|
|
|
*/
|
2020-10-20 20:27:12 +00:00
|
|
|
private $localPropertyAnalyzer;
|
2020-10-20 18:35:35 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodeQuality\NodeAnalyzer\ClassLikeAnalyzer
|
2020-10-20 18:35:35 +00:00
|
|
|
*/
|
2020-10-20 20:27:12 +00:00
|
|
|
private $classLikeAnalyzer;
|
2021-02-28 07:47:48 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-08-23 00:20:32 +00:00
|
|
|
* @var \PHPStan\Reflection\ReflectionProvider
|
2021-02-28 07:47:48 +00:00
|
|
|
*/
|
|
|
|
private $reflectionProvider;
|
2021-04-23 19:32:59 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeAnalyzer\ClassAnalyzer
|
2021-04-23 19:32:59 +00:00
|
|
|
*/
|
|
|
|
private $classAnalyzer;
|
2021-08-09 21:55:37 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeAnalyzer\PropertyPresenceChecker
|
2021-08-09 21:55:37 +00:00
|
|
|
*/
|
|
|
|
private $propertyPresenceChecker;
|
2023-02-06 15:51:07 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2023-02-06 15:51:07 +00:00
|
|
|
* @var \Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer
|
|
|
|
*/
|
|
|
|
private $phpAttributeAnalyzer;
|
|
|
|
public function __construct(MissingPropertiesFactory $missingPropertiesFactory, LocalPropertyAnalyzer $localPropertyAnalyzer, ClassLikeAnalyzer $classLikeAnalyzer, ReflectionProvider $reflectionProvider, ClassAnalyzer $classAnalyzer, PropertyPresenceChecker $propertyPresenceChecker, PhpAttributeAnalyzer $phpAttributeAnalyzer)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2020-10-20 18:35:35 +00:00
|
|
|
$this->missingPropertiesFactory = $missingPropertiesFactory;
|
2020-10-20 20:27:12 +00:00
|
|
|
$this->localPropertyAnalyzer = $localPropertyAnalyzer;
|
|
|
|
$this->classLikeAnalyzer = $classLikeAnalyzer;
|
2021-02-28 07:47:48 +00:00
|
|
|
$this->reflectionProvider = $reflectionProvider;
|
2021-04-23 19:32:59 +00:00
|
|
|
$this->classAnalyzer = $classAnalyzer;
|
2021-08-09 21:55:37 +00:00
|
|
|
$this->propertyPresenceChecker = $propertyPresenceChecker;
|
2023-02-06 15:51:07 +00:00
|
|
|
$this->phpAttributeAnalyzer = $phpAttributeAnalyzer;
|
2019-05-26 19:21:31 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-05-26 19:21:31 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Add missing dynamic properties', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-05-26 19:21:31 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function set()
|
|
|
|
{
|
|
|
|
$this->value = 5;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-05-26 19:21:31 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $value;
|
2021-04-04 09:01:11 +00:00
|
|
|
|
2019-05-26 19:21:31 +00:00
|
|
|
public function set()
|
|
|
|
{
|
|
|
|
$this->value = 5;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-05-26 19:21:31 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-05-26 19:21:31 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-05-26 19:21:31 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Class_::class];
|
2019-05-26 19:21:31 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Class_ $node
|
2019-05-26 19:21:31 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-05-26 19:21:31 +00:00
|
|
|
{
|
2020-10-20 20:27:12 +00:00
|
|
|
if ($this->shouldSkipClass($node)) {
|
2019-06-15 23:15:35 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-02-28 07:47:48 +00:00
|
|
|
$className = $this->getName($node);
|
|
|
|
if ($className === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->reflectionProvider->hasClass($className)) {
|
2021-02-28 07:47:48 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$classReflection = $this->reflectionProvider->getClass($className);
|
2019-07-10 07:23:37 +00:00
|
|
|
// special case for Laravel Collection macro magic
|
2021-05-09 20:15:43 +00:00
|
|
|
$fetchedLocalPropertyNameToTypes = $this->localPropertyAnalyzer->resolveFetchedPropertiesToTypesFromClass($node);
|
2020-02-02 18:15:36 +00:00
|
|
|
$propertiesToComplete = $this->resolvePropertiesToComplete($node, $fetchedLocalPropertyNameToTypes);
|
|
|
|
if ($propertiesToComplete === []) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-08-09 21:55:37 +00:00
|
|
|
$propertiesToComplete = $this->filterOutExistingProperties($node, $classReflection, $propertiesToComplete);
|
2021-05-09 20:15:43 +00:00
|
|
|
$newProperties = $this->missingPropertiesFactory->create($fetchedLocalPropertyNameToTypes, $propertiesToComplete);
|
2021-09-08 10:48:44 +00:00
|
|
|
if ($newProperties === []) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
$node->stmts = \array_merge($newProperties, $node->stmts);
|
2019-05-26 19:21:31 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkipClass(Class_ $class) : bool
|
2019-05-26 19:21:31 +00:00
|
|
|
{
|
2021-03-03 21:51:04 +00:00
|
|
|
if ($this->classAnalyzer->isAnonymousClass($class)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-10-20 20:27:12 +00:00
|
|
|
}
|
2021-11-09 13:36:07 +00:00
|
|
|
$className = (string) $this->nodeNameResolver->getName($class);
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->reflectionProvider->hasClass($className)) {
|
|
|
|
return \true;
|
2021-02-28 07:47:48 +00:00
|
|
|
}
|
2023-02-06 15:51:07 +00:00
|
|
|
if ($this->phpAttributeAnalyzer->hasPhpAttribute($class, 'AllowDynamicProperties')) {
|
|
|
|
return \true;
|
|
|
|
}
|
2021-02-28 07:47:48 +00:00
|
|
|
$classReflection = $this->reflectionProvider->getClass($className);
|
2020-10-20 20:27:12 +00:00
|
|
|
// properties are accessed via magic, nothing we can do
|
2021-02-28 07:47:48 +00:00
|
|
|
if ($classReflection->hasMethod('__set')) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2019-09-06 10:30:58 +00:00
|
|
|
}
|
2021-02-28 07:47:48 +00:00
|
|
|
return $classReflection->hasMethod('__get');
|
2019-05-26 19:21:31 +00:00
|
|
|
}
|
2019-07-10 07:23:37 +00:00
|
|
|
/**
|
2020-08-11 10:59:04 +00:00
|
|
|
* @param array<string, Type> $fetchedLocalPropertyNameToTypes
|
2020-10-20 20:27:12 +00:00
|
|
|
* @return string[]
|
2019-07-10 07:23:37 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function resolvePropertiesToComplete(Class_ $class, array $fetchedLocalPropertyNameToTypes) : array
|
2019-07-10 07:23:37 +00:00
|
|
|
{
|
2020-10-20 20:27:12 +00:00
|
|
|
$propertyNames = $this->classLikeAnalyzer->resolvePropertyNames($class);
|
2020-08-11 10:59:04 +00:00
|
|
|
/** @var string[] $fetchedLocalPropertyNames */
|
2021-05-09 20:15:43 +00:00
|
|
|
$fetchedLocalPropertyNames = \array_keys($fetchedLocalPropertyNameToTypes);
|
|
|
|
return \array_diff($fetchedLocalPropertyNames, $propertyNames);
|
2019-07-10 07:23:37 +00:00
|
|
|
}
|
2020-08-11 10:59:04 +00:00
|
|
|
/**
|
2020-10-20 20:27:12 +00:00
|
|
|
* @param string[] $propertiesToComplete
|
2020-08-11 10:59:04 +00:00
|
|
|
* @return string[]
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function filterOutExistingProperties(Class_ $class, ClassReflection $classReflection, array $propertiesToComplete) : array
|
2020-08-11 10:59:04 +00:00
|
|
|
{
|
2020-10-20 20:27:12 +00:00
|
|
|
$missingPropertyNames = [];
|
2021-08-09 21:55:37 +00:00
|
|
|
$className = $classReflection->getName();
|
2020-10-20 20:27:12 +00:00
|
|
|
// remove other properties that are accessible from this scope
|
|
|
|
foreach ($propertiesToComplete as $propertyToComplete) {
|
2021-02-28 07:47:48 +00:00
|
|
|
if ($classReflection->hasProperty($propertyToComplete)) {
|
2020-10-20 20:27:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-09 10:52:53 +00:00
|
|
|
$propertyMetadata = new PropertyMetadata($propertyToComplete, new ObjectType($className));
|
2021-08-09 21:55:37 +00:00
|
|
|
$hasClassContextProperty = $this->propertyPresenceChecker->hasClassContextProperty($class, $propertyMetadata);
|
|
|
|
if ($hasClassContextProperty) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-20 20:27:12 +00:00
|
|
|
$missingPropertyNames[] = $propertyToComplete;
|
2020-10-20 18:35:35 +00:00
|
|
|
}
|
2020-10-20 20:27:12 +00:00
|
|
|
return $missingPropertyNames;
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
2019-05-26 19:21:31 +00:00
|
|
|
}
|