Skip to content

Commit

Permalink
Merge pull request #941 from ondrejmirtes/mixed-allows-null
Browse files Browse the repository at this point in the history
ReflectionNamedType with mixed allows null
  • Loading branch information
Ocramius committed Jan 3, 2022
2 parents 24a0301 + b16fa8e commit 4c4a1da
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Reflection/Adapter/ReflectionNamedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use ReflectionNamedType as CoreReflectionNamedType;
use Roave\BetterReflection\Reflection\ReflectionNamedType as BetterReflectionNamedType;

use function strtolower;

/**
* @psalm-suppress MissingImmutableAnnotation
*/
Expand All @@ -23,7 +25,7 @@ public function getName(): string

public function __toString(): string
{
return ($this->allowsNull ? '?' : '')
return ($this->allowsNull && strtolower($this->betterReflectionType->getName()) !== 'mixed' ? '?' : '')
. $this->betterReflectionType->__toString();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Adapter/ReflectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public static function fromTypeOrNull(BetterReflectionNamedType|BetterReflection
return new ReflectionIntersectionType($betterReflectionType);
}

return new ReflectionNamedType($betterReflectionType, false);
return new ReflectionNamedType($betterReflectionType, $betterReflectionType->allowsNull());
}
}
2 changes: 1 addition & 1 deletion src/Reflection/ReflectionNamedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getClass(): ReflectionClass

public function allowsNull(): bool
{
return false;
return strtolower($this->name) === 'mixed';
}

public function __toString(): string
Expand Down
15 changes: 15 additions & 0 deletions test/unit/Reflection/Adapter/ReflectionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

namespace Roave\BetterReflectionTest\Reflection\Adapter;

use PhpParser\Node\Identifier;
use PHPUnit\Framework\TestCase;
use ReflectionClass as CoreReflectionClass;
use ReflectionType as CoreReflectionType;
use Roave\BetterReflection\Reflection\Adapter\ReflectionIntersectionType as ReflectionIntersectionTypeAdapter;
use Roave\BetterReflection\Reflection\Adapter\ReflectionNamedType as ReflectionNamedTypeAdapter;
use Roave\BetterReflection\Reflection\Adapter\ReflectionType;
use Roave\BetterReflection\Reflection\Adapter\ReflectionType as ReflectionTypeAdapter;
use Roave\BetterReflection\Reflection\Adapter\ReflectionUnionType as ReflectionUnionTypeAdapter;
use Roave\BetterReflection\Reflection\ReflectionIntersectionType as BetterReflectionIntersectionType;
use Roave\BetterReflection\Reflection\ReflectionNamedType;
use Roave\BetterReflection\Reflection\ReflectionNamedType as BetterReflectionNamedType;
use Roave\BetterReflection\Reflection\ReflectionParameter;
use Roave\BetterReflection\Reflection\ReflectionUnionType as BetterReflectionUnionType;
use Roave\BetterReflection\Reflector\Reflector;

use function array_combine;
use function array_map;
Expand Down Expand Up @@ -83,4 +88,14 @@ public function testFromTypeOrNullWithIntersectionType(): void
{
self::assertInstanceOf(ReflectionIntersectionTypeAdapter::class, ReflectionTypeAdapter::fromTypeOrNull($this->createMock(BetterReflectionIntersectionType::class)));
}

public function testMixedAllowsNull(): void
{
$type = ReflectionType::fromTypeOrNull(new ReflectionNamedType(
$this->createMock(Reflector::class),
$this->createMock(ReflectionParameter::class),
new Identifier('mixed'),
));
self::assertTrue($type->allowsNull());
}
}
6 changes: 6 additions & 0 deletions test/unit/Reflection/ReflectionNamedTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public function testAllowsNull(): void
self::assertFalse($noNullType->allowsNull());
}

public function testMixedAllowsNull(): void
{
$noNullType = $this->createType('mixed');
self::assertTrue($noNullType->allowsNull());
}

public function isBuildinProvider(): Generator
{
yield ['string'];
Expand Down

0 comments on commit 4c4a1da

Please sign in to comment.