From 2c7b81657c6964a8f3f55b129d61c00151c52672 Mon Sep 17 00:00:00 2001 From: Shish Date: Mon, 17 Feb 2025 17:25:21 +0000 Subject: [PATCH] [generator] remove some redundant edge-cases Special-cases to handle 7.X-era bugs are no longer needed Evidence that these cases are redundant: removing them doesn't affect the generated code --- generator/src/Generator/WritePhpFunction.php | 8 +---- .../src/PhpStanFunctions/PhpStanType.php | 35 +++++++------------ .../PhpStanFunctions/PhpStanTypeTest.php | 6 +--- 3 files changed, 15 insertions(+), 34 deletions(-) diff --git a/generator/src/Generator/WritePhpFunction.php b/generator/src/Generator/WritePhpFunction.php index ad16bf95..d48d7688 100644 --- a/generator/src/Generator/WritePhpFunction.php +++ b/generator/src/Generator/WritePhpFunction.php @@ -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 .= ' '; @@ -158,7 +153,6 @@ private function displayParamsWithType(array $params): string $paramAsString .= '$'.$paramName; } - if ($param->hasDefaultValue() || $param->isOptionalWithNoDefault()) { $optDetected = true; } diff --git a/generator/src/PhpStanFunctions/PhpStanType.php b/generator/src/PhpStanFunctions/PhpStanType.php index be85570d..5aea1f3f 100644 --- a/generator/src/PhpStanFunctions/PhpStanType.php +++ b/generator/src/PhpStanFunctions/PhpStanType.php @@ -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; @@ -189,25 +182,23 @@ public function getSignatureType(?ErrorType $errorType = null): string $type = 'string'; } } + // filter out duplicates due to input types like "array|array" + $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|array) - 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; } /** diff --git a/generator/tests/PhpStanFunctions/PhpStanTypeTest.php b/generator/tests/PhpStanFunctions/PhpStanTypeTest.php index ccf05248..526400eb 100644 --- a/generator/tests/PhpStanFunctions/PhpStanTypeTest.php +++ b/generator/tests/PhpStanFunctions/PhpStanTypeTest.php @@ -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());