1414namespace RegexParser \Tests \Unit \Bridge \Rector ;
1515
1616use PhpParser \Node \Expr \FuncCall ;
17+ use PhpParser \Node \Scalar \String_ ;
1718use PhpParser \Node \Stmt \ClassConst ;
1819use PhpParser \ParserFactory ;
1920use PHPUnit \Framework \TestCase ;
@@ -30,15 +31,20 @@ public function test_refactors_configured_function_call(): void
3031 $ this ->initializeDependencies ($ rector );
3132
3233 $ parser = new ParserFactory ()->createForNewestSupportedVersion ();
33- $ stmts = $ parser ->parse ('<?php my_func("/[a-zA-Z0-9_]+/", $s); ' );
34+ $ stmts = array_values ( $ parser ->parse ('<?php my_func("/[a-zA-Z0-9_]+/", $s); ' ) ?? [] );
3435 $ funcCall = $ this ->findNode ($ stmts , FuncCall::class);
3536
36- $ this ->assertInstanceOf (FuncCall::class, $ funcCall );
37+ if (!$ funcCall instanceof FuncCall) {
38+ self ::fail ('FuncCall not found ' );
39+ }
3740 $ modified = $ rector ->refactor ($ funcCall );
3841
3942 $ this ->assertNotInstanceOf (\PhpParser \Node::class, $ modified );
40- $ this ->assertInstanceOf (\PhpParser \Node \Expr \FuncCall::class, $ funcCall );
41- $ this ->assertSame ('/[a-zA-Z0-9_]+/ ' , $ funcCall ->getArgs ()[0 ]->value ->value );
43+ $ patternArg = $ funcCall ->getArgs ()[0 ]->value ;
44+ if (!$ patternArg instanceof String_) {
45+ self ::fail ('Pattern argument not string literal ' );
46+ }
47+ $ this ->assertSame ('/[a-zA-Z0-9_]+/ ' , $ patternArg ->value );
4248 }
4349
4450 public function test_refactors_configured_class_constant (): void
@@ -48,13 +54,16 @@ public function test_refactors_configured_class_constant(): void
4854 $ this ->initializeDependencies ($ rector );
4955
5056 $ parser = new ParserFactory ()->createForNewestSupportedVersion ();
51- $ stmts = $ parser ->parse ('<?php class A { public const MY_REGEX = "/[a-zA-Z0-9_]+/"; } ' );
57+ $ stmts = array_values ( $ parser ->parse ('<?php class A { public const MY_REGEX = "/[a-zA-Z0-9_]+/"; } ' ) ?? [] );
5258 $ const = $ this ->findNode ($ stmts , ClassConst::class);
5359
54- $ this ->assertInstanceOf (ClassConst::class, $ const );
60+ if (!$ const instanceof ClassConst) {
61+ self ::fail ('ClassConst not found ' );
62+ }
5563 $ modified = $ rector ->refactor ($ const );
5664
5765 $ this ->assertNotInstanceOf (\PhpParser \Node::class, $ modified );
66+ $ this ->assertInstanceOf (String_::class, $ const ->consts [0 ]->value );
5867 $ this ->assertSame ('/[a-zA-Z0-9_]+/ ' , $ const ->consts [0 ]->value ->value );
5968 }
6069
@@ -65,6 +74,22 @@ public function test_refactors_configured_class_constant(): void
6574 *
6675 * @return T|null
6776 */
77+ /**
78+ * @template T of \PhpParser\Node
79+ *
80+ * @param array<int, \PhpParser\Node\Stmt> $stmts
81+ * @param class-string<T> $class
82+ *
83+ * @return T|null
84+ */
85+ /**
86+ * @template T of \PhpParser\Node
87+ *
88+ * @param array<int, \PhpParser\Node\Stmt> $stmts
89+ * @param class-string<T> $class
90+ *
91+ * @return T|null
92+ */
6893 private function findNode (array $ stmts , string $ class ): ?\PhpParser \Node
6994 {
7095 foreach ($ stmts as $ stmt ) {
@@ -80,7 +105,7 @@ private function findNode(array $stmts, string $class): ?\PhpParser\Node
80105 return $ node ;
81106 }
82107 }
83- } elseif ($ child instanceof $ class ) {
108+ } elseif ($ child instanceof \ PhpParser \Node && $ child instanceof $ class ) {
84109 return $ child ;
85110 }
86111 }
0 commit comments