Skip to content

Commit cefdc85

Browse files
staabmclxmstaab
andauthored
error message improvements (#63)
Co-authored-by: Markus Staab <[email protected]>
1 parent b4b72e2 commit cefdc85

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/Rules/PdoStatementExecuteErrorMethodRule.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,13 @@ private function checkErrors(MethodReflection $methodReflection, MethodCall $met
7676
return [];
7777
}
7878

79+
$placeholderExpectation = sprintf('Query expects %s placeholder', $placeholderCount);
80+
if ($placeholderCount > 1) {
81+
$placeholderExpectation = sprintf('Query expects %s placeholders', $placeholderCount);
82+
}
83+
7984
return [
80-
RuleErrorBuilder::message(sprintf('Query expects %s placeholders, but no values are given to execute().', $placeholderCount))->line($methodCall->getLine())->build(),
85+
RuleErrorBuilder::message(sprintf($placeholderExpectation.', but no values are given to execute().', $placeholderCount))->line($methodCall->getLine())->build(),
8186
];
8287
}
8388

@@ -100,14 +105,18 @@ private function checkParameterValues(MethodCall $methodCall, Scope $scope, stri
100105
$parameterCount = \count($parameters);
101106

102107
if ($parameterCount !== $placeholderCount) {
103-
if (1 === $parameterCount) {
104-
return [
105-
RuleErrorBuilder::message(sprintf('Query expects %s placeholders, but %s value is given to execute().', $placeholderCount, $parameterCount))->line($methodCall->getLine())->build(),
106-
];
108+
$placeholderExpectation = sprintf('Query expects %s placeholder', $placeholderCount);
109+
if ($placeholderCount > 1) {
110+
$placeholderExpectation = sprintf('Query expects %s placeholders', $placeholderCount);
111+
}
112+
113+
$parameterActual = sprintf('but %s value is given to execute()', $parameterCount);
114+
if ($parameterCount > 1) {
115+
$parameterActual = sprintf('but %s values are given to execute()', $parameterCount);
107116
}
108117

109118
return [
110-
RuleErrorBuilder::message(sprintf('Query expects %s placeholders, but %s values are given to execute().', $placeholderCount, $parameterCount))->line($methodCall->getLine())->build(),
119+
RuleErrorBuilder::message($placeholderExpectation.', '.$parameterActual.'.')->line($methodCall->getLine())->build(),
111120
];
112121
}
113122

@@ -121,7 +130,7 @@ private function checkParameterValues(MethodCall $methodCall, Scope $scope, stri
121130

122131
foreach ($parameters as $placeholderKey => $value) {
123132
if (!\in_array($placeholderKey, $namedPlaceholders)) {
124-
$errors[] = RuleErrorBuilder::message(sprintf('Value %s is given to execute(), but the query does not containt this placeholder.', $placeholderKey))->line($methodCall->getLine())->build();
133+
$errors[] = RuleErrorBuilder::message(sprintf('Value %s is given to execute(), but the query does not contain this placeholder.', $placeholderKey))->line($methodCall->getLine())->build();
125134
}
126135
}
127136

tests/PdoStatementExecuteErrorMethodRuleTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected function getRule(): Rule
1616
return $this->getRuleFromConfig(PdoStatementExecuteErrorMethodRule::class, __DIR__.'/../config/dba.neon');
1717
}
1818

19-
public function testSyntaxErrorInQueryRule(): void
19+
public function testParameterErrors(): void
2020
{
2121
require_once __DIR__.'/data/pdo-stmt-execute-error.php';
2222

@@ -26,15 +26,15 @@ public function testSyntaxErrorInQueryRule(): void
2626
12,
2727
],
2828
[
29-
'Value :wrongParamName is given to execute(), but the query does not containt this placeholder.',
29+
'Value :wrongParamName is given to execute(), but the query does not contain this placeholder.',
3030
12,
3131
],
3232
[
3333
'Query expects placeholder :adaid, but it is missing from values given to execute().',
3434
15,
3535
],
3636
[
37-
'Value :wrongParamName is given to execute(), but the query does not containt this placeholder.',
37+
'Value :wrongParamName is given to execute(), but the query does not contain this placeholder.',
3838
15,
3939
],
4040
/*
@@ -43,12 +43,12 @@ public function testSyntaxErrorInQueryRule(): void
4343
18,
4444
],
4545
[
46-
'Value :wrongParamValue is given to execute(), but the query does not containt this placeholder.',
46+
'Value :wrongParamValue is given to execute(), but the query does not contain this placeholder.',
4747
18,
4848
],
4949
*/
5050
[
51-
'Query expects 1 placeholders, but no values are given to execute().',
51+
'Query expects 1 placeholder, but no values are given to execute().',
5252
21,
5353
],
5454
[

0 commit comments

Comments
 (0)