Skip to content

Commit b991c7f

Browse files
shanginndg
authored andcommitted
Printer: refactoring, added printFunctionBody()
1 parent caa776c commit b991c7f

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/PhpGenerator/Printer.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public function printFunction(GlobalFunction $function, ?PhpNamespace $namespace
4646
. $function->getName();
4747
$returnType = $this->printReturnType($function);
4848
$params = $this->printParameters($function, strlen($line) + strlen($returnType) + 2); // 2 = parentheses
49-
$body = Helpers::simplifyTaggedNames($function->getBody(), $this->namespace);
50-
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
49+
$body = $this->printFunctionBody($function);
5150
$braceOnNextLine = $this->isBraceOnNextLine(str_contains($params, "\n"), (bool) $returnType);
5251

5352
return $this->printDocComment($function)
@@ -71,8 +70,7 @@ public function printClosure(Closure $closure, ?PhpNamespace $namespace = null):
7170
$useStr = strlen($tmp = implode(', ', $uses)) > $this->wrapLength && count($uses) > 1
7271
? "\n" . $this->indentation . implode(",\n" . $this->indentation, $uses) . ",\n"
7372
: $tmp;
74-
$body = Helpers::simplifyTaggedNames($closure->getBody(), $this->namespace);
75-
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
73+
$body = $this->printFunctionBody($closure);
7674

7775
return $this->printAttributes($closure->getAttributes(), inline: true)
7876
. 'function '
@@ -93,14 +91,14 @@ public function printArrowFunction(Closure $closure, ?PhpNamespace $namespace =
9391
}
9492
}
9593

96-
$body = Helpers::simplifyTaggedNames($closure->getBody(), $this->namespace);
94+
$body = $this->printFunctionBody($closure);
9795

9896
return $this->printAttributes($closure->getAttributes())
9997
. 'fn'
10098
. ($closure->getReturnReference() ? '&' : '')
10199
. $this->printParameters($closure)
102100
. $this->printReturnType($closure)
103-
. ' => ' . trim(Strings::normalize($body)) . ';';
101+
. ' => ' . rtrim($body, "\n") . ';';
104102
}
105103

106104

@@ -117,8 +115,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
117115
. $method->getName();
118116
$returnType = $this->printReturnType($method);
119117
$params = $this->printParameters($method, strlen($line) + strlen($returnType) + strlen($this->indentation) + 2);
120-
$body = Helpers::simplifyTaggedNames($method->getBody(), $this->namespace);
121-
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
118+
$body = $this->printFunctionBody($method);
122119
$braceOnNextLine = $this->isBraceOnNextLine(str_contains($params, "\n"), (bool) $returnType);
123120

124121
return $this->printDocComment($method)
@@ -132,6 +129,14 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
132129
}
133130

134131

132+
private function printFunctionBody(Closure|GlobalFunction|Method $function): string
133+
{
134+
$code = Helpers::simplifyTaggedNames($function->getBody(), $this->namespace);
135+
$code = Strings::normalize($code);
136+
return ltrim(rtrim($code) . "\n");
137+
}
138+
139+
135140
public function printClass(
136141
ClassType|InterfaceType|TraitType|EnumType $class,
137142
?PhpNamespace $namespace = null,

0 commit comments

Comments
 (0)