Skip to content

Commit a3fe60d

Browse files
authored
Merge pull request #128 from rvdbogerd/add-nullable-strict-types-for-native-arguments
Add nullable strict types for native and simple arguments
2 parents 4d69276 + e491194 commit a3fe60d

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/Php/ClassGenerator.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717

1818
class ClassGenerator
1919
{
20+
private $strictTypes;
21+
22+
public function __construct(bool $strictTypes = false)
23+
{
24+
$this->strictTypes = $strictTypes;
25+
}
26+
2027
private function handleBody(Generator\ClassGenerator $class, PHPClass $type)
2128
{
2229
foreach ($type->getProperties() as $prop) {
@@ -144,19 +151,29 @@ private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $
144151
} elseif ($type) {
145152
if ($type->isNativeType()) {
146153
$patramTag->setTypes($type->getPhpType());
154+
if ($this->strictTypes) {
155+
$parameter->setType($type->getPhpType());
156+
}
147157
} elseif ($p = $type->isSimpleType()) {
148158
if (($t = $p->getType()) && !$t->isNativeType()) {
149159
$patramTag->setTypes($t->getPhpType());
150160
$parameter->setType($t->getPhpType());
151161
} elseif ($t) {
152162
$patramTag->setTypes($t->getPhpType());
163+
if ($this->strictTypes) {
164+
$parameter->setType($t->getPhpType());
165+
}
153166
}
154167
} else {
155168
$patramTag->setTypes($type->getPhpType());
156169
$parameter->setType(($prop->getNullable() ? '?' : '') . $type->getPhpType());
157170
}
158171
}
159172

173+
if ($this->strictTypes && $prop->getDefault() === null) {
174+
$parameter->setDefaultValue(null);
175+
}
176+
160177
if ($prop->getNullable() && $parameter->getType()) {
161178
$parameter->setDefaultValue(null);
162179
}

tests/AbstractGenerator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ abstract class AbstractGenerator
1919
{
2020
protected $targetNs = [];
2121
protected $aliases = [];
22+
protected $strictTypes;
2223

2324
protected $phpDir;
2425
protected $jmsDir;
@@ -28,12 +29,14 @@ abstract class AbstractGenerator
2829

2930
private $loader;
3031

31-
public function __construct(array $targetNs, array $aliases = [], $tmp = null)
32+
33+
public function __construct(array $targetNs, array $aliases = [], $tmp = null, bool $strictTypes = false)
3234
{
3335
$tmp = $tmp ?: sys_get_temp_dir();
3436

3537
$this->targetNs = $targetNs;
3638
$this->aliases = $aliases;
39+
$this->strictTypes = $strictTypes;
3740

3841
$this->phpDir = "$tmp/php";
3942
$this->jmsDir = "$tmp/jms";
@@ -146,7 +149,7 @@ protected function writePHP(array $items)
146149
$pathGenerator = new PhpPsr4PathGenerator($paths);
147150

148151
$classWriter = new PHPClassWriter($pathGenerator);
149-
$writer = new PHPWriter($classWriter, new ClassGenerator());
152+
$writer = new PHPWriter($classWriter, new ClassGenerator($this->strictTypes));
150153
$writer->write($items);
151154
}
152155

0 commit comments

Comments
 (0)