55namespace Rector \Php85 \Rector \FuncCall ;
66
77use PhpParser \Node ;
8+ use PhpParser \Node \Arg ;
9+ use PhpParser \Node \Expr ;
810use PhpParser \Node \Expr \ArrayDimFetch ;
911use PhpParser \Node \Expr \FuncCall ;
1012use PhpParser \Node \Scalar \Int_ ;
@@ -65,12 +67,9 @@ public function refactor(Node $node): ?Node
6567 }
6668
6769 $ args = $ node ->getArgs ();
70+ $ firstArg = $ args [0 ];
6871
69- if (! isset ($ node ->args [0 ])) {
70- return null ;
71- }
72-
73- $ argExpr = $ args [0 ]->value ;
72+ $ argExpr = $ firstArg ->value ;
7473 $ type = $ this ->nodeTypeResolver ->getNativeType ($ argExpr );
7574
7675 if (! $ type ->isString ()->yes () && ! $ type ->isInteger ()->yes ()) {
@@ -81,30 +80,49 @@ public function refactor(Node $node): ?Node
8180 $ isInt = is_int ($ value );
8281
8382 if (! $ argExpr instanceof Int_) {
83+ return $ this ->refactorStringType ($ argExpr , $ isInt , $ args , $ node );
84+ }
8485
85- if ($ isInt ) {
86- return null ;
87- }
86+ return $ this ->refactorInt ($ value , $ isInt , $ args , $ node );
87+ }
8888
89- $ args [0 ]->value = new ArrayDimFetch ($ argExpr , new Int_ (0 ));
90- $ node ->args = $ args ;
89+ public function provideMinPhpVersion (): int
90+ {
91+ return PhpVersionFeature::DEPRECATE_ORD_WITH_MULTIBYTE_STRING ;
92+ }
9193
92- return $ node ;
94+ /**
95+ * @param Arg[] $args
96+ */
97+ private function refactorStringType (Expr $ argExpr , bool $ isInt , array $ args , FuncCall $ funcCall ): null |FuncCall
98+ {
99+ if ($ argExpr instanceof ArrayDimFetch) {
100+ return null ;
93101 }
94102
103+ if ($ isInt ) {
104+ return null ;
105+ }
106+
107+ $ args [0 ]->value = new ArrayDimFetch ($ argExpr , new Int_ (0 ));
108+ $ funcCall ->args = $ args ;
109+
110+ return $ funcCall ;
111+ }
112+
113+ /**
114+ * @param Arg[] $args
115+ */
116+ private function refactorInt (mixed $ value , bool $ isInt , array $ args , FuncCall $ funcCall ): FuncCall
117+ {
95118 $ value = (string ) $ value ;
96119 $ byte = $ value [0 ] ?? '' ;
97120
98121 $ byteValue = $ isInt ? new Int_ ((int ) $ byte ) : new String_ ($ byte );
99122
100123 $ args [0 ]->value = $ byteValue ;
101- $ node ->args = $ args ;
102-
103- return $ node ;
104- }
124+ $ funcCall ->args = $ args ;
105125
106- public function provideMinPhpVersion (): int
107- {
108- return PhpVersionFeature::DEPRECATE_ORD_WITH_MULTIBYTE_STRING ;
126+ return $ funcCall ;
109127 }
110128}
0 commit comments