Skip to content

Commit a4afc09

Browse files
committed
optimize
1 parent 936c901 commit a4afc09

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

rules/PHPUnit120/Rector/Class_/AllowMockObjectsWithoutExpectationsAttributeRector.php

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Rector\PHPUnit\Enum\PHPUnitClassName;
1818
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
1919
use Rector\Rector\AbstractRector;
20+
use Rector\ValueObject\MethodName;
2021
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2122
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2223

@@ -44,30 +45,14 @@ public function getNodeTypes(): array
4445
*/
4546
public function refactor(Node $node): ?Class_
4647
{
47-
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
48+
if ($this->shouldSkipClass($node)) {
4849
return null;
4950
}
5051

51-
// attribute must exist for the rule to work
52-
if (! $this->reflectionProvider->hasClass(PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS)) {
53-
return null;
54-
}
52+
$mockObjectPropertyNames = $this->matchMockObjectPropertyNames($node);
5553

56-
// already filled
57-
if ($this->attributeFinder->hasAttributeByClasses(
58-
$node,
59-
[PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS]
60-
)) {
61-
return null;
62-
}
63-
64-
// has mock objects properties and setUp() method?
65-
66-
if (! $node->getMethod('setUp') instanceof ClassMethod) {
67-
return null;
68-
}
69-
70-
if (! $this->hasMockObjectProperty($node)) {
54+
// there are no mock object properties
55+
if ($mockObjectPropertyNames === []) {
7156
return null;
7257
}
7358

@@ -156,18 +141,50 @@ public function testTwo(): void
156141

157142
}
158143

159-
private function hasMockObjectProperty(Class_ $class): bool
144+
/**
145+
* @return string[]
146+
*/
147+
private function matchMockObjectPropertyNames(Class_ $class): array
160148
{
149+
$propertyNames = [];
150+
161151
foreach ($class->getProperties() as $property) {
162152
if (! $property->type instanceof Name) {
163153
continue;
164154
}
165155

166-
if ($this->isName($property->type, PHPUnitClassName::MOCK_OBJECT)) {
167-
return true;
156+
if (! $this->isName($property->type, PHPUnitClassName::MOCK_OBJECT)) {
157+
continue;
168158
}
159+
160+
$propertyNames[] = $this->getName($property->props[0]);
161+
}
162+
163+
return $propertyNames;
164+
}
165+
166+
private function shouldSkipClass(Class_ $class): bool
167+
{
168+
if (! $this->testsNodeAnalyzer->isInTestClass($class)) {
169+
return true;
169170
}
170171

171-
return false;
172+
// attribute must exist for the rule to work
173+
if (! $this->reflectionProvider->hasClass(PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS)) {
174+
return true;
175+
}
176+
177+
// already filled
178+
if ($this->attributeFinder->hasAttributeByClasses(
179+
$class,
180+
[PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS]
181+
)) {
182+
return true;
183+
}
184+
185+
// has mock objects properties and setUp() method?
186+
187+
$setupClassMethod = $class->getMethod(MethodName::SET_UP);
188+
return ! $setupClassMethod instanceof ClassMethod;
172189
}
173190
}

0 commit comments

Comments
 (0)