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

Doctrine Repository inspections defaulting to arrays #1481

Open
Matt-PMCT opened this issue May 31, 2020 · 3 comments
Open

Doctrine Repository inspections defaulting to arrays #1481

Matt-PMCT opened this issue May 31, 2020 · 3 comments

Comments

@Matt-PMCT
Copy link

Matt-PMCT commented May 31, 2020

Using 0.20.195 and I have the following code
$userGroup = $this->getDoctrine()->getRepository(User\UserGroup::class)->findByTitle($userGroupTitle);
In the repository I have the function findByTitle, which is clearly typehinted to return a single UserGroup object:
public function findByTitle(string $title): UserGroup { ... }
This used to not throw a warning, but now I get this warning later when I try to pass the UserGroup
Expected parameter of type '\App\Entity\User\UserGroup', '\App\Entity\User\UserGroup[]' provided

To lay it out more clearly. I have created a custom Repository, where I wrote my own custom findByTitle function. This function has a specified return type. I would expect that return type to be used before the magic method logic is used to determine the return type.

@Matt-PMCT
Copy link
Author

This appears to be related to pull request #1471 which allowed for type hinting the magic doctrine methods. Specifically the code here c92446c

I am not very familiar with how the program works, but perhaps some logic to see if the user has manually overridden then magic methods by writing their own "findBy*" class before defaulting to the magic method determination.

Haehnchen added a commit that referenced this issue Jun 14, 2020
Haehnchen added a commit that referenced this issue Jun 14, 2020
Haehnchen added a commit that referenced this issue Jun 14, 2020
Haehnchen added a commit that referenced this issue Jun 14, 2020
ignore Doctrine repository return type provider on magic method pattern if already in repository #1481
@Haehnchen
Copy link
Owner

done as suggested ;)

@Matt-PMCT
Copy link
Author

@Haehnchen Thanks, I'm still not seeing this change working in 20.200, the same errors persist for me as described in the original post.

Maybe my problem would be caught by a second function in (src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/doctrine/fixtures/ObjectRepositoryResultTypeProvider.php)

   class BarRepository implements ObjectRepository
    {
        public function find() {}

        /**
         * @return Bar[]
         */
        public function findOneByFancyStuff() {}

        /**
         * @return Bar
         */
        public function findByFancyStuff() {}
    }

And then one more test (src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/doctrine/ObjectRepositoryResultTypeProviderTest.java)

public void testThatClassAsStringIsResolvedForMagicMethodsButNotWhenAlreadyExists() {
        // do nothing at all here; use type from the method it self
        assertPhpReferenceResolveTo(PhpFileType.INSTANCE,
            "<?php" +
                "/** @var \\Doctrine\\Common\\Persistence\\ObjectManager $om */\n" +
                "$om->getRepository('\\Foo\\Bar')->findOneByFancyStuff('foobar')[0]->get<caret>Id();",
            PlatformPatterns.psiElement(Method.class).withName("getId")
        );
        assertPhpReferenceResolveTo(PhpFileType.INSTANCE,
            "<?php" +
                "/** @var \\Doctrine\\Common\\Persistence\\ObjectManager $om */\n" +
                "$om->getRepository('\\Foo\\Bar')->findByFancyStuff('foobar')->get<caret>Id();",
            PlatformPatterns.psiElement(Method.class).withName("getId")
        );
        // repository class exists but method is magic
        assertPhpReferenceResolveTo(PhpFileType.INSTANCE,
            "<?php" +
                "/** @var \\Doctrine\\Common\\Persistence\\ObjectManager $om */\n" +
                "$om->getRepository('\\Foo\\Bar')->findOneByFancyStuffNotMagic('foobar')->get<caret>Id();",
            PlatformPatterns.psiElement(Method.class).withName("getId")
        );
    }

That would test my exact problem, I have a repository which is type hinted to return a single object with a findByTitle() class, but the inspection is assuming it returns an array.

Thanks for your work on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants