@@ -76,10 +76,10 @@ public function __construct(
7676 ?NodeFinder $ nodeFinder = null
7777 ) {
7878 $ this ->lexer = $ lexer ?? new Emulative (
79- [
79+ [
8080 'usedAttributes ' => ['comments ' , 'startLine ' , 'endLine ' , 'startTokenPos ' , 'endTokenPos ' ],
8181 ]
82- );
82+ );
8383
8484 $ this ->parser = $ parser ?? new Php7 ($ this ->lexer );
8585
@@ -111,10 +111,13 @@ public function convert(string $code, array $functionCallMap): string
111111 $ overridePlaceholders = [];
112112
113113 $ newStmts = $ this ->traverser ->traverse ($ oldStmts );
114+
115+ // Find function calls.
114116 $ funcCalls = $ this ->nodeFinder ->findInstanceOf ($ newStmts , FuncCall::class);
115117 foreach ($ funcCalls as $ funcCall ) {
116118 /** @var FuncCall $funcCall */
117119 if (!$ funcCall ->name ->hasAttribute (self ::ATTR_RESOLVED_NAME )) {
120+ // This function call has no resolved fully qualified name.
118121 continue ;
119122 }
120123
@@ -123,18 +126,28 @@ public function convert(string $code, array $functionCallMap): string
123126
124127 $ resolvedNameCode = $ resolvedName ->toCodeString ();
125128 if (isset ($ functionCallMap [$ resolvedNameCode ])) {
126- $ k = uniqid (md5 ($ resolvedNameCode ), true );
127- $ overridePlaceholders [$ k ] = $ functionCallMap [$ resolvedNameCode ];
129+ // There is a function call map > Create a unique key.
130+ $ key = uniqid (md5 ($ resolvedNameCode ), true );
131+
132+ // Put the key into the overridePlaceholders array as at the end we need to
133+ // replace those keys with the corresponding target function call.
134+ $ overridePlaceholders [$ key ] = $ functionCallMap [$ resolvedNameCode ];
128135
129- $ funcCall ->name = new FullyQualified ($ k );
136+ // Replace the name to be the fully qualified name, i.e. the given unique key
137+ // (we will replace that at the end).
138+ $ funcCall ->name = new FullyQualified ($ key );
130139 }
131140 }
132141
142+ // Print the source code.
133143 $ code = $ this ->printer ->printFormatPreserving ($ newStmts , $ oldStmts , $ oldTokens );
144+
145+ // Return the source code if there are no override placeholders.
134146 if (empty ($ overridePlaceholders )) {
135147 return $ code ;
136148 }
137149
150+ // Replace all override placeholders by their target function call.
138151 return str_replace (array_keys ($ overridePlaceholders ), array_values ($ overridePlaceholders ), $ code );
139152 }
140153}
0 commit comments