Skip to content

Commit

Permalink
Detect nullable-default parameters and generate appropriate outputs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shish authored Dec 2, 2024
1 parent 26b520c commit 078219f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
12 changes: 10 additions & 2 deletions generator/src/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ public function isVariadic(): bool

public function isNullable(): bool
{
return $this->type->isNullable();
if ($this->type->isNullable()) {
return true;
}

if ($this->getDefaultValue() === "null") {
return true;
}

return $this->getDefaultValue() === "NULL";
}

/*
Expand All @@ -94,7 +102,7 @@ public function getInitializer(): string

public function hasDefaultValue(): bool
{
return isset($this->parameter->initializer);
return property_exists($this->parameter, 'initializer') && $this->parameter->initializer !== null;
}

public function getDefaultValue(): ?string
Expand Down
18 changes: 17 additions & 1 deletion generator/src/WritePhpFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,21 @@ private function displayParamsWithType(array $params): string
$optDetected = false;

foreach ($params as $param) {
$paramAsString = $param->getSignatureType();
$paramAsString = '';
$typeDetected = false;

// parameters can not have type void
if ($param->getSignatureType() !== 'void') {
$paramAsString = $param->getSignatureType();
}

if ($paramAsString !== '') {
$paramAsString .= ' ';
if ($param->isNullable() && $paramAsString[0] !== "?") {
$paramAsString = "?" . $paramAsString;
}

$typeDetected = true;
}

$paramName = $param->getParameterName();
Expand All @@ -161,6 +173,10 @@ private function displayParamsWithType(array $params): string
$paramAsString .= ' = '.$this->defaultValueToString($defaultValue);
} elseif ($optDetected && !$param->isVariadic()) {
$paramAsString .= ' = null';

if ($typeDetected && $paramAsString[0] !== "?") {
$paramAsString = "?" . $paramAsString;
}
}
$paramsAsString[] = $paramAsString;
}
Expand Down

0 comments on commit 078219f

Please sign in to comment.