mirror of
https://github.com/danog/psalm-plugin-symfony.git
synced 2024-12-02 09:27:46 +01:00
[RepositoryStringShortcut] complain when argument is string only (#33)
This commit is contained in:
parent
55aa860581
commit
7a628c6fda
@ -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()
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user