Skip to content

Commit 2c7b816

Browse files
committed
[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
1 parent c87a46b commit 2c7b816

File tree

3 files changed

+15
-34
lines changed

3 files changed

+15
-34
lines changed

generator/src/Generator/WritePhpFunction.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,8 @@ private function displayParamsWithType(array $params): string
135135
$optDetected = false;
136136

137137
foreach ($params as $param) {
138-
$paramAsString = '';
139138
$typeDetected = false;
140-
141-
// parameters can not have type void
142-
if ($param->getSignatureType() !== 'void') {
143-
$paramAsString = $param->getSignatureType();
144-
}
139+
$paramAsString = $param->getSignatureType();
145140

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

161-
162156
if ($param->hasDefaultValue() || $param->isOptionalWithNoDefault()) {
163157
$optDetected = true;
164158
}

generator/src/PhpStanFunctions/PhpStanType.php

+13-22
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ public function __construct(string|\SimpleXMLElement $data, bool $writeOnly = fa
4242
$data = $regs[1];
4343
}
4444

45-
//weird case: null|false => null
46-
if ($data === 'null|false') {
47-
$this->nullable = false;
48-
$this->falsable = true;
49-
$this->types = ['null'];
50-
return;
51-
}
5245
//first we try to parse the type string to have a list as clean as possible.
5346
$nullable = false;
5447
$falsable = false;
@@ -189,25 +182,23 @@ public function getSignatureType(?ErrorType $errorType = null): string
189182
$type = 'string';
190183
}
191184
}
185+
// filter out duplicates due to input types like "array<string>|array<int>"
186+
$types = array_unique($types);
192187
sort($types);
193188

194-
//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>)
195-
if (count(array_unique($types)) > 1) {
189+
if (count($types) === 0) {
190+
return '';
191+
} elseif (count($types) === 1) {
192+
$finalType = $types[0];
193+
if ($finalType === 'bool' && !$nullable && $errorType === ErrorType::FALSY) {
194+
// If the function only returns a boolean, since false is for
195+
// error, true is for success. Let's replace this with a "void".
196+
return 'void';
197+
}
198+
return ($nullable !== false ? '?' : '').$finalType;
199+
} else {
196200
return '';
197201
}
198-
199-
if (\in_array('void', $types) || ($types === [] && !$nullable && !$falsable)) {
200-
return 'void';
201-
}
202-
203-
204-
$finalType = $types[0] ?? '';
205-
if ($finalType === 'bool' && !$nullable && $errorType === ErrorType::FALSY) {
206-
// If the function only returns a boolean, since false is for error, true is for success.
207-
// Let's replace this with a "void".
208-
return 'void';
209-
}
210-
return ($nullable !== false ? '?' : '').$finalType;
211202
}
212203

213204
/**

generator/tests/PhpStanFunctions/PhpStanTypeTest.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,7 @@ public function testVoid(): void
105105
{
106106
$param = new PhpStanType('');
107107
$this->assertEquals('', $param->getDocBlockType());
108-
if (PHP_VERSION_ID >= 80200) {
109-
$this->assertEquals('void', $param->getSignatureType());
110-
} else {
111-
$this->assertEquals('', $param->getSignatureType());
112-
}
108+
$this->assertEquals('', $param->getSignatureType());
113109

114110
$param = new PhpStanType('void');
115111
$this->assertEquals('void', $param->getDocBlockType());

0 commit comments

Comments
 (0)