Skip to content

Commit

Permalink
Fix generator for PHP >= 8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
andypost committed Aug 14, 2024
1 parent 6884364 commit af0c870
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
composer-flags: "--prefer-lowest"
- php: '8.3'
composer-flags: "--ignore-platform-req=php+" # TODO move that to a normal job without flag once phpspec supports it
- php: '8.4'
composer-flags: "--ignore-platform-req=php+" # TODO move that to a normal job without flag once phpspec supports it

steps:
- uses: actions/checkout@v4
Expand Down
8 changes: 5 additions & 3 deletions src/Prophecy/Doubler/Generator/ClassCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ private function generateMethod(Node\MethodNode $method): string
return $php.'}';
}

private function generateTypes(TypeNodeAbstract $typeNode): string
private function generateTypes(TypeNodeAbstract $typeNode, bool $nullable = FALSE): string
{
if (!$typeNode->getTypes()) {
return '';
}

// When we require PHP 8 we can stop generating ?foo nullables and remove this first block
if ($typeNode->canUseNullShorthand()) {
if ($typeNode->canUseNullShorthand() || $nullable) {
return sprintf( '?%s', $typeNode->getNonNullTypes()[0]);
} else {
return join('|', $typeNode->getTypes());
Expand All @@ -101,7 +101,9 @@ private function generateArguments(array $arguments): array
{
return array_map(function (Node\ArgumentNode $argument){

$php = $this->generateTypes($argument->getTypeNode());
$types = $argument->getTypeNode()->getTypes();
$null = $argument->isOptional() && $argument->getDefault() === NULL && \count($types) === 1 && $types[0] !== 'mixed';
$php = $this->generateTypes($argument->getTypeNode(), $null);

$php .= ' '.($argument->isPassedByReference() ? '&' : '');

Expand Down

0 comments on commit af0c870

Please sign in to comment.