Skip to content

Commit

Permalink
[RepositoryStringShortcut] complain when argument is string only (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
seferov committed May 26, 2020
1 parent 55aa860 commit 7a628c6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/Handler/ClassHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Scalar\String_;
use Psalm\Codebase;
use Psalm\CodeLocation;
use Psalm\Context;
Expand Down Expand Up @@ -72,7 +73,7 @@ public static function afterMethodCallAnalysis(
break;
case 'Doctrine\ORM\EntityManagerInterface::getrepository':
case 'Doctrine\Persistence\ObjectManager::getrepository':
if (!$expr->args[0]->value instanceof ClassConstFetch) {
if ($expr->args[0]->value instanceof String_ ) {
IssueBuffer::accepts(
new RepositoryStringShortcut(new CodeLocation($statements_source, $expr->args[0]->value)),
$statements_source->getSuppressedIssues()
Expand Down
55 changes: 43 additions & 12 deletions tests/acceptance/acceptance/RepositoryStringShortcut.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,9 @@ Feature: RepositoryStringShortcut
</plugins>
</psalm>
"""

Scenario: Asserting using 'AppBundle:Entity' syntax raises issue
Given I have the following code
And I have the following code preamble
"""
<?php
use Doctrine\ORM\EntityManagerInterface;
class SomeService
{
public function __construct(EntityManagerInterface $entityManager)
{
$entityManager->getRepository('AppBundle:Entity');
}
}
namespace Doctrine\ORM;
interface EntityManagerInterface
{
Expand All @@ -47,8 +36,50 @@ Feature: RepositoryStringShortcut
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
Then I see these errors
| Type | Message |
| RepositoryStringShortcut | Use Entity::class syntax instead |
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
17 changes: 5 additions & 12 deletions tests/acceptance/acceptance/RequestContent.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ Feature: Request getContent
</plugins>
</psalm>
"""

Scenario: Asserting '$request->getContent()' without any argument returns string
Given I have the following code
And I have the following code preamble
"""
<?php
use Symfony\Component\HttpFoundation\Request;
"""

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

0 comments on commit 7a628c6

Please sign in to comment.