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

Handle non docblock constructor promoted property #1462

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
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