Skip to content

Commit

Permalink
Fix generator for PHP >= 8.4 #624
Browse files Browse the repository at this point in the history
  • Loading branch information
andypost committed Aug 29, 2024
1 parent e3810e5 commit d0aa1d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions spec/Prophecy/Doubler/Generator/ClassCodeGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class CustomClass extends \RuntimeException implements \Prophecy\Doubler\Generat
public $name;
private $email;
public static function getName(array $fullname = NULL, \ReflectionClass $class, object $instance): ?string {
public static function getName(?array $fullname = NULL, \ReflectionClass $class, object $instance): ?string {
return $this->name;
}
protected function getEmail(?string $default = '[email protected]') {
Expand Down Expand Up @@ -272,7 +272,7 @@ function it_overrides_properly_methods_with_args_passed_by_reference(
namespace {
class CustomClass extends \RuntimeException implements \Prophecy\Doubler\Generator\MirroredInterface {
public function getName(array &$fullname = NULL) {
public function getName(?array &$fullname = NULL) {
return $this->name;
}
Expand Down
17 changes: 14 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 @@ -76,14 +77,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 @@ -99,7 +100,17 @@ private function generateArguments(array $arguments): array
{
return array_map(function (Node\ArgumentNode $argument) {

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

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

Expand Down

0 comments on commit d0aa1d9

Please sign in to comment.