Skip to content

Commit fffd706

Browse files
committed
return whole PhpFile from ClassGenerator
1 parent 1b1b7f2 commit fffd706

27 files changed

+116
-18
lines changed

src/ClassGenerator.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
final class ClassGenerator
1414
{
1515

16-
public function generateClass(ClassDefinition $definition): Code\PhpNamespace
16+
public function generateClass(ClassDefinition $definition): Code\PhpFile
1717
{
1818
$draft = ClassInNamespace::fromDefinition($definition);
1919
$namespace = $draft->getNamespace();
@@ -51,7 +51,11 @@ public function generateClass(ClassDefinition $definition): Code\PhpNamespace
5151
}
5252

5353

54-
return $namespace;
54+
$file = new Code\PhpFile();
55+
$file->setStrictTypes();
56+
$file->addNamespace($namespace);
57+
58+
return $file;
5559
}
5660

5761

src/Console/GenerateClassCommand.php

+12-14
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,18 @@ private function processFile(string $definitionFile, InputInterface $input, Outp
8686

8787
private function doGeneration(ClassDefinition $definition, string $definitionFile, InputInterface $input, OutputInterface $output, bool $readonly): void {
8888
$classGenerator = new ClassGenerator();
89-
$generatedClass = $classGenerator->generateClass($definition);
90-
91-
$code = '<?php declare(strict_types = 1);'
92-
. "\n\n"
93-
. \sprintf(
94-
\implode("\n", [
95-
'/**',
96-
' * Do not edit. This is generated file. Modify definition "%s" instead.',
97-
' */',
98-
]),
99-
\pathinfo($definitionFile, \PATHINFO_BASENAME)
100-
)
101-
. "\n\n"
102-
. $generatedClass;
89+
$generatedFile = $classGenerator->generateClass($definition);
90+
91+
$generatedFile->addComment(\sprintf(
92+
\implode("\n", [
93+
'/**',
94+
' * Do not edit. This is generated file. Modify definition "%s" instead.',
95+
' */',
96+
]),
97+
\pathinfo($definitionFile, \PATHINFO_BASENAME)
98+
));
99+
100+
$code = (string) $generatedFile;
103101

104102
$targetPath = Path::join(
105103
Path::getDirectory($definitionFile),

tests/KeepAnnotatedMethodsDecorator/Stub/StubKeepMethod.overwritten.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace Grifart\ClassScaffolder\Test\KeepAnnotatedMethodsDecorator\Stub;
26

37
use Grifart\ClassScaffolder\Definition\ClassDefinition;

tests/KeepAnnotatedMethodsDecorator/Stub/StubKeepMethod.preserved.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace Grifart\ClassScaffolder\Test\KeepAnnotatedMethodsDecorator\Stub;
26

37
use Grifart\ClassScaffolder\Definition\ClassDefinition;

tests/KeepMethodDecorator/Stub/StubKeepMethod.overwritten.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace Grifart\ClassScaffolder\Test\KeepMethodDecorator\Stub;
26

37
use Grifart\ClassScaffolder\Definition\ClassDefinition;

tests/KeepMethodDecorator/Stub/StubKeepMethod.preserved.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace Grifart\ClassScaffolder\Test\KeepMethodDecorator\Stub;
26

37
use Grifart\ClassScaffolder\Definition\ClassDefinition;

tests/KeepUseStatementsDecorator/Stub/StubKeepUses.fromCurrent.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace Grifart\ClassScaffolder\Test\KeepUseStatementsDecorator\Stub;
26

37
use Grifart\ClassScaffolder\Definition\ClassDefinitionBuilder;

tests/KeepUseStatementsDecorator/Stub/StubKeepUses.noCurrent.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace Grifart\ClassScaffolder\Test\KeepUseStatementsDecorator\Stub;
26

37
use Grifart\ClassScaffolder\Definition\Field;

tests/basic-test/classGenerator.1-simple.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS

tests/basic-test/classGenerator.10-promoted-properties.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS

tests/basic-test/classGenerator.11-union-with-generics.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS
48
{
5-
private GenericClass|int|callable|string|null $union;
9+
private GenericClass|int|string|null $union;
610
}

tests/basic-test/classGenerator.12-shape.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS

tests/basic-test/classGenerator.13-intersection.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
use Countable;

tests/basic-test/classGenerator.14-readonly.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS

tests/basic-test/classGenerator.15-builder-bc-api.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
use Iterator;

tests/basic-test/classGenerator.16-immutable-setters.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS

tests/basic-test/classGenerator.17-named-constructor.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS

tests/basic-test/classGenerator.2-with-iterator.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
use Iterator;

tests/basic-test/classGenerator.3-with-field.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS

tests/basic-test/classGenerator.4-with-field-nullable.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS

tests/basic-test/classGenerator.5-with-list.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS

tests/basic-test/classGenerator.6-with-complex-collection.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
use Grifart\ClassScaffolder\Definition\ClassDefinition;

tests/basic-test/classGenerator.7-setters.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS

tests/basic-test/classGenerator.8-generics.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS

tests/basic-test/classGenerator.9-cross-reference.phps

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
15
namespace NS;
26

37
final class CLS

tests/basic-test/classGenerator.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ require_once __DIR__ . '/../bootstrap.php';
116116
yield [
117117
'classGenerator.11-union.phps',
118118
(new ClassDefinition('NS\\CLS'))
119-
->withField('union', Types\union(Types\classType('NS\GenericClass'), 'int', 'callable', 'string', 'null'))
119+
->withField('union', Types\union(Types\classType('NS\GenericClass'), 'int', 'string', 'null'))
120120
->with(properties()),
121121
];
122122

0 commit comments

Comments
 (0)