Skip to content

Commit

Permalink
Fix broken generation
Browse files Browse the repository at this point in the history
  • Loading branch information
martinssipenko authored and staabm committed Nov 28, 2024
1 parent 8cef947 commit 5b86a97
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion generator/src/PhpStanFunctions/PhpStanFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(array $signature)
if (count($signature) < 1) {
throw new \RuntimeException('Invalid signatures');
}
$this->returnType = new PhpStanType(\array_shift($signature));
$this->returnType = new PhpStanType(\array_shift($signature), false, true);
foreach ($signature as $name => $type) {
$param = new PhpStanParameter($name, $type);
$this->parameters[$param->getName()] = $param;
Expand Down
2 changes: 1 addition & 1 deletion generator/src/PhpStanFunctions/PhpStanParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(string $name, string $type)
$name = trim($name, '=.&');

$this->name = $name;
$this->type = new PhpStanType($type, $writeOnly);
$this->type = new PhpStanType($type, $writeOnly, false);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion generator/src/PhpStanFunctions/PhpStanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PhpStanType
*/
private $types;

public function __construct(string $data, bool $writeOnly = false)
public function __construct(string $data, bool $writeOnly = false, bool $isReturnType = false)
{
//weird case: null|false => null
if ($data === 'null|false') {
Expand All @@ -57,6 +57,9 @@ public function __construct(string $data, bool $writeOnly = false)
if (($falsablePosition = \array_search('false', $returnTypes)) !== false) {
$falsable = true;
\array_splice($returnTypes, (int) $falsablePosition, 1);
if ($isReturnType === false) {
$returnTypes[] = 'bool';
}
}
/** @var int $count */
$count = \count($returnTypes);
Expand All @@ -76,10 +79,14 @@ public function __construct(string $data, bool $writeOnly = false)
//here we deal with some weird phpstan typings
if ($returnType === 'non-empty-string') {
$returnType = 'string';
} elseif ($returnType === 'non-falsy-string') {
$returnType = 'string';
} elseif ($returnType === 'positive-int') {
$returnType = 'int';
} elseif (is_numeric($returnType)) {
$returnType = 'int';
} elseif (\strpos($returnType, 'int<') !== false) {
$returnType = 'int';
}
if (\strpos($returnType, 'list<') !== false) {
$returnType = \str_replace('list', 'array', $returnType);
Expand Down

0 comments on commit 5b86a97

Please sign in to comment.