Skip to content

[PropertyInfo] PhpStan extractor nested object fix #44637

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 26, 2021
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
26 changes: 19 additions & 7 deletions src/Symfony/Component/PropertyInfo/PhpStan/NameScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,33 @@ final class NameScopeFactory
{
public function create(string $fullClassName): NameScope
{
$reflection = new \ReflectionClass($fullClassName);
$path = explode('\\', $fullClassName);
$className = array_pop($path);
[$namespace, $uses] = $this->extractFromFullClassName($fullClassName);
[$namespace, $uses] = $this->extractFromFullClassName($reflection);

foreach (class_uses($fullClassName) as $traitFullClassName) {
[, $traitUses] = $this->extractFromFullClassName($traitFullClassName);
$uses = array_merge($uses, $traitUses);
}
$uses = array_merge($uses, $this->collectUses($reflection));

return new NameScope($className, $namespace, $uses);
}

private function extractFromFullClassName(string $fullClassName): array
private function collectUses(\ReflectionClass $reflection): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

collectTraits would be a better name IMHO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think collectUses sounds more logic in this context, because method collects not only traits, but class uses as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas rebased the branch, waiting for yours input on remaining issue

{
$uses = [$this->extractFromFullClassName($reflection)[1]];

foreach ($reflection->getTraits() as $traitReflection) {
$uses[] = $this->extractFromFullClassName($traitReflection)[1];
}

if (false !== $parentClass = $reflection->getParentClass()) {
$uses[] = $this->collectUses($parentClass);
}

return $uses ? array_merge(...$uses) : [];
}

private function extractFromFullClassName(\ReflectionClass $reflection): array
{
$reflection = new \ReflectionClass($fullClassName);
$namespace = trim($reflection->getNamespaceName(), '\\');
$fileName = $reflection->getFileName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultValue;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\RootDummy\RootDummyItem;
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsedInTrait;
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsingTrait;
use Symfony\Component\PropertyInfo\Type;
Expand Down Expand Up @@ -116,6 +117,8 @@ public function typesProvider()
['arrayOfMixed', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_STRING), null)]],
['listOfStrings', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))]],
['self', [new Type(Type::BUILTIN_TYPE_OBJECT, false, Dummy::class)]],
['rootDummyItems', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, RootDummyItem::class))]],
['rootDummyItem', [new Type(Type::BUILTIN_TYPE_OBJECT, false, RootDummyItem::class)]],
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public function testGetProperties()
'files',
'propertyTypeStatic',
'parentAnnotationNoParent',
'rootDummyItems',
'rootDummyItem',
'a',
'DOB',
'Id',
Expand Down Expand Up @@ -138,6 +140,8 @@ public function testGetPropertiesWithCustomPrefixes()
'files',
'propertyTypeStatic',
'parentAnnotationNoParent',
'rootDummyItems',
'rootDummyItem',
'date',
'c',
'ct',
Expand Down Expand Up @@ -187,6 +191,8 @@ public function testGetPropertiesWithNoPrefixes()
'files',
'propertyTypeStatic',
'parentAnnotationNoParent',
'rootDummyItems',
'rootDummyItem',
],
$noPrefixExtractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy')
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\PropertyInfo\Tests\Fixtures;

use Symfony\Component\PropertyInfo\Tests\Fixtures\RootDummy\RootDummyItem;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
Expand Down Expand Up @@ -58,6 +60,16 @@ class ParentDummy
*/
public $parentAnnotationNoParent;

/**
* @var RootDummyItem[]
*/
public $rootDummyItems;

/**
* @var \Symfony\Component\PropertyInfo\Tests\Fixtures\RootDummy\RootDummyItem
*/
public $rootDummyItem;

/**
* @return bool|null
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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\RootDummy;

class RootDummyItem
{
}