Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions stubs/DBAL/since-4/Connection.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ use Traversable;

/**
* The queries of doctrine/dbal 4 are SQL sinks.
*
* The stub replaces the docblock of the class, so it declares the type aliases of DBAL's own docblock that the
* methods use.
*
* @phpstan-type WrapperParameterType = string|Type|ParameterType|ArrayParameterType
* @phpstan-type WrapperParameterTypeArray = array<int<0, max>, WrapperParameterType>|array<string, WrapperParameterType>
*/
class Connection
{
Expand Down
8 changes: 5 additions & 3 deletions stubs/EntityManager.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Doctrine\ORM;

use Doctrine\DBAL\LockMode;

class EntityManager implements EntityManagerInterface
{
/**
Expand All @@ -18,7 +20,7 @@ class EntityManager implements EntityManagerInterface
* @template T
* @param class-string<T> $entityName
* @param mixed $id
* @param null|int $lockMode
* @param LockMode|int|null $lockMode
* @param null|int $lockVersion
*
* @return null|T
Expand All @@ -39,8 +41,8 @@ class EntityManager implements EntityManagerInterface
}

/**
* @param object $entity
* @param null|int $lockMode
* @param object $entity
* @param LockMode|int|null $lockMode
*
* @return void
*/
Expand Down
5 changes: 3 additions & 2 deletions stubs/EntityManagerInterface.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\ORM;

use Doctrine\DBAL\LockMode;
use Doctrine\Persistence\ObjectManager;

interface EntityManagerInterface extends ObjectManager
Expand All @@ -23,7 +24,7 @@ interface EntityManagerInterface extends ObjectManager
*
* @template T
*/
public function find(string $entityName, $id, ?int $lockMode = null, ?int $lockVersion = null);
public function find(string $entityName, $id, LockMode|int|null $lockMode = null, ?int $lockVersion = null);

/**
* @param class-string<T> $entityName
Expand All @@ -40,5 +41,5 @@ interface EntityManagerInterface extends ObjectManager
*
* @return void
*/
public function refresh($entity, ?int $lockMode = null);
public function refresh($entity, LockMode|int|null $lockMode = null);
}
3 changes: 2 additions & 1 deletion stubs/EntityRepository.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Doctrine\ORM;
use Doctrine\Common\Collections\Selectable;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\DBAL\LockMode;
use Doctrine\Persistence\ObjectRepository;

/**
Expand Down Expand Up @@ -38,7 +39,7 @@ class EntityRepository implements ObjectRepository, Selectable
}

/**
* @param ?int $lockMode
* @param LockMode|int|null $lockMode
* @param ?int $lockVersion
*
* @return ?T
Expand Down
100 changes: 100 additions & 0 deletions tests/acceptance/Connection.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
Feature: Connection
In order to use Doctrine DBAL Connection safely
As a Psalm user
I need Psalm to typecheck the parameter types of Connection's query methods

Background:
Given I have Doctrine plugin enabled
And I have the following code preamble
"""
<?php
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Type;

/**
* @psalm-suppress InvalidReturnType
* @return Connection
* @psalm-capabilities read-props
*/
function connection() {}

/**
* @psalm-suppress InvalidReturnType
* @return Type
* @psalm-capabilities read-props
*/
function type() {}

"""
# Psalm enables cache when there's a composer.lock file
And I have empty composer.lock

@Connection::executeQuery
@Connection::executeStatement
@Connection::fetchAssociative
@Connection::fetchNumeric
@Connection::fetchOne
@Connection::fetchAllNumeric
@Connection::fetchAllAssociative
@Connection::fetchAllKeyValue
@Connection::fetchAllAssociativeIndexed
@Connection::fetchFirstColumn
@Connection::iterateNumeric
@Connection::iterateAssociative
@Connection::iterateKeyValue
@Connection::iterateAssociativeIndexed
@Connection::iterateColumn
Scenario Outline: Passing named parameter types to Connection's query methods
Given I have the "doctrine/dbal" package satisfying the ">= 4.0"
And I have the following code
"""
connection()-><method>(
'SELECT a FROM t WHERE a = :a AND b IN (:b) AND c = :c AND d = :d',
['a' => 1, 'b' => [1, 2], 'c' => 'x', 'd' => 'y'],
['a' => ParameterType::INTEGER, 'b' => ArrayParameterType::INTEGER, 'c' => 'string', 'd' => type()],
);
"""
When I run Psalm
Then I see no errors
Examples:
| method |
| executeQuery |
| executeStatement |
| fetchAssociative |
| fetchNumeric |
| fetchOne |
| fetchAllNumeric |
| fetchAllAssociative |
| fetchAllKeyValue |
| fetchAllAssociativeIndexed |
| fetchFirstColumn |
| iterateNumeric |
| iterateAssociative |
| iterateKeyValue |
| iterateAssociativeIndexed |
| iterateColumn |

@Connection::fetchOne
Scenario: Passing positional parameter types to Connection's query methods
Given I have the "doctrine/dbal" package satisfying the ">= 4.0"
And I have the following code
"""
connection()->fetchOne('SELECT a FROM t WHERE a = ? AND b = ?', [1, 'x'], [ParameterType::INTEGER, 'string']);
"""
When I run Psalm
Then I see no errors

@Connection::fetchOne
Scenario: Passing an invalid parameter type to Connection's query methods
Given I have the "doctrine/dbal" package satisfying the ">= 4.0"
And I have the following code
"""
connection()->fetchOne('SELECT a FROM t WHERE a = :a', ['a' => 1], ['a' => 1.5]);
"""
When I run Psalm
Then I see these errors
| Type | Message |
| InvalidArgument | /Argument 3 of .*Connection::fetchOne expects .*, but array{a: float\(1\.5\)} provided/ |
And I see no other errors
25 changes: 25 additions & 0 deletions tests/acceptance/EntityManager.feature
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,28 @@ Feature: EntityManager
"""
When I run Psalm
Then I see no errors

@EntityManager::find
Scenario: Finding an entity with a lock mode
Given I have the "doctrine/orm" package satisfying the ">= 3.0"
And I have the following code
"""
atan(em()->find(I::class, 1, \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE, 1));
"""
When I run Psalm
Then I see these errors
| Type | Message |
| InvalidArgument | Argument 1 of atan expects float, but I\|null provided |
And I see no other errors

@EntityManager::refresh
Scenario: Refreshing an entity with a lock mode
Given I have the "doctrine/orm" package satisfying the ">= 3.0"
And I have the following code
"""
function f(I $entity): void {
em()->refresh($entity, \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE);
}
"""
When I run Psalm
Then I see no errors
27 changes: 27 additions & 0 deletions tests/acceptance/EntityManagerInterface.feature
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,30 @@ Feature: EntityManagerInterface
| Type | Message |
| InvalidScalarArgument | Argument 1 of Doctrine\ORM\EntityManagerInterface::getReference expects class-string, but 123 provided |
And I see no other errors

@EntityManagerInterface::find
Scenario: Finding an entity with a lock mode
Given I have the "doctrine/orm" package satisfying the ">= 3.0"
And I have the following code
"""
function f(EntityManagerInterface $em): void {
atan($em->find(I::class, 1, \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE, 1));
}
"""
When I run Psalm
Then I see these errors
| Type | Message |
| InvalidArgument | Argument 1 of atan expects float, but I\|null provided |
And I see no other errors

@EntityManagerInterface::refresh
Scenario: Refreshing an entity with a lock mode
Given I have the "doctrine/orm" package satisfying the ">= 3.0"
And I have the following code
"""
function f(EntityManagerInterface $em, I $entity): void {
$em->refresh($entity, \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE);
}
"""
When I run Psalm
Then I see no errors
13 changes: 13 additions & 0 deletions tests/acceptance/EntityRepository.feature
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,16 @@ Feature: EntityRepository
"""
When I run Psalm
Then I see no errors

@EntityRepository::find
Scenario: Finding an entity with a lock mode
Given I have the "doctrine/orm" package satisfying the ">= 3.0"
And I have the following code
"""
atan(repo(I::class)->find(1, \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE, 1));
"""
When I run Psalm
Then I see these errors
| Type | Message |
| InvalidArgument | Argument 1 of atan expects float, but I\|null provided |
And I see no other errors
Loading