Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mixed ReflectionNamedType does allow null values #941

Merged
merged 1 commit into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' ? '?' : '')
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
. $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