@@ -95,7 +95,7 @@ public function extractFunctionBody(string $name): ?string
9595 private function getReformattedContents (array $ nodes , int $ level ): string
9696 {
9797 $ body = $ this ->getNodeContents (...$ nodes );
98- $ body = $ this ->performReplacements ($ body , $ this ->prepareReplacements ($ nodes ));
98+ $ body = $ this ->performReplacements ($ body , $ this ->prepareReplacements ($ nodes, $ level ));
9999 return Helpers::unindent ($ body , $ level );
100100 }
101101
@@ -104,11 +104,12 @@ private function getReformattedContents(array $nodes, int $level): string
104104 * @param Node[] $nodes
105105 * @return array<array{int, int, string}>
106106 */
107- private function prepareReplacements (array $ nodes ): array
107+ private function prepareReplacements (array $ nodes, int $ level ): array
108108 {
109109 $ start = $ this ->getNodeStartPos ($ nodes [0 ]);
110110 $ replacements = [];
111- (new NodeFinder )->find ($ nodes , function (Node $ node ) use (&$ replacements , $ start ) {
111+ $ indent = "\n" . str_repeat ("\t" , $ level );
112+ (new NodeFinder )->find ($ nodes , function (Node $ node ) use (&$ replacements , $ start , $ level , $ indent ) {
112113 if ($ node instanceof Node \Name \FullyQualified) {
113114 if ($ node ->getAttribute ('originalName ' ) instanceof Node \Name) {
114115 $ of = match (true ) {
@@ -122,30 +123,64 @@ private function prepareReplacements(array $nodes): array
122123 Helpers::tagName ($ node ->toCodeString (), $ of ),
123124 ];
124125 }
125- } elseif ($ node instanceof Node \Scalar \String_ || $ node instanceof Node \Scalar \EncapsedStringPart) {
126- // multi-line strings => singleline
127- $ token = $ this ->getNodeContents ($ node );
128- if (str_contains ($ token , "\n" )) {
129- $ quote = $ node instanceof Node \Scalar \String_ ? '" ' : '' ;
130- $ replacements [] = [
131- $ node ->getStartFilePos () - $ start ,
132- $ node ->getEndFilePos () - $ start ,
133- $ quote . addcslashes ($ node ->value , "\x00.. \x1F" ) . $ quote ,
134- ];
126+
127+ } elseif (
128+ $ node instanceof Node \Scalar \String_
129+ && in_array ($ node ->getAttribute ('kind ' ), [Node \Scalar \String_::KIND_SINGLE_QUOTED , Node \Scalar \String_::KIND_DOUBLE_QUOTED ], true )
130+ && str_contains ($ node ->getAttribute ('rawValue ' ), "\n" )
131+ ) { // multi-line strings -> single line
132+ $ replacements [] = [
133+ $ node ->getStartFilePos () - $ start ,
134+ $ node ->getEndFilePos () - $ start ,
135+ '" ' . addcslashes ($ node ->value , "\x00.. \x1F" ) . '" ' ,
136+ ];
137+
138+ } elseif (
139+ $ node instanceof Node \Scalar \String_
140+ && in_array ($ node ->getAttribute ('kind ' ), [Node \Scalar \String_::KIND_NOWDOC , Node \Scalar \String_::KIND_HEREDOC ], true )
141+ && Helpers::unindent ($ node ->getAttribute ('docIndentation ' ), $ level ) === $ node ->getAttribute ('docIndentation ' )
142+ ) { // fix indentation of NOWDOW/HEREDOC
143+ $ replacements [] = [
144+ $ node ->getStartFilePos () - $ start ,
145+ $ node ->getEndFilePos () - $ start ,
146+ str_replace ("\n" , $ indent , $ this ->getNodeContents ($ node )),
147+ ];
148+
149+ } elseif (
150+ $ node instanceof Node \Scalar \Encapsed
151+ && $ node ->getAttribute ('kind ' ) === Node \Scalar \String_::KIND_DOUBLE_QUOTED
152+ ) { // multi-line strings -> single line
153+ foreach ($ node ->parts as $ part ) {
154+ if ($ part instanceof Node \Scalar \EncapsedStringPart) {
155+ $ replacements [] = [
156+ $ part ->getStartFilePos () - $ start ,
157+ $ part ->getEndFilePos () - $ start ,
158+ addcslashes ($ part ->value , "\x00.. \x1F\"" ),
159+ ];
160+ }
135161 }
136- } elseif ($ node instanceof Node \Scalar \Encapsed) {
137- // HEREDOC => "string"
138- if ($ node ->getAttribute ('kind ' ) === Node \Scalar \String_::KIND_HEREDOC ) {
139- $ replacements [] = [
140- $ node ->getStartFilePos () - $ start ,
141- $ node ->parts [0 ]->getStartFilePos () - $ start - 1 ,
142- '" ' ,
143- ];
144- $ replacements [] = [
145- end ($ node ->parts )->getEndFilePos () - $ start + 1 ,
146- $ node ->getEndFilePos () - $ start ,
147- '" ' ,
148- ];
162+ } elseif (
163+ $ node instanceof Node \Scalar \Encapsed && $ node ->getAttribute ('kind ' ) === Node \Scalar \String_::KIND_HEREDOC
164+ && Helpers::unindent ($ node ->getAttribute ('docIndentation ' ), $ level ) === $ node ->getAttribute ('docIndentation ' )
165+ ) { // fix indentation of HEREDOC
166+ $ replacements [] = [
167+ $ tmp = $ node ->getStartFilePos () - $ start + strlen ($ node ->getAttribute ('docLabel ' )) + 3 , // <<<
168+ $ tmp ,
169+ $ indent ,
170+ ];
171+ $ replacements [] = [
172+ $ tmp = $ node ->getEndFilePos () - $ start - strlen ($ node ->getAttribute ('docLabel ' )),
173+ $ tmp ,
174+ $ indent ,
175+ ];
176+ foreach ($ node ->parts as $ part ) {
177+ if ($ part instanceof Node \Scalar \EncapsedStringPart) {
178+ $ replacements [] = [
179+ $ part ->getStartFilePos () - $ start ,
180+ $ part ->getEndFilePos () - $ start ,
181+ str_replace ("\n" , $ indent , $ this ->getNodeContents ($ part )),
182+ ];
183+ }
149184 }
150185 }
151186 });
0 commit comments