diff --git a/src/Php/ClassGenerator.php b/src/Php/ClassGenerator.php index bf06da5..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,12 +151,18 @@ private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $ } elseif ($type) { if ($type->isNativeType()) { $patramTag->setTypes($type->getPhpType()); + 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()); + if ($this->strictTypes) { + $parameter->setType($t->getPhpType()); + } } } else { $patramTag->setTypes($type->getPhpType()); @@ -157,6 +170,10 @@ private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $ } } + if ($this->strictTypes && $prop->getDefault() === null) { + $parameter->setDefaultValue(null); + } + if ($prop->getNullable() && $parameter->getType()) { $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); }