44namespace Rector \PHPUnit \CodeQuality \Rector \ClassMethod ;
55
66use PhpParser \Node ;
7- use PhpParser \Node \Expr \StaticCall ;
8- use PhpParser \Node \Identifier ;
9- use PhpParser \Node \Name \FullyQualified ;
107use PhpParser \Node \Stmt \ClassMethod ;
11- use PHPStan \Reflection \ReflectionProvider ;
12- use Rector \PHPStan \ScopeFetcher ;
13- use Rector \PHPUnit \Enum \BehatClassName ;
14- use Rector \PHPUnit \Enum \PHPUnitClassName ;
15- use Rector \PHPUnit \Enum \WebmozartClassName ;
8+ use Rector \Configuration \Deprecation \Contract \DeprecatedInterface ;
9+ use Rector \Exception \ShouldNotHappenException ;
1610use Rector \Rector \AbstractRector ;
1711use Symplify \RuleDocGenerator \ValueObject \CodeSample \CodeSample ;
1812use Symplify \RuleDocGenerator \ValueObject \RuleDefinition ;
1913/**
20- * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BehatPHPUnitAssertToWebmozartRector\BehatPHPUnitAssertToWebmozartRectorTest
14+ * @deprecated This rule is deprecated as too narrow, it only targets Behat context files. Implement it as a custom rule instead.
2115 */
22- final class BehatPHPUnitAssertToWebmozartRector extends AbstractRector
16+ final class BehatPHPUnitAssertToWebmozartRector extends AbstractRector implements DeprecatedInterface
2317{
24- /**
25- * @readonly
26- */
27- private ReflectionProvider $ reflectionProvider ;
28- /**
29- * @var array<string, string>
30- */
31- private const PHPUNIT_TO_WEBMOZART_METHODS = [
32- // Boolean
33- 'assertTrue ' => 'true ' ,
34- 'assertFalse ' => 'false ' ,
35- // Null / empty
36- 'assertNull ' => 'null ' ,
37- 'assertNotNull ' => 'notNull ' ,
38- 'assertEmpty ' => 'isEmpty ' ,
39- 'assertNotEmpty ' => 'notEmpty ' ,
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- // array
52- 'assertContains ' => 'oneOf ' ,
53- 'assertNotContains ' => 'notOneOf ' ,
54- // Comparison / equality
55- 'assertSame ' => 'same ' ,
56- 'assertNotSame ' => 'notSame ' ,
57- 'assertEquals ' => 'eq ' ,
58- 'assertNotEquals ' => 'notEq ' ,
59- 'assertGreaterThan ' => 'greaterThan ' ,
60- 'assertGreaterThanOrEqual ' => 'greaterThanEq ' ,
61- 'assertLessThan ' => 'lessThan ' ,
62- 'assertLessThanOrEqual ' => 'lessThanEq ' ,
63- // Strings
64- 'assertStringContainsString ' => 'contains ' ,
65- 'assertStringNotContainsString ' => 'notContains ' ,
66- 'assertStringStartsWith ' => 'startsWith ' ,
67- 'assertStringStartsNotWith ' => 'notStartsWith ' ,
68- 'assertStringEndsWith ' => 'endsWith ' ,
69- 'assertStringEndsNotWith ' => 'notEndsWith ' ,
70- 'assertMatchesRegularExpression ' => 'regex ' ,
71- 'assertDoesNotMatchRegularExpression ' => 'notRegex ' ,
72- // Bool
73- 'assertNotTrue ' => 'false ' ,
74- 'assertNotFalse ' => 'true ' ,
75- // Arrays / count
76- 'assertCount ' => 'count ' ,
77- 'assertArrayHasKey ' => 'keyExists ' ,
78- 'assertArrayNotHasKey ' => 'keyNotExists ' ,
79- // Misc / less direct
80- 'assertFileExists ' => 'fileExists ' ,
81- 'assertFileIsReadable ' => 'readable ' ,
82- 'assertDirectoryExists ' => 'directory ' ,
83- // Instance of
84- 'assertNotInstanceOf ' => 'notInstanceOf ' ,
85- ];
86- /**
87- * @var string[]
88- */
89- private const FLIPPED_ARGS = ['assertSame ' , 'assertNotSame ' , 'assertEquals ' , 'assertNotEquals ' , 'assertGreaterThan ' , 'assertGreaterThanOrEqual ' , 'assertLessThan ' , 'assertLessThanOrEqual ' , 'assertCount ' , 'assertInstanceOf ' , 'assertNotInstanceOf ' , 'assertArrayHasKey ' , 'assertArrayNotHasKey ' , 'assertStringContainsString ' , 'assertStringStartsWith ' , 'assertMatchesRegularExpression ' , 'assertDoesNotMatchRegularExpression ' ];
90- public function __construct (ReflectionProvider $ reflectionProvider )
91- {
92- $ this ->reflectionProvider = $ reflectionProvider ;
93- }
9418 public function getRuleDefinition (): RuleDefinition
9519 {
9620 return new RuleDefinition ('Change PHPUnit assert in Behat context files to Webmozart Assert, as first require a TestCase instance ' , [new CodeSample (<<<'CODE_SAMPLE'
@@ -120,7 +44,7 @@ public function someMethod()
12044)]);
12145 }
12246 /**
123- * @return array<class-string>
47+ * @return array<class-string<Node> >
12448 */
12549 public function getNodeTypes (): array
12650 {
@@ -129,50 +53,8 @@ public function getNodeTypes(): array
12953 /**
13054 * @param ClassMethod $node
13155 */
132- public function refactor (Node $ node ): ?ClassMethod
56+ public function refactor (Node $ node ): ?Node
13357 {
134- $ scope = ScopeFetcher::fetch ($ node );
135- if (!$ scope ->isInClass ()) {
136- return null ;
137- }
138- $ classReflection = $ scope ->getClassReflection ();
139- if (!$ classReflection ->is (BehatClassName::CONTEXT )) {
140- return null ;
141- }
142- if (!$ this ->reflectionProvider ->hasClass (WebmozartClassName::ASSERT )) {
143- return null ;
144- }
145- $ hasChanged = \false;
146- $ this ->traverseNodesWithCallable ($ node , function (Node $ node ) use (&$ hasChanged ): ?StaticCall {
147- if (!$ node instanceof StaticCall) {
148- return null ;
149- }
150- if (!$ this ->isName ($ node ->class , PHPUnitClassName::ASSERT )) {
151- return null ;
152- }
153- $ phpunitMethodName = $ this ->getName ($ node ->name );
154- if ($ phpunitMethodName === null ) {
155- return null ;
156- }
157- // changed method name
158- $ webmozartMethodName = self ::PHPUNIT_TO_WEBMOZART_METHODS [$ phpunitMethodName ] ?? null ;
159- if ($ webmozartMethodName === null ) {
160- return null ;
161- }
162- if (in_array ($ phpunitMethodName , self ::FLIPPED_ARGS , \true) && count ($ node ->args ) >= 2 ) {
163- // flip first 2 args
164- $ temp = $ node ->args [0 ];
165- $ node ->args [0 ] = $ node ->args [1 ];
166- $ node ->args [1 ] = $ temp ;
167- }
168- $ node ->class = new FullyQualified (WebmozartClassName::ASSERT );
169- $ node ->name = new Identifier ($ webmozartMethodName );
170- $ hasChanged = \true;
171- return $ node ;
172- });
173- if (!$ hasChanged ) {
174- return null ;
175- }
176- return $ node ;
58+ throw new ShouldNotHappenException (sprintf ('"%s" is deprecated as too narrow. Implement it as a custom rule instead. ' , self ::class));
17759 }
17860}
0 commit comments