From aeaebb61aa58b18d0590a4ed9e3d3f7c30fc7dea Mon Sep 17 00:00:00 2001 From: Andrej Rypo Date: Mon, 23 Mar 2020 14:54:25 +0100 Subject: [PATCH] Allow disabling strict types. --- src/DI/Compiler.php | 16 +++++++++++++++- src/DI/PhpGenerator.php | 19 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/DI/Compiler.php b/src/DI/Compiler.php index 8f1bac2db..4e196ca14 100644 --- a/src/DI/Compiler.php +++ b/src/DI/Compiler.php @@ -46,6 +46,9 @@ class Compiler /** @var string */ private $className = 'Container'; + /** @var bool */ + private $strictTypes = true; + public function __construct(ContainerBuilder $builder = null) { @@ -100,6 +103,17 @@ public function setClassName(string $className) } + /** + * Enables/disables declare(strict_types=1) in generated code. + * @return static + */ + public function setStrictTypes(bool $on) + { + $this->strictTypes = $on; + return $this; + } + + /** * Adds new configuration. * @return static @@ -281,7 +295,7 @@ public function generateCode(): string $this->builder->complete(); - $generator = new PhpGenerator($this->builder); + $generator = new PhpGenerator($this->builder, $this->strictTypes); $class = $generator->generate($this->className); $this->dependencies->add($this->builder->getDependencies()); diff --git a/src/DI/PhpGenerator.php b/src/DI/PhpGenerator.php index 9b4ff9ef2..8b820e3ab 100644 --- a/src/DI/PhpGenerator.php +++ b/src/DI/PhpGenerator.php @@ -29,10 +29,25 @@ class PhpGenerator /** @var string */ private $className; + /** @var bool */ + private $strictTypes = true; - public function __construct(ContainerBuilder $builder) + + public function __construct(ContainerBuilder $builder, bool $strictTypes = true) { $this->builder = $builder; + $this->strictTypes = $strictTypes; + } + + + /** + * Enables/disables declare(strict_types=1) in generated code. + * @return static + */ + public function setStrictTypes(bool $on) + { + $this->strictTypes = $on; + return $this; } @@ -76,7 +91,7 @@ public function toString(Php\ClassType $class): string { return '/** @noinspection PhpParamsInspection,PhpMethodMayBeStaticInspection */ -declare(strict_types=1); +' . ($this->strictTypes ? 'declare(strict_types=1);' : '') . ' ' . $class->__toString(); }