Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This extension provides following features:
* `PDO->query` knows the array shape of the returned results and therefore can return a generic `PDOStatement`
* `mysqli->query` knows the array shape of the returned results and therefore can return a generic `mysqli_result`
* `SyntaxErrorInQueryMethodRule` can inspect sql queries and detect syntax errors - `SyntaxErrorInQueryFunctionRule` can do the same for functions
* `mysqli_real_escape_string` and `mysqli->real_escape_string` dynamic return type extensions

[see the unit-testsuite](https://github.com/staabm/phpstan-dba/tree/main/tests/data) to get a feeling about the current featureset.

Expand Down
6 changes: 6 additions & 0 deletions config/extensions.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ services:
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: staabm\PHPStanDba\Extensions\MysqliEscapeStringDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
- phpstan.broker.dynamicFunctionReturnTypeExtension
80 changes: 80 additions & 0 deletions src/Extensions/MysqliEscapeStringDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace staabm\PHPStanDba\Extensions;

use mysqli;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;

final class MysqliEscapeStringDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension, DynamicFunctionReturnTypeExtension
{
public function getClass(): string
{
return mysqli::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return 'real_escape_string' === $methodReflection->getName();
}

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return 'mysqli_real_escape_string' === $functionReflection->getName();
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$args = $functionCall->getArgs();
if (\count($args) < 2) {
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
}

$argType = $scope->getType($args[1]->value);

return $this->inferType($argType);
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$args = $methodCall->getArgs();
if (0 === \count($args)) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
}

$argType = $scope->getType($args[0]->value);

return $this->inferType($argType);
}

private function inferType(Type $argType): Type
{
$intersection = [new StringType()];

if ($argType->isNumericString()->yes()) {
// a numeric string is by definition non-empty. therefore don't combine the e accessories
$intersection[] = new AccessoryNumericStringType();
} elseif ($argType->isNonEmptyString()->yes()) {
$intersection[] = new AccessoryNonEmptyStringType();
}

if (\count($intersection) > 1) {
return new IntersectionType($intersection);
}

return new StringType();
}
}
22 changes: 22 additions & 0 deletions tests/data/mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,26 @@ public function fnQuery(mysqli $mysqli, string $query)
$result = mysqli_query($mysqli, $query);
assertType('bool|mysqli_result', $result);
}

/**
* @param numeric $n
* @param non-empty-string $nonE
* @param numeric-string $numericString
*/
public function escape(mysqli $mysqli, int $i, float $f, $n, string $s, $nonE, string $numericString)
{
assertType('numeric-string', mysqli_real_escape_string($mysqli, (string) $i));
assertType('numeric-string', mysqli_real_escape_string($mysqli, (string) $f));
assertType('numeric-string', mysqli_real_escape_string($mysqli, (string) $n));
assertType('numeric-string', mysqli_real_escape_string($mysqli, $numericString));
assertType('non-empty-string', mysqli_real_escape_string($mysqli, $nonE));
assertType('string', mysqli_real_escape_string($mysqli, $s));

assertType('numeric-string', $mysqli->real_escape_string((string) $i));
assertType('numeric-string', $mysqli->real_escape_string((string) $f));
assertType('numeric-string', $mysqli->real_escape_string((string) $n));
assertType('numeric-string', $mysqli->real_escape_string($numericString));
assertType('non-empty-string', $mysqli->real_escape_string($nonE));
assertType('string', $mysqli->real_escape_string($s));
}
}