Skip to content

Commit c614e71

Browse files
staabmclxmstaab
andauthored
don't error on unknown types (#89)
Co-authored-by: Markus Staab <[email protected]>
1 parent 6bc277e commit c614e71

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

phpstan.neon.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ parameters:
1919
-
2020
message: '#.*has no return type specified.#'
2121
path: tests/*
22+
-
23+
message: '#.*with no type specified.#'
24+
path: tests/*
2225
-
2326
message: '#.*return type has no value type specified in iterable type iterable.#'
2427
path: tests/*

src/QueryReflection/MysqliQueryReflector.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,15 @@ public function validateQueryString(string $queryString): ?Error
7676
return null;
7777
} catch (mysqli_sql_exception $e) {
7878
if (\in_array($e->getCode(), [self::MYSQL_SYNTAX_ERROR_CODE, self::MYSQL_UNKNOWN_COLUMN_IN_FIELDLIST, self::MYSQL_UNKNOWN_TABLE], true)) {
79-
8079
$message = $e->getMessage();
8180

8281
// make error string consistent across mysql/mariadb
8382
$message = str_replace(' MySQL server', ' MySQL/MariaDB server', $message);
8483
$message = str_replace(' MariaDB server', ' MySQL/MariaDB server', $message);
8584

8685
// to ease debugging, print the error we simulated
87-
if ($e->getCode() === self::MYSQL_SYNTAX_ERROR_CODE && QueryReflection::getRuntimeConfiguration()->isDebugEnabled()) {
88-
$message = $message ."\n\nSimulated query: ". $simulatedQuery;
86+
if (self::MYSQL_SYNTAX_ERROR_CODE === $e->getCode() && QueryReflection::getRuntimeConfiguration()->isDebugEnabled()) {
87+
$message = $message."\n\nSimulated query: ".$simulatedQuery;
8988
}
9089

9190
return new Error($message, $e->getCode());

src/QueryReflection/QueryReflection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,14 @@ public function resolveParameters(Type $parameterTypes): ?array
168168

169169
if ($valueTypes[$i] instanceof ConstantScalarType) {
170170
$parameters[$placeholderName] = $valueTypes[$i]->getValue();
171+
} else {
172+
return null;
171173
}
172174
} elseif ($keyType instanceof ConstantIntegerType) {
173175
if ($valueTypes[$i] instanceof ConstantScalarType) {
174176
$parameters[$keyType->getValue()] = $valueTypes[$i]->getValue();
177+
} else {
178+
return null;
175179
}
176180
}
177181
}

src/QueryReflection/RuntimeConfiguration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public function debugMode(bool $mode): self
4242
return $this;
4343
}
4444

45-
public function isDebugEnabled():bool {
45+
public function isDebugEnabled(): bool
46+
{
4647
return $this->debugMode;
4748
}
4849

src/Rules/SyntaxErrorInPreparedStatementMethodRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
use PhpParser\Node\Expr\CallLike;
99
use PhpParser\Node\Expr\MethodCall;
1010
use PhpParser\Node\Expr\New_;
11-
use PhpParser\Node\Name;
1211
use PhpParser\Node\Name\FullyQualified;
1312
use PHPStan\Analyser\Scope;
1413
use PHPStan\Rules\Rule;
1514
use PHPStan\Rules\RuleError;
1615
use PHPStan\Rules\RuleErrorBuilder;
1716
use PHPStan\Type\ObjectType;
18-
use Prophecy\Call\Call;
1917
use staabm\PHPStanDba\QueryReflection\QueryReflection;
2018

2119
/**
@@ -83,6 +81,7 @@ public function processNode(Node $callLike, Scope $scope): array
8381

8482
/**
8583
* @param MethodCall|New_ $callLike
84+
*
8685
* @return RuleError[]
8786
*/
8887
private function checkErrors(CallLike $callLike, Scope $scope): array

tests/data/syntax-error-in-prepared-statement.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public function syntaxErrorInConstruct()
1717
$stmt = new PreparedStatement('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada', []);
1818
}
1919

20+
public function noErrorOnMixedParams(Connection $connection, $unknownType)
21+
{
22+
$connection->preparedQuery('
23+
SELECT email, adaid
24+
FROM ada
25+
WHERE gesperrt = ? AND email LIKE ?
26+
LIMIT 1
27+
', [1, $unknownType]);
28+
}
29+
2030
public function preparedParams(Connection $connection)
2131
{
2232
$connection->preparedQuery('SELECT email, adaid FROM ada WHERE gesperrt = ?', [1]);

0 commit comments

Comments
 (0)