Skip to content

Commit

Permalink
Merge branch '7.1' into 7.2
Browse files Browse the repository at this point in the history
* 7.1:
  Fix resolve enum in string type resolver
  [PropertyInfo] Upmerge #59012
  [BeanstalkMessenger] Round delay to an integer to avoid deprecation warning
  [PropertyInfo] Fix interface handling in `PhpStanTypeHelper`
  [HttpClient] Test POST to GET redirects
  [HttpKernel] Denormalize request data using the csv format when using "#[MapQueryString]" or "#[MapRequestPayload]" (except for content data)
  fix: preserve and nowrap in profiler code highlighting
  • Loading branch information
xabbuh committed Dec 11, 2024
2 parents 65fb9be + 65bb593 commit 8c05131
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 2 deletions.
110 changes: 109 additions & 1 deletion Tests/Extractor/PhpStanExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
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\ConstructorDummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\ConstructorDummyWithoutDocBlock;
use Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultValue;
use Symfony\Component\PropertyInfo\Tests\Fixtures\DockBlockFallback;
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\DummyNamespace;
use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyPropertyAndGetterWithDifferentTypes;
use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyUnionType;
use Symfony\Component\PropertyInfo\Tests\Fixtures\IFace;
use Symfony\Component\PropertyInfo\Tests\Fixtures\IntRangeDummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\InvalidDummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy;
Expand Down Expand Up @@ -552,6 +555,77 @@ public static function allowPrivateAccessLegacyProvider(): array
];
}

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

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

/**
* @dataProvider typesProvider
*/
Expand Down Expand Up @@ -968,7 +1042,41 @@ public static function allowPrivateAccessProvider(): array

public function testGenericInterface()
{
$this->assertNull($this->extractor->getTypes(Dummy::class, 'genericInterface'));
$this->assertEquals(
Type::generic(Type::object(\BackedEnum::class), Type::string()),
$this->extractor->getType(Dummy::class, 'genericInterface'),
);
}

/**
* @dataProvider genericsProvider
*/
public function testGenerics(string $property, Type $expectedType)
{
$this->assertEquals($expectedType, $this->extractor->getType(DummyGeneric::class, $property));
}

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

Expand Down
41 changes: 41 additions & 0 deletions Tests/Fixtures/DummyGeneric.php
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 <[email protected]>
*
* 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;

}
2 changes: 1 addition & 1 deletion Util/PhpStanTypeHelper.php
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

0 comments on commit 8c05131

Please sign in to comment.