Skip to content

[PropertyInfo] Fix interface handling in PhpStanTypeHelper #59012

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

Merged
merged 1 commit into from
Dec 10, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Clazz;
use Symfony\Component\PropertyInfo\Tests\Fixtures\ConstructorDummyWithoutDocBlock;
use Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultValue;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyCollection;
use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyGeneric;
use Symfony\Component\PropertyInfo\Tests\Fixtures\IFace;
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php80PromotedDummy;
Expand Down Expand Up @@ -482,7 +485,88 @@ public static function php80TypesProvider()

public function testGenericInterface()
{
$this->assertNull($this->extractor->getTypes(Dummy::class, 'genericInterface'));
$this->assertEquals(
[
new Type(
builtinType: Type::BUILTIN_TYPE_OBJECT,
class: \BackedEnum::class,
collectionValueType: new Type(
builtinType: Type::BUILTIN_TYPE_STRING,
)
),
],
$this->extractor->getTypes(Dummy::class, 'genericInterface')
);
}

/**
* @param list<Type> $expectedTypes
* @dataProvider genericsProvider
*/
public function testGenericsLegacy(string $property, array $expectedTypes)
{
$this->assertEquals($expectedTypes, $this->extractor->getTypes(DummyGeneric::class, $property));
}

/**
* @return iterable<array{0: string, 1: list<Type>}>
*/
public static function genericsProvider(): iterable
{
yield [
'basicClass',
[
new Type(
builtinType: Type::BUILTIN_TYPE_OBJECT,
class: Clazz::class,
collectionValueType: new Type(
builtinType: Type::BUILTIN_TYPE_OBJECT,
class: Dummy::class,
)
),
],
];
yield [
'nullableClass',
[
new Type(
builtinType: Type::BUILTIN_TYPE_OBJECT,
class: Clazz::class,
nullable: true,
collectionValueType: new Type(
builtinType: Type::BUILTIN_TYPE_OBJECT,
class: Dummy::class,
)
),
],
];
yield [
'basicInterface',
[
new Type(
builtinType: Type::BUILTIN_TYPE_OBJECT,
class: IFace::class,
collectionValueType: new Type(
builtinType: Type::BUILTIN_TYPE_OBJECT,
class: Dummy::class,
)
),
],
];
yield [
'nullableInterface',
[
new Type(
builtinType: Type::BUILTIN_TYPE_OBJECT,
class: IFace::class,
nullable: true,
collectionValueType: new Type(
builtinType: Type::BUILTIN_TYPE_OBJECT,
class: Dummy::class,
)
),
],
];
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyInfo\Tests\Fixtures;

interface IFace {}

class Clazz {}

class DummyGeneric
{

/**
* @var Clazz<Dummy>
*/
public $basicClass;

/**
* @var ?Clazz<Dummy>
*/
public $nullableClass;

/**
* @var IFace<Dummy>
*/
public $basicInterface;

/**
* @var ?IFace<Dummy>
*/
public $nullableInterface;

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function extractTypes(TypeNode $node, NameScope $nameScope): array
$collection = $mainType->isCollection() || \is_a($mainType->getClassName(), \Traversable::class, true) || \is_a($mainType->getClassName(), \ArrayAccess::class, true);

// it's safer to fall back to other extractors if the generic type is too abstract
if (!$collection && !class_exists($mainType->getClassName())) {
if (!$collection && !class_exists($mainType->getClassName()) && !interface_exists($mainType->getClassName(), false)) {
return [];
}

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy