Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[PropertyInfo] Namespace use s #44417

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

Closed
ivansrdic opened this issue Dec 2, 2021 · 4 comments Β· Fixed by #44430
Closed

[PropertyInfo] Namespace use s #44417

ivansrdic opened this issue Dec 2, 2021 · 4 comments Β· Fixed by #44430

Comments

@ivansrdic
Copy link

Symfony version(s) affected

5.4.0,6.0.0

Description

If we have a class which has use statements which use a namespace instead of a class/interface, \Symfony\Component\PropertyInfo\PhpStan\NameScope::resolveStringName throws an exception.

How to reproduce

namespace App\Test\SomeNamespace;

class MyNamespacedClass
{
}

namespace App\Entity;

use App\Test\SomeNamespace as TestSomeNamespace;

class MyClass
{
    public function __construct()
    {
        $object = new TestSomeNamespace\MyNamespacedClass();
    }
}

run bin/console cache:clear and you will get an exception like this

In NameScope.php line 50:
  [ErrorException]                                    
  Warning: Undefined array key "MyNamespacedClass"

Possible Solution

I think there is a mistake in logic in \Symfony\Component\PropertyInfo\PhpStan\NameScope::resolveStringName, lines 48 and 50. The array_shift call is correct, but we need it's output because that's the value by which the uses are indexed in $this->uses

Changing those two lines to

    public function resolveStringName(string $name): string
    {
        if (0 === strpos($name, '\\')) {
            return ltrim($name, '\\');
        }

        $nameParts = explode('\\', $name);
        if (isset($this->uses[$nameParts[0]])) {
            if (1 === \count($nameParts)) {
                return $this->uses[$nameParts[0]];
            }
            array_shift($nameParts);

            return sprintf('%s\\%s', $this->uses[$nameParts[0]], implode('\\', $nameParts));
        }

        if (null !== $this->namespace) {
            return sprintf('%s\\%s', $this->namespace, $name);
        }

        return $name;
    }
    public function resolveStringName(string $name): string
    {
        if (0 === strpos($name, '\\')) {
            return ltrim($name, '\\');
        }

        $nameParts = explode('\\', $name);
        if (isset($this->uses[$nameParts[0]])) {
            if (1 === \count($nameParts)) {
                return $this->uses[$nameParts[0]];
            }
            $use = array_shift($nameParts);

            return sprintf('%s\\%s', $use, implode('\\', $nameParts));
        }

        if (null !== $this->namespace) {
            return sprintf('%s\\%s', $this->namespace, $name);
        }

        return $name;
    }

Additional Context

No response

@nicolas-grekas
Copy link
Member

Must be related to #40457 /cc @Korbeil WDYT?

Korbeil added a commit to Korbeil/symfony that referenced this issue Dec 2, 2021
@Korbeil
Copy link
Contributor

Korbeil commented Dec 2, 2021

Thanks for your report @ivansrdic and your ping @nicolas-grekas.
I made a reproducer: Korbeil@33f8d17 and will try to find a fix as soon as I can πŸ™
I tried with your suggestion but it doesn't seem to fix the issue.

@ivansrdic
Copy link
Author

@Korbeil
Any update to this? Is the PR intentionally left with the failing test for the return type patch until someone reviews it, or are the reviewers waiting for that test to be fixed?
Can I help somehow maybe?

@Korbeil
Copy link
Contributor

Korbeil commented Dec 14, 2021

The failing test is not related to the PR, I'm just waiting for someone to review / merge the pull request.

nicolas-grekas added a commit that referenced this issue Dec 14, 2021
This PR was merged into the 5.4 branch.

Discussion
----------

[PropertyInfo] Fix aliased namespace matching

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #44417
| License       | MIT
| Doc PR        | N/A

When you have an aliased namespace that you use in your code it was erroring because we didn't stored the first part of the namespace that should be replaced.

Commits
-------

432b1a1 Fix aliased namespaces matching
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants