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
35 changes: 30 additions & 5 deletions src/Rules/DoctrineKeyValueStyleRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
Expand Down Expand Up @@ -109,13 +111,36 @@ public function processNode(Node $callLike, Scope $scope): array
return [];
}

$args = $callLike->getArgs();
// Reorder arguments to account for named parameters
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$callLike->getArgs(),
$methodReflection->getVariants(),
$methodReflection->getNamedArgumentsVariants(),
);

if (\count($args) < 1) {
if ($callLike instanceof MethodCall) {
$reorderedCall = ArgumentsNormalizer::reorderMethodArguments(
$parametersAcceptor,
$callLike,
);
} else {
$reorderedCall = ArgumentsNormalizer::reorderNewArguments(
$parametersAcceptor,
$callLike,
);
}

if ($reorderedCall === null) {
return [];
}
$reorderedArgs = $reorderedCall->getArgs();

if (\count($reorderedArgs) < 1) {
return [];
}

$tableExpr = $args[0]->value;
$tableExpr = $reorderedArgs[0]->value;
$tableType = $scope->getType($tableExpr);
$tableNames = $tableType->getConstantStrings();
if (\count($tableNames) === 0) {
Expand Down Expand Up @@ -143,11 +168,11 @@ public function processNode(Node $callLike, Scope $scope): array
foreach ($arrayArgPositions as $arrayArgPosition) {
// If the argument doesn't exist, just skip it since we don't want
// to error in case it has a default value
if (! \array_key_exists($arrayArgPosition, $args)) {
if (! \array_key_exists($arrayArgPosition, $reorderedArgs)) {
continue;
}

$argType = $scope->getType($args[$arrayArgPosition]->value);
$argType = $scope->getType($reorderedArgs[$arrayArgPosition]->value);
$argArrays = $argType->getConstantArrays();
if (\count($argArrays) === 0) {
$errors[] = 'Argument #' . $arrayArgPosition . ' is not a constant array, got ' . $argType->describe(VerbosityLevel::precise());
Expand Down
14 changes: 14 additions & 0 deletions tests/rules/DoctrineKeyValueStyleRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,18 @@ public function testLaxIntegerRanges(): void
{
$this->analyse([__DIR__ . '/data/doctrine-key-value-style-integer-ranges.php'], []);
}

public function testNamedParameters(): void
{
if (PHP_VERSION_ID < 80000) {
self::markTestSkipped('Test requires PHP 8.0');
}

$this->analyse([__DIR__ . '/data/doctrine-key-value-style-named-parameters.php'], [
[
'Query error: Column "ada.not_a_column" does not exist',
16,
],
]);
}
}
18 changes: 18 additions & 0 deletions tests/rules/data/doctrine-key-value-style-named-parameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php // lint >= 8.0

namespace DoctrineKeyValueStyleRuleTest;

use staabm\PHPStanDba\Tests\Fixture\Connection;

class Foo
{
public function noErrorWithNamedParameters(Connection $conn)
{
$conn->assembleOneArray(cols: ['email' => 'foo'], tableName: 'ada');
}

public function errorWithNamedParameters(Connection $conn)
{
$conn->assembleOneArray(cols: ['not_a_column' => 'foo'], tableName: 'ada');
}
}
Loading