Skip to content

Commit

Permalink
Fix generator for PHP >= 8.4 phpspec#624
Browse files Browse the repository at this point in the history
  • Loading branch information
andypost committed Aug 21, 2024
1 parent d1235b2 commit 210c459
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Prophecy/Doubler/Generator/ClassCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Prophecy\Doubler\Generator;

use Prophecy\Doubler\Generator\Node\ArgumentTypeNode;
use Prophecy\Doubler\Generator\Node\ReturnTypeNode;
use Prophecy\Doubler\Generator\Node\TypeNodeAbstract;

Expand Down Expand Up @@ -78,14 +79,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 +102,18 @@ private function generateArguments(array $arguments): array
{
return array_map(function (Node\ArgumentNode $argument){

$php = $this->generateTypes($argument->getTypeNode());
$types = $argument->getTypeNode()->getTypes();
if ($nullable = $argument->isOptional() && $argument->getDefault() === NULL) {
$count = \count($types);
if ($count === 1 && $types[0] === 'mixed' ) {
$nullable = FALSE;
}
elseif ($count > 1 && !isset($types['null'])) {
$argument->setTypeNode(new ArgumentTypeNode('null', ...$argument->getTypeNode()->getNonNullTypes()));
$nullable = FALSE;
}
}
$php = $this->generateTypes($argument->getTypeNode(), $nullable);

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

Expand Down

0 comments on commit 210c459

Please sign in to comment.