[RepositoryStringShortcut] complain when argument is string only (#33)

This commit is contained in:
Farhad Safarov 2020-05-26 18:53:50 +03:00 committed by GitHub
parent 55aa860581
commit 7a628c6fda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 25 deletions

View File

@ -5,6 +5,7 @@ namespace Psalm\SymfonyPsalmPlugin\Handler;
use PhpParser\Node; use PhpParser\Node;
use PhpParser\Node\Expr; use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Scalar\String_;
use Psalm\Codebase; use Psalm\Codebase;
use Psalm\CodeLocation; use Psalm\CodeLocation;
use Psalm\Context; use Psalm\Context;
@ -72,7 +73,7 @@ class ClassHandler implements AfterClassLikeAnalysisInterface, AfterMethodCallAn
break; break;
case 'Doctrine\ORM\EntityManagerInterface::getrepository': case 'Doctrine\ORM\EntityManagerInterface::getrepository':
case 'Doctrine\Persistence\ObjectManager::getrepository': case 'Doctrine\Persistence\ObjectManager::getrepository':
if (!$expr->args[0]->value instanceof ClassConstFetch) { if ($expr->args[0]->value instanceof String_ ) {
IssueBuffer::accepts( IssueBuffer::accepts(
new RepositoryStringShortcut(new CodeLocation($statements_source, $expr->args[0]->value)), new RepositoryStringShortcut(new CodeLocation($statements_source, $expr->args[0]->value)),
$statements_source->getSuppressedIssues() $statements_source->getSuppressedIssues()

View File

@ -22,20 +22,9 @@ Feature: RepositoryStringShortcut
</plugins> </plugins>
</psalm> </psalm>
""" """
And I have the following code preamble
Scenario: Asserting using 'AppBundle:Entity' syntax raises issue
Given I have the following code
""" """
<?php <?php
use Doctrine\ORM\EntityManagerInterface;
class SomeService
{
public function __construct(EntityManagerInterface $entityManager)
{
$entityManager->getRepository('AppBundle:Entity');
}
}
namespace Doctrine\ORM; namespace Doctrine\ORM;
interface EntityManagerInterface interface EntityManagerInterface
{ {
@ -47,8 +36,50 @@ Feature: RepositoryStringShortcut
public function getRepository($className); public function getRepository($className);
} }
""" """
Scenario: Asserting using 'AppBundle:Entity' syntax raises issue
Given I have the following code
"""
use Doctrine\ORM\EntityManagerInterface;
class SomeService
{
public function __construct(EntityManagerInterface $entityManager)
{
$entityManager->getRepository('AppBundle:Entity');
}
}
"""
When I run Psalm When I run Psalm
Then I see these errors Then I see these errors
| Type | Message | | Type | Message |
| RepositoryStringShortcut | Use Entity::class syntax instead | | RepositoryStringShortcut | Use Entity::class syntax instead |
And I see no other errors And I see no other errors
Scenario: Asserting using 'Entity::class' notation does not raise issue
Given I have the following code
"""
class SomeService
{
public function __construct(EntityManagerInterface $entityManager)
{
$entityManager->getRepository(Entity::class);
}
}
"""
When I run Psalm
Then I see no errors
Scenario: Dynamic repository calls should not be complained
Given I have the following code
"""
class SomeService
{
public function __construct(EntityManagerInterface $entityManager)
{
$className = 'App\Entity\EntityA';
$entityManager->getRepository($className);
}
}
"""
When I run Psalm
Then I see no errors

View File

@ -16,14 +16,15 @@ Feature: Request getContent
</plugins> </plugins>
</psalm> </psalm>
""" """
And I have the following code preamble
"""
<?php
use Symfony\Component\HttpFoundation\Request;
"""
Scenario: Asserting '$request->getContent()' without any argument returns string Scenario: Asserting '$request->getContent()' without any argument returns string
Given I have the following code Given I have the following code
""" """
<?php
use Symfony\Component\HttpFoundation\Request;
class App class App
{ {
public function index(Request $request): void public function index(Request $request): void
@ -38,10 +39,6 @@ Feature: Request getContent
Scenario: Asserting '$request->getContent(false)' returns string Scenario: Asserting '$request->getContent(false)' returns string
Given I have the following code Given I have the following code
""" """
<?php
use Symfony\Component\HttpFoundation\Request;
class App class App
{ {
public function index(Request $request): void public function index(Request $request): void
@ -56,10 +53,6 @@ Feature: Request getContent
Scenario: Asserting '$request->getContent(true)' returns resource Scenario: Asserting '$request->getContent(true)' returns resource
Given I have the following code Given I have the following code
""" """
<?php
use Symfony\Component\HttpFoundation\Request;
class App class App
{ {
public function index(Request $request): void public function index(Request $request): void