Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[generator] remove some redundant edge-cases #628

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions generator/src/Generator/WritePhpFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,8 @@ private function displayParamsWithType(array $params): string
$optDetected = false;

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

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

if ($paramAsString !== '') {
$paramAsString .= ' ';
Expand All @@ -158,7 +153,6 @@ private function displayParamsWithType(array $params): string
$paramAsString .= '$'.$paramName;
}


if ($param->hasDefaultValue() || $param->isOptionalWithNoDefault()) {
$optDetected = true;
}
Expand Down
35 changes: 13 additions & 22 deletions generator/src/PhpStanFunctions/PhpStanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ public function __construct(string|\SimpleXMLElement $data, bool $writeOnly = fa
$data = $regs[1];
}

//weird case: null|false => null
if ($data === 'null|false') {
$this->nullable = false;
$this->falsable = true;
$this->types = ['null'];
return;
}
//first we try to parse the type string to have a list as clean as possible.
$nullable = false;
$falsable = false;
Expand Down Expand Up @@ -189,25 +182,23 @@ public function getSignatureType(?ErrorType $errorType = null): string
$type = 'string';
}
}
// filter out duplicates due to input types like "array<string>|array<int>"
$types = array_unique($types);
sort($types);

//if there are several distinct types, no typehint (we use distinct in case doc block contains several times the same type, for example array<int>|array<string>)
if (count(array_unique($types)) > 1) {
if (count($types) === 0) {
return '';
} elseif (count($types) === 1) {
$finalType = $types[0];
if ($finalType === 'bool' && !$nullable && $errorType === ErrorType::FALSY) {
// If the function only returns a boolean, since false is for
// error, true is for success. Let's replace this with a "void".
return 'void';
}
return ($nullable !== false ? '?' : '').$finalType;
} else {
return '';
}

if (\in_array('void', $types) || ($types === [] && !$nullable && !$falsable)) {
return 'void';
}


$finalType = $types[0] ?? '';
if ($finalType === 'bool' && !$nullable && $errorType === ErrorType::FALSY) {
// If the function only returns a boolean, since false is for error, true is for success.
// Let's replace this with a "void".
return 'void';
}
return ($nullable !== false ? '?' : '').$finalType;
}

/**
Expand Down
6 changes: 1 addition & 5 deletions generator/tests/PhpStanFunctions/PhpStanTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,7 @@ public function testVoid(): void
{
$param = new PhpStanType('');
$this->assertEquals('', $param->getDocBlockType());
if (PHP_VERSION_ID >= 80200) {
$this->assertEquals('void', $param->getSignatureType());
} else {
$this->assertEquals('', $param->getSignatureType());
}
$this->assertEquals('', $param->getSignatureType());

$param = new PhpStanType('void');
$this->assertEquals('void', $param->getDocBlockType());
Expand Down
Loading