Skip to content

Commit

Permalink
Merge pull request #1462 from dgafka/handle-non-docblock-constructor-…
Browse files Browse the repository at this point in the history
…promoted-property

Handle non docblock constructor promoted property
  • Loading branch information
goetas committed Jan 12, 2023
2 parents 4b40b09 + 19d5dd8 commit e8b252e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
2 changes: 2 additions & 0 deletions phpstan/no-typed-prop.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ parameters:
- %currentWorkingDirectory%/tests/Fixtures/TypedProperties/UnionTypedProperties.php
- %currentWorkingDirectory%/tests/Metadata/Driver/UnionTypedPropertiesDriverTest.php
- %currentWorkingDirectory%/tests/Fixtures/DocBlockType/Collection/ConstructorPropertyPromotion.php
- %currentWorkingDirectory%/tests/Fixtures/DocBlockType/Collection/ConstructorPropertyPromotionWithoutDocblock.php
- %currentWorkingDirectory%/tests/Fixtures/DocBlockType/Collection/ConstructorPropertyPromotionWithScalar.php
- %currentWorkingDirectory%/tests/Serializer/BaseSerializationTest.php
11 changes: 10 additions & 1 deletion src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,16 @@ private function resolveTypeFromDocblock(\ReflectionProperty $reflectionProperty
{
$docComment = $reflectionProperty->getDocComment();
if (!$docComment && PHP_VERSION_ID >= 80000 && $reflectionProperty->isPromoted()) {
$docComment = $reflectionProperty->getDeclaringClass()->getConstructor()->getDocComment();
$constructor = $reflectionProperty->getDeclaringClass()->getConstructor();
if (!$constructor) {
return [];
}

$docComment = $constructor->getDocComment();

if (!$docComment) {
return [];
}

$tokens = $this->lexer->tokenize($docComment);
$phpDocNode = $this->phpDocParser->parse(new TokenIterator($tokens));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures\DocBlockType\Collection;

final class ConstructorPropertyPromotionWithScalar
{
/**
* @param string $data
*/
public function __construct(
private $data,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures\DocBlockType\Collection;

final class ConstructorPropertyPromotionWithoutDocblock
{
public function __construct(
private array $data,
) {
}
}
30 changes: 30 additions & 0 deletions tests/Metadata/Driver/DocBlockDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use JMS\Serializer\Tests\Fixtures\DocBlockType\Collection\CollectionOfScalars;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Collection\CollectionTypedAsGenericClass;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Collection\ConstructorPropertyPromotion;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Collection\ConstructorPropertyPromotionWithoutDocblock;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Collection\ConstructorPropertyPromotionWithScalar;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Collection\Details\ProductColor;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Collection\Details\ProductDescription;
use JMS\Serializer\Tests\Fixtures\DocBlockType\Collection\Details\ProductName;
Expand Down Expand Up @@ -378,6 +380,34 @@ public function testInferTypeForConstructorPropertyPromotion()
);
}

public function testInferTypeForConstructorPropertyPromotionWithoutDocblock()
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Constructor property promotion requires PHP 8.0');
}

$m = $this->resolve(ConstructorPropertyPromotionWithoutDocblock::class);

self::assertEquals(
null,
$m->propertyMetadata['data']->type
);
}

public function testInferTypeForConstructorPropertyPromotionWithScalar()
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Constructor property promotion requires PHP 8.0');
}

$m = $this->resolve(ConstructorPropertyPromotionWithScalar::class);

self::assertEquals(
['name' => 'string', 'params' => []],
$m->propertyMetadata['data']->type
);
}

public function testInferTypeForPhpstanArray()
{
$m = $this->resolve(PhpstanArrayShape::class);
Expand Down

0 comments on commit e8b252e

Please sign in to comment.