diff --git a/src/Generator/ClassGenerator.php b/src/Generator/ClassGenerator.php index 3ac3dd23..f00d66ee 100644 --- a/src/Generator/ClassGenerator.php +++ b/src/Generator/ClassGenerator.php @@ -2,6 +2,7 @@ namespace Laminas\Code\Generator; +use Laminas\Code\Generator\Exception\InvalidArgumentException; use Laminas\Code\Reflection\ClassReflection; use function array_diff; @@ -847,6 +848,14 @@ public function addMethodFromGenerator(MethodGenerator $method) )); } + if ('__construct' !== $methodName) { + foreach ($method->getParameters() as $parameter) { + if ($parameter instanceof InInitializerGenerator) { + throw new InvalidArgumentException('In Initializer can only be added to constructor.'); + } + } + } + $this->methods[strtolower($methodName)] = $method; return $this; } diff --git a/src/Generator/InInitializerGenerator.php b/src/Generator/InInitializerGenerator.php new file mode 100644 index 00000000..80c50c33 --- /dev/null +++ b/src/Generator/InInitializerGenerator.php @@ -0,0 +1,39 @@ +visibility = $visibility; + } +} \ No newline at end of file diff --git a/src/Generator/ObjectGenerator.php b/src/Generator/ObjectGenerator.php new file mode 100644 index 00000000..cd9255b0 --- /dev/null +++ b/src/Generator/ObjectGenerator.php @@ -0,0 +1,19 @@ +generate()); } + + /** @requires PHP >= 8.1 */ + public function testGenerateClassWithInInitializer(): void + { + $classGenerator = new ClassGenerator(); + $classGenerator->setName('InInitializer'); + + $classGenerator->addMethod('__construct', [ + new InInitializerGenerator('inInitializer', 'string') + ]); + + $expectedOutput = <<generate()); + } + + /** @requires PHP >= 8.1 */ + public function testGenerateClassWithInInitializerWithDefaultValue(): void + { + $classGenerator = new ClassGenerator(); + $classGenerator->setName('InInitializerWithDefaultValue'); + + $classGenerator->addMethod('__construct', [ + new InInitializerGenerator( + 'inInitializer', + 'Foo', + InInitializerGenerator::VISIBILITY_PRIVATE, + new ObjectGenerator( + 'Foo', + [ + 'baz' => 'qux' + ], + 'bar', + ) + ) + ]); + + $expectedOutput = <<generate()); + } + + /** @requires PHP >= 8.1 */ + public function testClassFromReflectionWithInInitializer(): void + { + $className = uniqid('ClassWithInInitializer', false); + + eval('namespace ' . __NAMESPACE__ . '; class ' . $className . '{ public function __construct( public string \$inInitializer){} }'); + + $classGenerator = ClassGenerator::fromReflection(new ReflectionClass(__NAMESPACE__ . '\\' . $className)); + + $expectedOutput = <<generate()); + } + + /** @requires PHP >= 8.1 */ + public function testFailToGenerateClassWithInInitializerOnOtherMethods(): void + { + $classGenerator = new ClassGenerator(); + $classGenerator->setName('InInitializerOnOtherMethods'); + + $this->expectExceptionObject( + new InvalidArgumentException('In Initializer can only be added to constructor.') + ); + + $classGenerator->addMethod('thisIsNoConstructor', [ + new InInitializerGenerator('inInitializer', 'string') + ]); + } }