Skip to content

Commit 1ed1531

Browse files
committed
Printer: tuned generated coding style
1 parent 03eb652 commit 1ed1531

14 files changed

+51
-51
lines changed

src/PhpGenerator/Printer.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function printFunction(GlobalFunction $function, PhpNamespace $namespace
3535
. $function->getName()
3636
. $this->printParameters($function, $namespace)
3737
. $this->printReturnType($function, $namespace)
38-
. "\n{\n" . $this->indent(ltrim(rtrim($function->getBody()) . "\n")) . '}';
38+
. "\n{\n" . $this->indent(ltrim(rtrim($function->getBody()) . "\n")) . "}\n";
3939
}
4040

4141

@@ -71,11 +71,11 @@ public function printMethod(Method $method, PhpNamespace $namespace = null): str
7171
. ($params = $this->printParameters($method, $namespace))
7272
. $this->printReturnType($method, $namespace)
7373
. ($method->isAbstract() || $method->getBody() === null
74-
? ';'
74+
? ";\n"
7575
: (strpos($params, "\n") === false ? "\n" : ' ')
7676
. "{\n"
7777
. $this->indent(ltrim(rtrim($method->getBody()) . "\n"))
78-
. '}');
78+
. "}\n");
7979
}
8080

8181

@@ -86,30 +86,36 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
8686
$traits = [];
8787
foreach ($class->getTraitResolutions() as $trait => $resolutions) {
8888
$traits[] = 'use ' . $resolver($trait)
89-
. ($resolutions ? " {\n" . $this->indentation . implode(";\n" . $this->indentation, $resolutions) . ";\n}" : ';');
89+
. ($resolutions ? " {\n" . $this->indentation . implode(";\n" . $this->indentation, $resolutions) . ";\n}\n" : ";\n");
9090
}
9191

9292
$consts = [];
9393
foreach ($class->getConstants() as $const) {
9494
$consts[] = Helpers::formatDocComment((string) $const->getComment())
9595
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
96-
. 'const ' . $const->getName() . ' = ' . Helpers::dump($const->getValue()) . ';';
96+
. 'const ' . $const->getName() . ' = ' . Helpers::dump($const->getValue()) . ";\n";
9797
}
9898

9999
$properties = [];
100100
foreach ($class->getProperties() as $property) {
101101
$properties[] = Helpers::formatDocComment((string) $property->getComment())
102102
. ($property->getVisibility() ?: 'public') . ($property->isStatic() ? ' static' : '') . ' $' . $property->getName()
103103
. ($property->getValue() === null ? '' : ' = ' . Helpers::dump($property->getValue()))
104-
. ';';
104+
. ";\n";
105105
}
106106

107107
$methods = [];
108108
foreach ($class->getMethods() as $method) {
109109
$methods[] = $this->printMethod($method, $namespace);
110110
}
111111

112-
$methodSpace = str_repeat("\n", $this->linesBetweenMethods + 1);
112+
$members = array_filter([
113+
implode('', $traits),
114+
implode('', $consts),
115+
implode("\n", $properties),
116+
($methods && $properties ? str_repeat("\n", $this->linesBetweenMethods - 1) : '')
117+
. implode(str_repeat("\n", $this->linesBetweenMethods), $methods),
118+
]);
113119

114120
return Strings::normalize(
115121
Helpers::formatDocComment($class->getComment() . "\n")
@@ -119,11 +125,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
119125
. ($class->getExtends() ? 'extends ' . implode(', ', array_map($resolver, (array) $class->getExtends())) . ' ' : '')
120126
. ($class->getImplements() ? 'implements ' . implode(', ', array_map($resolver, $class->getImplements())) . ' ' : '')
121127
. ($class->getName() ? "\n" : '') . "{\n"
122-
. $this->indent(
123-
($traits ? implode("\n", $traits) . "\n\n" : '')
124-
. ($consts ? implode("\n", $consts) . "\n\n" : '')
125-
. ($properties ? implode("\n\n", $properties) . $methodSpace : '')
126-
. ($methods ? implode($methodSpace, $methods) . "\n" : ''))
128+
. ($members ? $this->indent(implode("\n", $members)) : '')
127129
. '}'
128130
) . ($class->getName() ? "\n" : '');
129131
}
@@ -155,9 +157,9 @@ public function printNamespace(PhpNamespace $namespace): string
155157
. implode("\n", $classes);
156158

157159
if ($namespace->getBracketedSyntax()) {
158-
return 'namespace' . ($name ? " $name" : '') . " {\n\n"
160+
return 'namespace' . ($name ? " $name" : '') . "\n{\n"
159161
. $this->indent($body)
160-
. "\n}\n";
162+
. "}\n";
161163

162164
} else {
163165
return ($name ? "namespace $name;\n\n" : '')
@@ -175,7 +177,8 @@ public function printFile(PhpFile $file): string
175177

176178
return Strings::normalize(
177179
"<?php\n"
178-
. ($file->getComment() ? "\n" . Helpers::formatDocComment($file->getComment() . "\n") . "\n" : '')
180+
. ($file->getComment() ? "\n" . Helpers::formatDocComment($file->getComment() . "\n") : '')
181+
. "\n"
179182
. ($file->getStrictTypes() ? "declare(strict_types=1);\n\n" : '')
180183
. implode("\n\n", $namespaces)
181184
) . "\n";

tests/PhpGenerator/GlobalFunction.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ same(
2121
*/
2222
function func(stdClass $a, $b = null)
2323
{
24-
}', (string) $function);
24+
}
25+
', (string) $function);

tests/PhpGenerator/Method.longParams.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ same(
3030
string $l
3131
) {
3232
return null;
33-
}', (string) $method);
33+
}
34+
', (string) $method);

tests/PhpGenerator/Method.returnTypes.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace
4646
'function create(): Foo
4747
{
4848
return new Foo();
49-
}', (string) $method);
49+
}
50+
', (string) $method);
5051

5152
}

tests/PhpGenerator/Method.scalarParameters.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ same(
4545
'function create(string $a, bool $b)
4646
{
4747
return null;
48-
}', (string) $method);
48+
}
49+
', (string) $method);

tests/PhpGenerator/Method.variadics.phpt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ same(
4343
'function variadic()
4444
{
4545
return 42;
46-
}', (string) $method);
46+
}
47+
', (string) $method);
4748

4849

4950
// variadic method with one parameter
@@ -56,7 +57,8 @@ same(
5657
'function variadic(...$foo)
5758
{
5859
return 42;
59-
}', (string) $method);
60+
}
61+
', (string) $method);
6062

6163

6264
// variadic method with multiple parameters
@@ -71,7 +73,8 @@ same(
7173
'function variadic($foo, $bar, ...$baz)
7274
{
7375
return 42;
74-
}', (string) $method);
76+
}
77+
', (string) $method);
7578

7679

7780
// method with typehinted variadic param
@@ -84,7 +87,8 @@ same(
8487
'function variadic(array ...$foo)
8588
{
8689
return 42;
87-
}', (string) $method);
90+
}
91+
', (string) $method);
8892

8993

9094
// method with typrhinted by-value variadic param
@@ -97,4 +101,5 @@ same(
97101
'function variadic(array &...$foo)
98102
{
99103
return 42;
100-
}', (string) $method);
104+
}
105+
', (string) $method);

tests/PhpGenerator/expected/ClassType.from.expect

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,11 @@ class Class2 extends Class1 implements Interface2
5757
class Class3
5858
{
5959
public $prop1;
60-
61-
6260
}
6361

6462
class Class4
6563
{
6664
const THE_CONSTANT = 9;
67-
6865
}
6966

7067
class Class5
@@ -88,5 +85,4 @@ class Class6
8885
{
8986
const THE_PRIVATE_CONSTANT = 9;
9087
const THE_PUBLIC_CONSTANT = 9;
91-
9288
}

tests/PhpGenerator/expected/PhpFile.bracketed.expect

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
* Hey there, I'm here to document things.
66
*/
77

8-
namespace Foo {
9-
8+
namespace Foo
9+
{
1010
class A implements A, \Bar\C
1111
{
1212
use C;
1313
use \Bar\D;
14-
1514
}
1615

1716
interface B
@@ -21,16 +20,14 @@ namespace Foo {
2120
trait C
2221
{
2322
}
24-
2523
}
2624

2725

28-
namespace Bar {
29-
26+
namespace Bar
27+
{
3028
class B extends \Foo\A implements \Foo\B
3129
{
3230
use \Foo\C;
33-
3431
}
3532

3633
interface C
@@ -40,12 +37,11 @@ namespace Bar {
4037
trait D
4138
{
4239
}
43-
4440
}
4541

4642

47-
namespace Baz {
48-
43+
namespace Baz
44+
{
4945
class E
5046
{
5147
}
@@ -57,23 +53,20 @@ namespace Baz {
5753
trait G
5854
{
5955
}
60-
6156
}
6257

6358

64-
namespace {
65-
59+
namespace
60+
{
6661
class H
6762
{
6863
}
69-
7064
}
7165

7266

73-
namespace FooBar {
74-
67+
namespace FooBar
68+
{
7569
class I
7670
{
7771
}
78-
7972
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
class A
34
{
45
}

tests/PhpGenerator/expected/PhpFile.regular.expect

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class A implements A, \Bar\C
1111
{
1212
use C;
1313
use \Bar\D;
14-
1514
}
1615

1716
interface B
@@ -28,7 +27,6 @@ namespace Bar;
2827
class B extends \Foo\A implements \Foo\B
2928
{
3029
use \Foo\C;
31-
3230
}
3331

3432
interface C

0 commit comments

Comments
 (0)