Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/Rules/PdoStatementExecuteMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function processNode(Node $methodCall, Scope $scope): array
return [];
}

if (PDOStatement::class !== $methodReflection->getDeclaringClass()->getName()) {
if (!(new \PHPStan\Type\ObjectType(PDOStatement::class))->isSuperTypeOf($scope->getType($methodCall->var))->yes()) {
return [];
}

Expand Down
13 changes: 13 additions & 0 deletions tests/default/Fixture/MyPdoStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace staabm\PHPStanDba\Tests\Fixture;

class MyPdoStatement extends \PDOStatement
{
public function execute(?array $params = null): bool
{
return parent::execute($params);
}
}
18 changes: 18 additions & 0 deletions tests/rules/PdoStatementExecuteMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,22 @@ public function testPlaceholderBug(): void
],
]);
}

public function testSubclassedPdoStatement(): void
{
$this->analyse([__DIR__ . '/data/pdo-stmt-execute-subclassed.php'], [
[
'Query expects placeholder :adaid, but it is missing from values given.',
14,
],
[
'Value :wrongParamName is given, but the query does not contain this placeholder.',
14,
],
[
'Query expects placeholder :adaid, but it is missing from values given.',
18,
],
]);
}
}
27 changes: 27 additions & 0 deletions tests/rules/data/pdo-stmt-execute-subclassed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace PdoStatementExecuteSubclassedMethodRuleTest;

use PDO;
use staabm\PHPStanDba\Tests\Fixture\MyPdoStatement;

class Foo
{
public function errors(PDO $pdo): void
{
$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE adaid = :adaid');
assert($stmt instanceof MyPdoStatement);
$stmt->execute(['wrongParamName' => 1]); // error: wrong param name

$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE adaid = :adaid');
assert($stmt instanceof MyPdoStatement);
$stmt->execute(); // error: missing parameter
}

public function noErrors(PDO $pdo): void
{
$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE adaid = :adaid');
assert($stmt instanceof MyPdoStatement);
$stmt->execute(['adaid' => 1]); // correct
}
}
Loading