@@ -76,6 +76,7 @@ public function extractMethodBodies(string $className): array
76
76
77
77
public function extractFunctionBody (string $ name ): ?string
78
78
{
79
+ /** @var Node\Stmt\Function_ $functionNode */
79
80
$ functionNode = (new NodeFinder )->findFirst ($ this ->statements , function (Node $ node ) use ($ name ) {
80
81
return $ node instanceof Node \Stmt \Function_ && $ node ->namespacedName ->toString () === $ name ;
81
82
});
@@ -87,9 +88,8 @@ public function extractFunctionBody(string $name): ?string
87
88
/** @param Node[] $statements */
88
89
private function getReformattedBody (array $ statements , int $ level ): string
89
90
{
90
- $ replacements = $ this ->prepareReplacements ($ statements );
91
91
$ body = $ this ->getNodeContents (...$ statements );
92
- $ body = $ this ->performReplacements ($ body , $ replacements );
92
+ $ body = $ this ->performReplacements ($ body , $ this -> prepareReplacements ( $ statements ) );
93
93
return Helpers::unindent ($ body , $ level );
94
94
}
95
95
@@ -98,55 +98,44 @@ private function prepareReplacements(array $statements): array
98
98
{
99
99
$ start = $ statements [0 ]->getStartFilePos ();
100
100
$ replacements = [];
101
- $ nodeFinder = new NodeFinder ;
102
-
103
- // name-nodes => resolved fully-qualified name
104
- foreach ($ nodeFinder ->findInstanceOf ($ statements , Node \Name \FullyQualified::class) as $ node ) {
105
- if ($ node ->hasAttribute ('originalName ' )
106
- && $ node ->getAttribute ('originalName ' ) instanceof Node \Name
107
- ) {
108
- $ replacements [] = [
109
- $ node ->getStartFilePos () - $ start ,
110
- $ node ->getEndFilePos () - $ start ,
111
- $ node ->toCodeString (),
112
- ];
113
- }
114
- }
101
+ (new NodeFinder )->find ($ statements , function (Node $ node ) use (&$ replacements , $ start ) {
102
+ if ($ node instanceof Node \Name \FullyQualified) {
103
+ if ($ node ->getAttribute ('originalName ' ) instanceof Node \Name) {
104
+ $ replacements [] = [
105
+ $ node ->getStartFilePos () - $ start ,
106
+ $ node ->getEndFilePos () - $ start ,
107
+ $ node ->toCodeString (),
108
+ ];
109
+ }
115
110
116
- // multi-line strings => singleline
117
- foreach (array_merge (
118
- $ nodeFinder ->findInstanceOf ($ statements , Node \Scalar \String_::class),
119
- $ nodeFinder ->findInstanceOf ($ statements , Node \Scalar \EncapsedStringPart::class)
120
- ) as $ node ) {
121
- /** @var Node\Scalar\String_|Node\Scalar\EncapsedStringPart $node */
122
- $ token = $ this ->getNodeContents ($ node );
123
- if (strpos ($ token , "\n" ) !== false ) {
124
- $ quote = $ node instanceof Node \Scalar \String_ ? '" ' : '' ;
125
- $ replacements [] = [
126
- $ node ->getStartFilePos () - $ start ,
127
- $ node ->getEndFilePos () - $ start ,
128
- $ quote . addcslashes ($ node ->value , "\x00.. \x1F" ) . $ quote ,
129
- ];
130
- }
131
- }
111
+ } elseif ($ node instanceof Node \Scalar \String_ || $ node instanceof Node \Scalar \EncapsedStringPart) {
112
+ // multi-line strings => singleline
113
+ $ token = $ this ->getNodeContents ($ node );
114
+ if (strpos ($ token , "\n" ) !== false ) {
115
+ $ quote = $ node instanceof Node \Scalar \String_ ? '" ' : '' ;
116
+ $ replacements [] = [
117
+ $ node ->getStartFilePos () - $ start ,
118
+ $ node ->getEndFilePos () - $ start ,
119
+ $ quote . addcslashes ($ node ->value , "\x00.. \x1F" ) . $ quote ,
120
+ ];
121
+ }
132
122
133
- // HEREDOC => "string"
134
- foreach ( $ nodeFinder -> findInstanceOf ( $ statements , Node \ Scalar \Encapsed::class) as $ node ) {
135
- /** @var Node\Scalar\Encapsed $node */
136
- if ( $ node -> getAttribute ( ' kind ' ) === Node \ Scalar \String_:: KIND_HEREDOC ) {
137
- $ replacements [] = [
138
- $ node ->getStartFilePos () - $ start ,
139
- $ node -> parts [ 0 ]-> getStartFilePos () - $ start - 1 ,
140
- ' " ' ,
141
- ];
142
- $ replacements [] = [
143
- end ( $ node ->parts )-> getEndFilePos () - $ start + 1 ,
144
- $ node -> getEndFilePos () - $ start ,
145
- ' " ' ,
146
- ];
123
+ } elseif ( $ node instanceof Node \ Scalar \Encapsed) {
124
+ // HEREDOC => "string"
125
+ if ( $ node -> getAttribute ( ' kind ' ) === Node \Scalar \String_:: KIND_HEREDOC ) {
126
+ $ replacements [] = [
127
+ $ node -> getStartFilePos () - $ start ,
128
+ $ node ->parts [ 0 ]-> getStartFilePos () - $ start - 1 ,
129
+ ' " ' ,
130
+ ];
131
+ $ replacements [] = [
132
+ end ( $ node -> parts )-> getEndFilePos () - $ start + 1 ,
133
+ $ node ->getEndFilePos () - $ start ,
134
+ ' " ' ,
135
+ ];
136
+ }
147
137
}
148
- }
149
-
138
+ });
150
139
return $ replacements ;
151
140
}
152
141
0 commit comments