diff --git a/src/Php/ClassGenerator.php b/src/Php/ClassGenerator.php index 4afee34..1c80ccd 100644 --- a/src/Php/ClassGenerator.php +++ b/src/Php/ClassGenerator.php @@ -17,6 +17,13 @@ class ClassGenerator { + private $strictTypes; + + public function __construct(bool $strictTypes = false) + { + $this->strictTypes = $strictTypes; + } + private function handleBody(Generator\ClassGenerator $class, PHPClass $type) { foreach ($type->getProperties() as $prop) { @@ -144,15 +151,18 @@ private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $ } elseif ($type) { if ($type->isNativeType()) { $patramTag->setTypes($type->getPhpType()); - $parameter->setType($type->getPhpType()); // Added by rvdb: add strict typing for scalar arguments + if ($this->strictTypes) { + $parameter->setType($type->getPhpType()); + } } elseif ($p = $type->isSimpleType()) { if (($t = $p->getType()) && !$t->isNativeType()) { $patramTag->setTypes($t->getPhpType()); $parameter->setType($t->getPhpType()); } elseif ($t) { $patramTag->setTypes($t->getPhpType()); - $parameter->setType($t->getPhpType()); // Added by rvdb: add strict typing for simple arguments - + if ($this->strictTypes) { + $parameter->setType($t->getPhpType()); + } } } else { $patramTag->setTypes($type->getPhpType()); @@ -160,7 +170,7 @@ private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $ } } - if ($prop->getDefault() === null) { // Added by rvdb: make setter arguments nullable + if ($this->strictTypes && $prop->getDefault() === null) { $parameter->setDefaultValue(null); } diff --git a/tests/AbstractGenerator.php b/tests/AbstractGenerator.php index b2a7d2c..4182dca 100644 --- a/tests/AbstractGenerator.php +++ b/tests/AbstractGenerator.php @@ -19,6 +19,7 @@ abstract class AbstractGenerator { protected $targetNs = []; protected $aliases = []; + protected $strictTypes; protected $phpDir; protected $jmsDir; @@ -28,12 +29,14 @@ abstract class AbstractGenerator private $loader; - public function __construct(array $targetNs, array $aliases = [], $tmp = null) + + public function __construct(array $targetNs, array $aliases = [], $tmp = null, bool $strictTypes = false) { $tmp = $tmp ?: sys_get_temp_dir(); $this->targetNs = $targetNs; $this->aliases = $aliases; + $this->strictTypes = $strictTypes; $this->phpDir = "$tmp/php"; $this->jmsDir = "$tmp/jms"; @@ -146,7 +149,7 @@ protected function writePHP(array $items) $pathGenerator = new PhpPsr4PathGenerator($paths); $classWriter = new PHPClassWriter($pathGenerator); - $writer = new PHPWriter($classWriter, new ClassGenerator()); + $writer = new PHPWriter($classWriter, new ClassGenerator($this->strictTypes)); $writer->write($items); }