Skip to content

DBAL Prepared statement not properly narrowing type? #762

@DjordyKoert

Description

@DjordyKoert

Using DBAL's (4.x) prepare() method does not seem to properly narrow the result type.

#197 shows that the prepare method is properly supported, so I'm not entirely sure why this isn't working

public function temp(): void
    {
        $sql = <<<SQL
        SELECT
            product.productId
        FROM
            product
        SQL;

        $data = $this->connection->fetchAssociative($sql);

        \PHPStan\dumpType($data); // Works fine

        $stmt = $this->connection->prepare($sql);
        $result = $stmt->executeQuery();
        $data = $result->fetchAssociative();

        \PHPStan\dumpType($stmt);
        \PHPStan\dumpType($result);
        \PHPStan\dumpType($data);
    }
  143    Dumped type: array{productId: int<0, 4294967295>}|false             
  149    Dumped type: Doctrine\DBAL\Statement                                  
  150    Dumped type: Doctrine\DBAL\Result                                     
  151    Dumped type: array<string, mixed>|false
<?php // phpstan-dba-bootstrap.php

use staabm\PHPStanDba\QueryReflection\PdoMysqlQueryReflector;
use staabm\PHPStanDba\QueryReflection\RuntimeConfiguration;
use staabm\PHPStanDba\QueryReflection\QueryReflection;
use Webmozart\Assert\Assert;
use YourSurprise\CRM\Kernel;

require_once __DIR__ . '/vendor/autoload.php';

$kernel = new Kernel($_ENV['APP_ENV'], (bool)($_ENV['APP_DEBUG'] ?? false));
$kernel->boot();

$pdo = $kernel->getContainer()->get('database_connection')->getNativeConnection();

Assert::isInstanceOf($pdo, \PDO::class);

$config = new RuntimeConfiguration()
    ->debugMode(true)
    ->analyzeWriteQueries(true);

$reflector = new PdoMysqlQueryReflector($pdo);

QueryReflection::setupReflector(
    $reflector,
    $config
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions