Repository navigation
Expand file tree
/
Copy pathSuffixMockObjectPropertyRector.php
More file actions
89 lines (77 loc) · 2.33 KB
/
Copy pathSuffixMockObjectPropertyRector.php
File metadata and controls
89 lines (77 loc) · 2.33 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
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
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
/**
* @deprecated This rule is deprecated, as renaming properties can produce invalid variable names and collide with
* existing ones. Use a coding standard to enforce the "Mock" suffix instead.
*/
final class SuffixMockObjectPropertyRector extends AbstractRector implements DeprecatedInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Suffix mock object property names with "Mock" to clearly separate from real objects later on',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
final class MockingEntity extends TestCase
{
private MockObject $simpleObject;
protected function setUp(): void
{
$this->simpleObject = $this->createMock(SimpleObject::class);
}
public function test()
{
$this->simpleObject->method('someMethod')->willReturn('someValue');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
final class MockingEntity extends TestCase
{
private MockObject $simpleObjectMock;
protected function setUp(): void
{
$this->simpleObjectMock = $this->createMock(SimpleObject::class);
}
public function test()
{
$this->simpleObjectMock->method('someMethod')->willReturn('someValue');
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
throw new ShouldNotHappenException(sprintf(
'"%s" is deprecated, as renaming properties can produce invalid variable names. Use a coding standard to enforce the "Mock" suffix instead.',
self::class,
));
}
}