Skip to content

Commit

Permalink
Fix broken generation
Browse files Browse the repository at this point in the history
  • Loading branch information
martinssipenko committed Mar 17, 2024
1 parent 3a6e43b commit 8e292e7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion generator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"thecodingmachine/phpstan-strict-rules": "^1.0",
"squizlabs/php_codesniffer": "^3.2",
"php-coveralls/php-coveralls": "^2.1",
"phpstan/phpstan": "^1.5"
"phpstan/phpstan": "^1.10.40"
},
"scripts": {
"test": "vendor/bin/phpunit",
Expand Down
27 changes: 15 additions & 12 deletions generator/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 signoatures');
}
$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 8e292e7

Please sign in to comment.