-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathMissingPropertiesResolver.php
More file actions
57 lines (45 loc) · 1.8 KB
/
MissingPropertiesResolver.php
File metadata and controls
57 lines (45 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\NodeAnalyzer;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ClassReflection;
use Rector\CodeQuality\ValueObject\DefinedPropertyWithType;
use Rector\NodeAnalyzer\PropertyPresenceChecker;
final readonly class MissingPropertiesResolver
{
public function __construct(
private ClassLikeAnalyzer $classLikeAnalyzer,
private PropertyPresenceChecker $propertyPresenceChecker,
) {
}
/**
* @param DefinedPropertyWithType[] $definedPropertiesWithTypes
* @return DefinedPropertyWithType[]
*/
public function resolve(Class_ $class, ClassReflection $classReflection, array $definedPropertiesWithTypes): array
{
$existingPropertyNames = $this->classLikeAnalyzer->resolvePropertyNames($class);
$missingPropertiesWithTypes = [];
foreach ($definedPropertiesWithTypes as $definedPropertyWithType) {
// 1. property already exists, skip it
if (in_array($definedPropertyWithType->getName(), $existingPropertyNames, true)) {
continue;
}
// 2. is part of class docblock or another magic, skip it
if ($classReflection->hasInstanceProperty($definedPropertyWithType->getName())) {
continue;
}
// 3. is fetched by parent class on non-private property etc., skip it
$hasClassContextProperty = $this->propertyPresenceChecker->hasClassContextProperty(
$class,
$definedPropertyWithType
);
if ($hasClassContextProperty) {
continue;
}
// it's most likely missing!
$missingPropertiesWithTypes[] = $definedPropertyWithType;
}
return $missingPropertiesWithTypes;
}
}