-
Notifications
You must be signed in to change notification settings - Fork 74
/
FunctionalTestDefaultThemePropertyRector.php
120 lines (103 loc) · 4.05 KB
/
FunctionalTestDefaultThemePropertyRector.php
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace DrupalRector\Drupal8\Rector\Deprecation;
use Drupal\Tests\BrowserTestBase;
use PhpParser\Builder\Property;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Exception\ShouldNotHappenException;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractScopeAwareRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://www.drupal.org/node/3083055
*
* @see \DrupalRector\Tests\Rector\Class_\FunctionalTestDefaultThemePropertyRector\FunctionalTestDefaultThemePropertyRectorTest
*/
final class FunctionalTestDefaultThemePropertyRector extends AbstractScopeAwareRector
{
/**
* @var PhpDocInfoFactory
*/
private PhpDocInfoFactory $phpDocInfoFactory;
/**
* @var ValueResolver
*/
private ValueResolver $valueResolver;
public function __construct(ValueResolver $valueResolver, PhpDocInfoFactory $phpDocInfoFactory)
{
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->valueResolver = $valueResolver;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Adds $defaultTheme property to Functional and FunctionalJavascript tests which do not have them.', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClassTest {
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClassTest {
protected $defaultTheme = 'stark'
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Node\Stmt\Class_::class];
}
/**
* @param Node\Stmt\Class_ $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
assert($node instanceof Node\Stmt\Class_);
if ($node->isAbstract() || $node->isAnonymous()) {
return null;
}
$type = $this->nodeTypeResolver->getType($node);
if (count($type->getObjectClassNames()) === 0 || !$type->isObject()->yes()) {
return null;
}
assert($type instanceof ObjectType);
$browserTestBaseType = new ObjectType(BrowserTestBase::class);
if ($type->isSmallerThanOrEqual($browserTestBaseType)->yes()) {
return null;
}
if ($type->isSuperTypeOf(new ObjectType(BrowserTestBase::class))->no()) {
return null;
}
$classReflection = $type->getClassReflection();
if ($classReflection === null || !$classReflection->hasNativeProperty('defaultTheme')) {
throw new ShouldNotHappenException(sprintf('Functional test class %s should have had a defaultTheme property but one not found.', $type->getClassName()));
}
$defaultThemeProperty = $classReflection->getProperty('defaultTheme', $scope);
assert($defaultThemeProperty instanceof \PHPStan\Reflection\Php\PhpPropertyReflection);
$reflectionProperty = $defaultThemeProperty->getNativeReflection();
$betterReflection = $reflectionProperty->getBetterReflection();
$defaultValueExpression = $betterReflection->getDefaultValueExpression();
if ($defaultValueExpression instanceof Node\Scalar\String_ && strlen($this->valueResolver->getValue($defaultValueExpression)) > 0) {
return null;
}
// Is processed by \Rector\PostRector\Rector\PropertyAddingPostRector
// Sets as `private`, which we need `protected` and default value.
$propertyBuilder = new Property('defaultTheme');
$propertyBuilder->makeProtected();
$propertyBuilder->setDefault('stark');
$propertyBuilder->setDocComment("/**\n * {@inheritdoc}\n */");
$property = $propertyBuilder->getNode();
$this->phpDocInfoFactory->createFromNode($property);
$node->stmts = array_merge([$property, new Node\Stmt\Nop()], $node->stmts);
return $node;
}
}