Skip to content

Commit

Permalink
update for latest PHPStan's getType on properties
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 23, 2024
1 parent d0b92fb commit 9ba5388
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/Reflection/EntityDateTimePropertyReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\TypeCombinator;
Expand All @@ -29,7 +30,10 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
return false;
}

$propertyType = TypeCombinator::removeNull($property->getType()); // remove null to properly match subtype
$type = $property->getReadableType() ?? $property->getWritableType();
if ($type === null) throw new ShouldNotHappenException();

$propertyType = TypeCombinator::removeNull($type); // remove null to properly match subtype
$dateTimeType = new ObjectType(\DateTimeImmutable::class);
$hasDateTime = $dateTimeType->isSuperTypeOf($propertyType)->yes();

Expand All @@ -40,14 +44,14 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
$property = $classReflection->getPropertyTags()[$propertyName] ?? null;
if ($property === null) {
if ($property === null || $property->getReadableType() === null) {
throw new ShouldNotHappenException();
}

return new AnnotationPropertyReflection(
$classReflection,
$property->getType(),
TypeCombinator::union($property->getType(), new StringType()),
$property->getReadableType(),
TypeCombinator::union($property->getWritableType() ?? new NeverType(), new StringType()),
$property->isReadable(),
$property->isWritable()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Reflection\PropertyReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NeverType;
use PHPStan\Type\TypeCombinator;


Expand Down Expand Up @@ -41,14 +42,14 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
$property = $classReflection->getPropertyTags()[$propertyName] ?? null;
if ($property === null) {
if ($property === null || $property->getReadableType() === null) {
throw new ShouldNotHappenException();
}

return new AnnotationPropertyReflection(
$classReflection,
$property->getType(),
TypeCombinator::union($property->getType(), new IntegerType()),
$property->getReadableType(),
TypeCombinator::union($property->getWritableType() ?? new NeverType(), new IntegerType()),
$property->isReadable(),
$property->isWritable()
);
Expand Down

0 comments on commit 9ba5388

Please sign in to comment.