-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support writing string to DateTimeImmutable properties [closes #8]
- Loading branch information
Showing
5 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/Reflection/EntityDateTimePropertyReflectionExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nextras\OrmPhpStan\Reflection; | ||
|
||
use Nextras\Orm\Entity\IEntity; | ||
use Nextras\OrmPhpStan\Reflection\Annotations\AnnotationPropertyReflection; | ||
use PHPStan\Reflection\Annotations\AnnotationsPropertiesClassReflectionExtension; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\PropertiesClassReflectionExtension; | ||
use PHPStan\Reflection\PropertyReflection; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
|
||
class EntityDateTimePropertyReflectionExtension implements PropertiesClassReflectionExtension | ||
{ | ||
/** @var AnnotationsPropertiesClassReflectionExtension */ | ||
private $annotationsExtension; | ||
|
||
|
||
public function __construct(AnnotationsPropertiesClassReflectionExtension $annotationsExtension) | ||
{ | ||
$this->annotationsExtension = $annotationsExtension; | ||
} | ||
|
||
|
||
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool | ||
{ | ||
$hasProperty = $this->annotationsExtension->hasProperty($classReflection, $propertyName); | ||
if (!$hasProperty) { | ||
return false; | ||
} | ||
|
||
$interfaces = array_map(function (ClassReflection $interface) { | ||
return $interface->getName(); | ||
}, $classReflection->getInterfaces()); | ||
if (!in_array(IEntity::class, $interfaces, true)) { | ||
return false; | ||
} | ||
|
||
$property = $this->annotationsExtension->getProperty($classReflection, $propertyName); | ||
$propertyType = TypeCombinator::removeNull($property->getReadableType()); // remove null to be properly match subtype | ||
$dateTimeType = new ObjectType(\DateTimeImmutable::class); | ||
$hasDateTime = $dateTimeType->isSuperTypeOf($propertyType)->yes(); | ||
|
||
return $hasDateTime; | ||
} | ||
|
||
|
||
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection | ||
{ | ||
$property = $this->annotationsExtension->getProperty($classReflection, $propertyName); | ||
return new AnnotationPropertyReflection( | ||
$property->getDeclaringClass(), | ||
$property->getReadableType(), | ||
TypeCombinator::union($property->getWritableType(), new StringType()), | ||
$property->isReadable(), | ||
$property->isWritable() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace NextrasTests\OrmPhpStan\Types; | ||
|
||
class DateTimePropertyTypesTest | ||
{ | ||
public function testError(Book $book): void | ||
{ | ||
$book->date = 1; | ||
} | ||
|
||
|
||
public function testOk(Book $book): void | ||
{ | ||
$book->date = 'now'; | ||
$this->takeNullableDateTime($book->date); | ||
$book->date = null; | ||
} | ||
|
||
|
||
private function takeNullableDateTime(?\DateTimeImmutable $data): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters