Skip to content

Commit 4cf1dd1

Browse files
committed
type '?xyz' sets nullable flag
1 parent 8c3e402 commit 4cf1dd1

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed

src/PhpGenerator/Parameter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public function isReference(): bool
5656
/** @return static */
5757
public function setType(?string $type): self
5858
{
59+
if ($type && $type[0] === '?') {
60+
$type = substr($type, 1);
61+
$this->nullable = true;
62+
}
5963
$this->type = $type;
6064
return $this;
6165
}

src/PhpGenerator/Property.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ public function isStatic(): bool
7171

7272

7373
/** @return static */
74-
public function setType(?string $val): self
74+
public function setType(?string $type): self
7575
{
76-
$this->type = $val;
76+
if ($type && $type[0] === '?') {
77+
$type = substr($type, 1);
78+
$this->nullable = true;
79+
}
80+
$this->type = $type;
7781
return $this;
7882
}
7983

src/PhpGenerator/Traits/FunctionLike.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,13 @@ public function isVariadic(): bool
125125

126126

127127
/** @return static */
128-
public function setReturnType(?string $val): self
128+
public function setReturnType(?string $type): self
129129
{
130-
$this->returnType = $val;
130+
if ($type && $type[0] === '?') {
131+
$type = substr($type, 1);
132+
$this->returnNullable = true;
133+
}
134+
$this->returnType = $type;
131135
return $this;
132136
}
133137

tests/PhpGenerator/PhpNamespace.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ $method->addParameter('a')->setType('Bar\C')->addAttribute('Bar\\D');
8181
$method->addParameter('b')->setType('self');
8282
$method->addParameter('c')->setType('parent');
8383
$method->addParameter('d')->setType('array');
84-
$method->addParameter('e')->setType('callable');
84+
$method->addParameter('e')->setType('?callable');
8585
$method->addParameter('f')->setType('Bar\C|string');
8686

8787
sameFile(__DIR__ . '/expected/PhpNamespace.expect', (string) $namespace);

tests/PhpGenerator/expected/PhpNamespace.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class A implements A, C
1313
self $b,
1414
parent $c,
1515
array $d,
16-
callable $e,
16+
?callable $e,
1717
C|string $f,
1818
): static|A {
1919
}

0 commit comments

Comments
 (0)