Skip to content

Commit 899d5b8

Browse files
staabmclxmstaab
andauthored
moved countPlaceholders() and extractNamedPlaceholders() into QueryReflection (#118)
Co-authored-by: Markus Staab <[email protected]>
1 parent 786157f commit 899d5b8

File tree

2 files changed

+43
-42
lines changed

2 files changed

+43
-42
lines changed

src/QueryReflection/QueryReflection.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node\Expr;
88
use PhpParser\Node\Expr\BinaryOp\Concat;
99
use PHPStan\Analyser\Scope;
10+
use PHPStan\ShouldNotHappenException;
1011
use PHPStan\Type\Constant\ConstantArrayType;
1112
use PHPStan\Type\Constant\ConstantIntegerType;
1213
use PHPStan\Type\Constant\ConstantStringType;
@@ -16,6 +17,8 @@
1617

1718
final class QueryReflection
1819
{
20+
public const REGEX_PLACEHOLDER = '{:[a-zA-Z0-9_]+}';
21+
1922
/**
2023
* @var QueryReflector|null
2124
*/
@@ -182,4 +185,42 @@ public static function getRuntimeConfiguration(): RuntimeConfiguration
182185

183186
return self::$runtimeConfiguration;
184187
}
188+
189+
/**
190+
* @return 0|positive-int
191+
*/
192+
public function countPlaceholders(string $queryString): int
193+
{
194+
$numPlaceholders = substr_count($queryString, '?');
195+
196+
if (0 !== $numPlaceholders) {
197+
return $numPlaceholders;
198+
}
199+
200+
$numPlaceholders = preg_match_all(self::REGEX_PLACEHOLDER, $queryString);
201+
if (false === $numPlaceholders || $numPlaceholders < 0) {
202+
throw new ShouldNotHappenException();
203+
}
204+
205+
return $numPlaceholders;
206+
}
207+
208+
/**
209+
* @return list<string>
210+
*/
211+
public function extractNamedPlaceholders(string $queryString): array
212+
{
213+
// pdo does not support mixing of named and '?' placeholders
214+
$numPlaceholders = substr_count($queryString, '?');
215+
216+
if (0 !== $numPlaceholders) {
217+
return [];
218+
}
219+
220+
if (preg_match_all(self::REGEX_PLACEHOLDER, $queryString, $matches) > 0) {
221+
return $matches[0];
222+
}
223+
224+
return [];
225+
}
185226
}

src/Rules/PdoStatementExecuteMethodRule.php

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424
final class PdoStatementExecuteMethodRule implements Rule
2525
{
26-
public const REGEX_PLACEHOLDER = '{:[a-zA-Z0-9_]+}';
27-
2826
public function getNodeType(): string
2927
{
3028
return MethodCall::class;
@@ -71,7 +69,7 @@ private function checkErrors(MethodReflection $methodReflection, MethodCall $met
7169
}
7270

7371
$args = $methodCall->getArgs();
74-
$placeholderCount = $this->countPlaceholders($queryString);
72+
$placeholderCount = $queryReflection->countPlaceholders($queryString);
7573

7674
if (0 === \count($args)) {
7775
if (0 === $placeholderCount) {
@@ -123,7 +121,7 @@ private function checkParameterValues(MethodCall $methodCall, Scope $scope, stri
123121
}
124122

125123
$errors = [];
126-
$namedPlaceholders = $this->extractNamedPlaceholders($queryString);
124+
$namedPlaceholders = $queryReflection->extractNamedPlaceholders($queryString);
127125
if (\count($namedPlaceholders) > 0) {
128126
foreach ($namedPlaceholders as $namedPlaceholder) {
129127
if (!\array_key_exists($namedPlaceholder, $parameters)) {
@@ -140,42 +138,4 @@ private function checkParameterValues(MethodCall $methodCall, Scope $scope, stri
140138

141139
return $errors;
142140
}
143-
144-
/**
145-
* @return 0|positive-int
146-
*/
147-
private function countPlaceholders(string $queryString): int
148-
{
149-
$numPlaceholders = substr_count($queryString, '?');
150-
151-
if (0 !== $numPlaceholders) {
152-
return $numPlaceholders;
153-
}
154-
155-
$numPlaceholders = preg_match_all(self::REGEX_PLACEHOLDER, $queryString);
156-
if (false === $numPlaceholders || $numPlaceholders < 0) {
157-
throw new ShouldNotHappenException();
158-
}
159-
160-
return $numPlaceholders;
161-
}
162-
163-
/**
164-
* @return list<string>
165-
*/
166-
private function extractNamedPlaceholders(string $queryString): array
167-
{
168-
// pdo does not support mixing of named and '?' placeholders
169-
$numPlaceholders = substr_count($queryString, '?');
170-
171-
if (0 !== $numPlaceholders) {
172-
return [];
173-
}
174-
175-
if (preg_match_all(self::REGEX_PLACEHOLDER, $queryString, $matches) > 0) {
176-
return $matches[0];
177-
}
178-
179-
return [];
180-
}
181141
}

0 commit comments

Comments
 (0)