phpstan / phpstan-doctrine

Doctrine extensions for PHPStan
MIT License
590 stars 97 forks source link

Incompatible bleedingEdge Connection stub typehint with DBAL 4 #559

Closed trearcul closed 5 months ago

trearcul commented 5 months ago

Hi,

I hope I'm not creating duplicity here, but I've just run into an incompatibility with the bleeding-edge stub of DBAL\Connection added in https://github.com/phpstan/phpstan-doctrine/pull/546. This issue arises with DBAL 4, leading to false positives when an array of ParameterType|ArrayParameterType enums is passed as the $types argument of Connection::execute* methods.

E.g

namespace Vendor\App;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;

final readonly class DbalFooFinder
{
    public function __construct(private Connection $connection)
    {
    }

    /** @return list<array{bar: string, baz: string}> */
    public function find(string $baz): array
    {
        /** @var list<array{bar: string, baz: string}> $result */
        $result = $this->connection
            ->executeQuery(
                'SELECT bar, baz FROM foo WHERE baz = :baz',
                ['baz' => $baz],
                ['baz' => ParameterType::STRING],
            )
            ->fetchAllAssociative();

        return $result;
    }
}

Results in

 ------ --------------------------------------------------------------------------------
  Line   src/DbalFooFinder.php
 ------ --------------------------------------------------------------------------------
  23     Parameter #3 $types of method Doctrine\DBAL\Connection::executeQuery() expects
         array<int|string, Doctrine\DBAL\Types\Type|int|string|null>, array<string,
         Doctrine\DBAL\ParameterType::STRING> given.
 ------ --------------------------------------------------------------------------------

Even though the DBAL 4's typehint for $types parameter is

/**
 * @psalm-type WrapperParameterType = string|Type|ParameterType|ArrayParameterType
 * @psalm-type WrapperParameterTypeArray = array<int<0, max>, WrapperParameterType>|array<string, WrapperParameterType>
 */

/**
 * @psalm-param WrapperParameterTypeArray $types
 */
ondrejmirtes commented 5 months ago

/cc @VincentLanglet Please solve this, it's a consequence of https://github.com/phpstan/phpstan-doctrine/pull/546

VincentLanglet commented 5 months ago

HI @trearcul, can you try #561 ?

trearcul commented 5 months ago

Hi @VincentLanglet, awesome, your fix works with ParameterType enums like charm. Thanks for fast response from both of you! ❤️

ondrejmirtes commented 5 months ago

Fixed by https://github.com/phpstan/phpstan-doctrine/pull/561 and released as 1.3.67.

github-actions[bot] commented 4 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.