55namespace Rector \PHPUnit \CodeQuality \Rector \ClassMethod ;
66
77use PhpParser \Node ;
8- use PhpParser \Node \Expr \StaticCall ;
9- use PhpParser \Node \Identifier ;
10- use PhpParser \Node \Name \FullyQualified ;
118use PhpParser \Node \Stmt \ClassMethod ;
12- use PHPStan \Reflection \ReflectionProvider ;
13- use Rector \PHPStan \ScopeFetcher ;
14- use Rector \PHPUnit \Enum \BehatClassName ;
15- use Rector \PHPUnit \Enum \PHPUnitClassName ;
16- use Rector \PHPUnit \Enum \WebmozartClassName ;
9+ use Rector \Configuration \Deprecation \Contract \DeprecatedInterface ;
10+ use Rector \Exception \ShouldNotHappenException ;
1711use Rector \Rector \AbstractRector ;
1812use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
1913use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
2014
2115/**
22- * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BehatPHPUnitAssertToWebmozartRector\BehatPHPUnitAssertToWebmozartRectorTest
16+ * @deprecated This rule is deprecated as too narrow, it only targets Behat context files. Implement it as a custom rule instead.
2317 */
24- final class BehatPHPUnitAssertToWebmozartRector extends AbstractRector
18+ final class BehatPHPUnitAssertToWebmozartRector extends AbstractRector implements DeprecatedInterface
2519{
26- /**
27- * @var array<string, string>
28- */
29- private const array PHPUNIT_TO_WEBMOZART_METHODS = [
30- // Boolean
31- 'assertTrue ' => 'true ' ,
32- 'assertFalse ' => 'false ' ,
33-
34- // Null / empty
35- 'assertNull ' => 'null ' ,
36- 'assertNotNull ' => 'notNull ' ,
37- 'assertEmpty ' => 'isEmpty ' ,
38- 'assertNotEmpty ' => 'notEmpty ' ,
39-
40- // Type checks
41- 'assertIsString ' => 'string ' ,
42- 'assertIsInt ' => 'integer ' ,
43- 'assertIsFloat ' => 'float ' ,
44- 'assertIsBool ' => 'boolean ' ,
45- 'assertIsArray ' => 'isArray ' ,
46- 'assertIsObject ' => 'object ' ,
47- 'assertIsCallable ' => 'isCallable ' ,
48- 'assertIsResource ' => 'resource ' ,
49- 'assertIsIterable ' => 'isIterable ' ,
50- 'assertInstanceOf ' => 'isInstanceOf ' ,
51-
52- // array
53- 'assertContains ' => 'oneOf ' ,
54- 'assertNotContains ' => 'notOneOf ' ,
55-
56- // Comparison / equality
57- 'assertSame ' => 'same ' ,
58- 'assertNotSame ' => 'notSame ' ,
59- 'assertEquals ' => 'eq ' ,
60- 'assertNotEquals ' => 'notEq ' ,
61- 'assertGreaterThan ' => 'greaterThan ' ,
62- 'assertGreaterThanOrEqual ' => 'greaterThanEq ' ,
63- 'assertLessThan ' => 'lessThan ' ,
64- 'assertLessThanOrEqual ' => 'lessThanEq ' ,
65-
66- // Strings
67- 'assertStringContainsString ' => 'contains ' ,
68- 'assertStringNotContainsString ' => 'notContains ' ,
69- 'assertStringStartsWith ' => 'startsWith ' ,
70- 'assertStringStartsNotWith ' => 'notStartsWith ' ,
71- 'assertStringEndsWith ' => 'endsWith ' ,
72- 'assertStringEndsNotWith ' => 'notEndsWith ' ,
73- 'assertMatchesRegularExpression ' => 'regex ' ,
74- 'assertDoesNotMatchRegularExpression ' => 'notRegex ' ,
75-
76- // Bool
77- 'assertNotTrue ' => 'false ' ,
78- 'assertNotFalse ' => 'true ' ,
79-
80- // Arrays / count
81- 'assertCount ' => 'count ' ,
82- 'assertArrayHasKey ' => 'keyExists ' ,
83- 'assertArrayNotHasKey ' => 'keyNotExists ' ,
84-
85- // Misc / less direct
86- 'assertFileExists ' => 'fileExists ' ,
87- 'assertFileIsReadable ' => 'readable ' ,
88- 'assertDirectoryExists ' => 'directory ' ,
89-
90- // Instance of
91- 'assertNotInstanceOf ' => 'notInstanceOf ' ,
92- ];
93-
94- /**
95- * @var string[]
96- */
97- private const array FLIPPED_ARGS = [
98- 'assertSame ' ,
99- 'assertNotSame ' ,
100- 'assertEquals ' ,
101- 'assertNotEquals ' ,
102- 'assertGreaterThan ' ,
103- 'assertGreaterThanOrEqual ' ,
104- 'assertLessThan ' ,
105- 'assertLessThanOrEqual ' ,
106- 'assertCount ' ,
107- 'assertInstanceOf ' ,
108- 'assertNotInstanceOf ' ,
109- 'assertArrayHasKey ' ,
110- 'assertArrayNotHasKey ' ,
111- 'assertStringContainsString ' ,
112- 'assertStringStartsWith ' ,
113- 'assertMatchesRegularExpression ' ,
114- 'assertDoesNotMatchRegularExpression ' ,
115- ];
116-
117- public function __construct (
118- private readonly ReflectionProvider $ reflectionProvider
119- ) {
120- }
121-
12220 public function getRuleDefinition (): RuleDefinition
12321 {
124-
12522 return new RuleDefinition (
12623 'Change PHPUnit assert in Behat context files to Webmozart Assert, as first require a TestCase instance ' ,
12724 [
@@ -152,13 +49,12 @@ public function someMethod()
15249}
15350CODE_SAMPLE
15451 ),
155-
15652 ]
15753 );
15854 }
15955
16056 /**
161- * @return array<class-string>
57+ * @return array<class-string<Node> >
16258 */
16359 public function getNodeTypes (): array
16460 {
@@ -168,63 +64,11 @@ public function getNodeTypes(): array
16864 /**
16965 * @param ClassMethod $node
17066 */
171- public function refactor (Node $ node ): ?ClassMethod
67+ public function refactor (Node $ node ): ?Node
17268 {
173- $ scope = ScopeFetcher::fetch ($ node );
174- if (! $ scope ->isInClass ()) {
175- return null ;
176- }
177-
178- $ classReflection = $ scope ->getClassReflection ();
179- if (! $ classReflection ->is (BehatClassName::CONTEXT )) {
180- return null ;
181- }
182-
183- if (! $ this ->reflectionProvider ->hasClass (WebmozartClassName::ASSERT )) {
184- return null ;
185- }
186-
187- $ hasChanged = false ;
188-
189- $ this ->traverseNodesWithCallable ($ node , function (Node $ node ) use (&$ hasChanged ): ?StaticCall {
190- if (! $ node instanceof StaticCall) {
191- return null ;
192- }
193-
194- if (! $ this ->isName ($ node ->class , PHPUnitClassName::ASSERT )) {
195- return null ;
196- }
197-
198- $ phpunitMethodName = $ this ->getName ($ node ->name );
199- if ($ phpunitMethodName === null ) {
200- return null ;
201- }
202-
203- // changed method name
204- $ webmozartMethodName = self ::PHPUNIT_TO_WEBMOZART_METHODS [$ phpunitMethodName ] ?? null ;
205- if ($ webmozartMethodName === null ) {
206- return null ;
207- }
208-
209- if (in_array ($ phpunitMethodName , self ::FLIPPED_ARGS , true ) && count ($ node ->args ) >= 2 ) {
210- // flip first 2 args
211- $ temp = $ node ->args [0 ];
212- $ node ->args [0 ] = $ node ->args [1 ];
213- $ node ->args [1 ] = $ temp ;
214- }
215-
216- $ node ->class = new FullyQualified (WebmozartClassName::ASSERT );
217- $ node ->name = new Identifier ($ webmozartMethodName );
218-
219- $ hasChanged = true ;
220-
221- return $ node ;
222- });
223-
224- if (! $ hasChanged ) {
225- return null ;
226- }
227-
228- return $ node ;
69+ throw new ShouldNotHappenException (sprintf (
70+ '"%s" is deprecated as too narrow. Implement it as a custom rule instead. ' ,
71+ self ::class,
72+ ));
22973 }
23074}
0 commit comments