Skip to content

Commit

Permalink
Added rule identifiers (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Nov 7, 2024
1 parent 61a6233 commit a152c34
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/Rules/DoctrineKeyValueStyleRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\IntegerRangeType;
Expand Down Expand Up @@ -65,7 +65,7 @@ public function getNodeType(): string
}

/**
* @return RuleError[]
* @return list<IdentifierRuleError>
*/
public function processNode(Node $callLike, Scope $scope): array
{
Expand Down Expand Up @@ -114,7 +114,7 @@ public function processNode(Node $callLike, Scope $scope): array
$tableNames = $tableType->getConstantStrings();
if (\count($tableNames) === 0) {
return [
RuleErrorBuilder::message('Argument #0 expects a constant string, got ' . $tableType->describe(VerbosityLevel::precise()))->line($callLike->getStartLine())->build(),
RuleErrorBuilder::message('Argument #0 expects a constant string, got ' . $tableType->describe(VerbosityLevel::precise()))->identifier('dba.keyValue')->line($callLike->getStartLine())->build(),
];
}

Expand Down Expand Up @@ -185,7 +185,7 @@ public function processNode(Node $callLike, Scope $scope): array

$ruleErrors = [];
foreach ($errors as $error) {
$ruleErrors[] = RuleErrorBuilder::message('Query error: ' . $error)->line($callLike->getStartLine())->build();
$ruleErrors[] = RuleErrorBuilder::message('Query error: ' . $error)->identifier('dba.keyValue')->line($callLike->getStartLine())->build();
}
return $ruleErrors;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Rules/PdoStatementExecuteMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand Down Expand Up @@ -55,7 +55,7 @@ public function processNode(Node $methodCall, Scope $scope): array
}

/**
* @return RuleError[]
* @return list<IdentifierRuleError>
*/
private function checkErrors(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): array
{
Expand Down Expand Up @@ -97,7 +97,7 @@ private function checkErrors(MethodReflection $methodReflection, MethodCall $met
$parameters = $queryReflection->resolveParameters($parameterTypes) ?? [];
} catch (UnresolvableQueryException $exception) {
return [
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->line($methodCall->getStartLine())->build(),
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->identifier('dba.unresolvableQuery')->line($methodCall->getStartLine())->build(),
];
}

Expand All @@ -110,13 +110,13 @@ private function checkErrors(MethodReflection $methodReflection, MethodCall $met
}
} catch (UnresolvableQueryException $exception) {
return [
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->line($methodCall->getStartLine())->build(),
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->identifier('dba.unresolvableQuery')->line($methodCall->getStartLine())->build(),
];
}

$ruleErrors = [];
foreach ($errors as $error) {
$ruleErrors[] = RuleErrorBuilder::message($error)->line($methodCall->getStartLine())->build();
$ruleErrors[] = RuleErrorBuilder::message($error)->identifier('dba.invalidPlaceholder')->line($methodCall->getStartLine())->build();
}

return $ruleErrors;
Expand Down
9 changes: 6 additions & 3 deletions src/Rules/QueryPlanAnalyzerRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -92,15 +92,15 @@ public function processNode(Node $callLike, Scope $scope): array
return $this->analyze($callLike, $scope);
} catch (UnresolvableQueryException $exception) {
return [
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->line($callLike->getStartLine())->build(),
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->identifier('dba.unresolvableQuery')->line($callLike->getStartLine())->build(),
];
}
}

/**
* @param MethodCall|New_ $callLike
*
* @return RuleError[]
* @return list<IdentifierRuleError>
*/
private function analyze(CallLike $callLike, Scope $scope): array
{
Expand Down Expand Up @@ -144,6 +144,7 @@ private function analyze(CallLike $callLike, Scope $scope): array
$table
)
)
->identifier('dba.missingIndex')
->line($callLike->getStartLine())
->tip('see Mysql Docs https://dev.mysql.com/doc/refman/8.0/en/select-optimization.html')
->build();
Expand All @@ -156,6 +157,7 @@ private function analyze(CallLike $callLike, Scope $scope): array
$table
)
)
->identifier('dba.tableScan')
->line($callLike->getStartLine())
->tip('see Mysql Docs https://dev.mysql.com/doc/refman/8.0/en/table-scan-avoidance.html')
->build();
Expand All @@ -168,6 +170,7 @@ private function analyze(CallLike $callLike, Scope $scope): array
$table
)
)
->identifier('dba.unindexedReads')
->line($callLike->getStartLine())
->tip('see Mysql Docs https://dev.mysql.com/doc/refman/8.0/en/select-optimization.html')
->build();
Expand Down
12 changes: 6 additions & 6 deletions src/Rules/SyntaxErrorInDibiPreparedStatementMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantArrayType;
Expand Down Expand Up @@ -92,7 +92,7 @@ public function processNode(Node $callLike, Scope $scope): array
/**
* @param MethodCall|New_ $callLike
*
* @return RuleError[]
* @return list<IdentifierRuleError>
*/
private function checkErrors(CallLike $callLike, Scope $scope, MethodReflection $methodReflection): array
{
Expand Down Expand Up @@ -171,7 +171,7 @@ private function checkErrors(CallLike $callLike, Scope $scope, MethodReflection
}

return [
RuleErrorBuilder::message($placeholderExpectation . ', ' . $parameterActual . '.')->line($callLike->getStartLine())->build(),
RuleErrorBuilder::message($placeholderExpectation . ', ' . $parameterActual . '.')->identifier('dba.placeholderMismatch')->line($callLike->getStartLine())->build(),
];
}
}
Expand All @@ -196,7 +196,7 @@ private function checkErrors(CallLike $callLike, Scope $scope, MethodReflection
$validity = $queryReflection->validateQueryString($queryParameters[0]);

if (null !== $validity) {
return [RuleErrorBuilder::message($validity->asRuleMessage())->line($callLike->getStartLine())->build()];
return [RuleErrorBuilder::message($validity->asRuleMessage())->identifier('dba.syntaxError')->line($callLike->getStartLine())->build()];
}

$result = $queryReflection->getResultType($queryParameters[0], QueryReflector::FETCH_TYPE_BOTH);
Expand All @@ -206,11 +206,11 @@ private function checkErrors(CallLike $callLike, Scope $scope, MethodReflection
$columnsInResult = \count($result->getValueTypes()) / 2;

if ('fetchPairs' === $methodReflection->getName() && 2 !== $columnsInResult) {
return [RuleErrorBuilder::message('fetchPairs requires exactly 2 selected columns, got ' . $columnsInResult . '.')->line($callLike->getStartLine())->build()];
return [RuleErrorBuilder::message('fetchPairs requires exactly 2 selected columns, got ' . $columnsInResult . '.')->identifier('dba.error')->line($callLike->getStartLine())->build()];
}

if ('fetchSingle' === $methodReflection->getName() && 1 !== $columnsInResult) {
return [RuleErrorBuilder::message('fetchSingle requires exactly 1 selected column, got ' . $columnsInResult . '.')->line($callLike->getStartLine())->build()];
return [RuleErrorBuilder::message('fetchSingle requires exactly 1 selected column, got ' . $columnsInResult . '.')->identifier('dba.error')->line($callLike->getStartLine())->build()];
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Rules/SyntaxErrorInPreparedStatementMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -89,7 +89,7 @@ public function processNode(Node $callLike, Scope $scope): array
/**
* @param MethodCall|New_ $callLike
*
* @return RuleError[]
* @return list<IdentifierRuleError>
*/
private function checkErrors(CallLike $callLike, Scope $scope): array
{
Expand All @@ -113,7 +113,7 @@ private function checkErrors(CallLike $callLike, Scope $scope): array
$parameters = $queryReflection->resolveParameters($parameterTypes) ?? [];
} catch (UnresolvableQueryException $exception) {
return [
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->line($callLike->getStartLine())->build(),
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->identifier('dba.unresolvableQuery')->line($callLike->getStartLine())->build(),
];
}
}
Expand Down Expand Up @@ -144,13 +144,13 @@ private function checkErrors(CallLike $callLike, Scope $scope): array

$ruleErrors = [];
foreach ($errors as $error) {
$ruleErrors[] = RuleErrorBuilder::message($error)->line($callLike->getStartLine())->build();
$ruleErrors[] = RuleErrorBuilder::message($error)->identifier('dba.syntaxError')->line($callLike->getStartLine())->build();
}

return $ruleErrors;
} catch (UnresolvableQueryException $exception) {
return [
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->line($callLike->getStartLine())->build(),
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->identifier('dba.unresolvableQuery')->line($callLike->getStartLine())->build(),
];
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/SyntaxErrorInQueryFunctionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ public function processNode(Node $node, Scope $scope): array
$queryError = $queryReflection->validateQueryString($queryString);
if (null !== $queryError) {
return [
RuleErrorBuilder::message($queryError->asRuleMessage())->line($node->getStartLine())->build(),
RuleErrorBuilder::message($queryError->asRuleMessage())->identifier('dba.syntaxError')->line($node->getStartLine())->build(),
];
}
}
} catch (UnresolvableQueryException $exception) {
return [
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->line($node->getStartLine())->build(),
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->identifier('dba.unresolvableQuery')->line($node->getStartLine())->build(),
];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Rules/SyntaxErrorInQueryMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ public function processNode(Node $node, Scope $scope): array
$queryError = $queryReflection->validateQueryString($queryString);
if (null !== $queryError) {
return [
RuleErrorBuilder::message($queryError->asRuleMessage())->line($node->getStartLine())->build(),
RuleErrorBuilder::message($queryError->asRuleMessage())->identifier('dba.syntaxError')->line($node->getStartLine())->build(),
];
}
}
} catch (UnresolvableQueryException $exception) {
return [
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->line($node->getStartLine())->build(),
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->identifier('dba.unresolvableQuery')->line($node->getStartLine())->build(),
];
}

Expand Down

0 comments on commit a152c34

Please sign in to comment.