From cf7e67b4c55428fdb3c8045d89e82b4864c469c4 Mon Sep 17 00:00:00 2001 From: Robbert van den Bogerd Date: Wed, 16 Jan 2019 11:23:18 +0100 Subject: [PATCH 1/3] Use proper types and add null as default value for serializer setters --- src/Php/ClassGenerator.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Php/ClassGenerator.php b/src/Php/ClassGenerator.php index bf06da5..202bfbd 100644 --- a/src/Php/ClassGenerator.php +++ b/src/Php/ClassGenerator.php @@ -144,12 +144,15 @@ private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $ } elseif ($type) { if ($type->isNativeType()) { $patramTag->setTypes($type->getPhpType()); + $parameter->setType($type->getPhpType()); // Added by rvdb } 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 + } } else { $patramTag->setTypes($type->getPhpType()); From c83895a357c2ac2be199dff410644d59e911de19 Mon Sep 17 00:00:00 2001 From: Robbert van den Bogerd Date: Mon, 11 Jan 2021 21:25:26 +0100 Subject: [PATCH 2/3] Re-add default nullable arguments --- src/Php/ClassGenerator.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Php/ClassGenerator.php b/src/Php/ClassGenerator.php index 202bfbd..4afee34 100644 --- a/src/Php/ClassGenerator.php +++ b/src/Php/ClassGenerator.php @@ -144,14 +144,14 @@ private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $ } elseif ($type) { if ($type->isNativeType()) { $patramTag->setTypes($type->getPhpType()); - $parameter->setType($type->getPhpType()); // Added by rvdb + $parameter->setType($type->getPhpType()); // Added by rvdb: add strict typing for scalar arguments } 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 + $parameter->setType($t->getPhpType()); // Added by rvdb: add strict typing for simple arguments } } else { @@ -160,6 +160,10 @@ private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $ } } + if ($prop->getDefault() === null) { // Added by rvdb: make setter arguments nullable + $parameter->setDefaultValue(null); + } + if ($prop->getNullable() && $parameter->getType()) { $parameter->setDefaultValue(null); } From e4911944c1f33547c14e7e1239b5857a063ab581 Mon Sep 17 00:00:00 2001 From: Robbert van den Bogerd Date: Fri, 29 Jan 2021 13:49:58 +0100 Subject: [PATCH 3/3] Make strict typing for scalar arguments opt-in --- src/Php/ClassGenerator.php | 18 ++++++++++++++---- tests/AbstractGenerator.php | 7 +++++-- 2 files changed, 19 insertions(+), 6 deletions(-) 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); }