Skip to content
Open
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
6 changes: 6 additions & 0 deletions config/rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,9 @@ services:
- 'mysqli::execute_query#0'
- 'Doctrine\DBAL\Connection::query#0' # deprecated in doctrine
- 'Doctrine\DBAL\Connection::exec#0' # deprecated in doctrine

-
class: staabm\PHPStanDba\Rules\MysqliFetchColumnRule
tags: [phpstan.rules.rule]
arguments:
reportMaybes: %reportMaybes%
53 changes: 3 additions & 50 deletions src/Extensions/MysqliResultDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
Expand All @@ -35,13 +33,6 @@

final class MysqliResultDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension, DynamicMethodReturnTypeExtension
{
// same-value replacements for MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH constants in mysqli extension
private const MYSQLI_ASSOC = 1;

private const MYSQLI_NUM = 2;

private const MYSQLI_BOTH = 3;

private ReflectionProvider $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
Expand Down Expand Up @@ -70,7 +61,7 @@ public function fetchAll(array $modes, Type $rowType): ?Type
{
$rows = [];
foreach ($modes as $mode) {
$modeRowType = $this->getRowTypeForMode($rowType, $mode);
$modeRowType = MysqliResultObjectType::getRowTypeForMode($rowType, $mode);
if ($modeRowType === null) {
return null;
}
Expand Down Expand Up @@ -215,7 +206,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return $this->fetchAll($modes, $rowType);
}

$numericRowType = $this->getRowTypeForMode($rowType, self::MYSQLI_NUM);
$numericRowType = MysqliResultObjectType::getRowTypeForMode($rowType);
if ($numericRowType === null) {
return null;
}
Expand Down Expand Up @@ -350,7 +341,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
return $this->fetchAll($modes, $rowType);
}

$numericRowType = $this->getRowTypeForMode($rowType, self::MYSQLI_NUM);
$numericRowType = MysqliResultObjectType::getRowTypeForMode($rowType);
if ($numericRowType === null) {
return null;
}
Expand Down Expand Up @@ -425,42 +416,4 @@ private function getModes(Type $modeType): ?array

return $modes;
}

private function getRowTypeForMode(Type $rowType, int $mode): ?Type
{
if (! \in_array($mode, [self::MYSQLI_NUM, self::MYSQLI_ASSOC, self::MYSQLI_BOTH], true)) {
return null;
}

$rowTypes = [];
foreach ($rowType->getConstantArrays() as $arrayType) {
$rowTypes[] = $this->convertRowType($arrayType, $mode);
}

if (count($rowTypes) === 0) {
return null;
}

return TypeCombinator::union(...$rowTypes);
}

private function convertRowType(ConstantArrayType $rowType, int $mode): Type
{
$builder = ConstantArrayTypeBuilder::createEmpty();
$numericIndex = 0;

foreach ($rowType->getKeyTypes() as $index => $keyType) {
$valueType = $rowType->getValueTypes()[$index];
if ($mode === self::MYSQLI_ASSOC || $mode === self::MYSQLI_BOTH) {
$builder->setOffsetValueType($keyType, $valueType);
}

if ($mode === self::MYSQLI_NUM || $mode === self::MYSQLI_BOTH) {
$builder->setOffsetValueType(new ConstantIntegerType($numericIndex), $valueType);
$numericIndex++;
}
}

return $builder->getArray();
}
}
42 changes: 42 additions & 0 deletions src/MysqliReflection/MysqliResultObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@
namespace staabm\PHPStanDba\MysqliReflection;

use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\IsSuperTypeOfResult;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

final class MysqliResultObjectType extends ObjectType
{
// same-value replacements for MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH constants in mysqli extension
private const MYSQLI_ASSOC = 1;

private const MYSQLI_NUM = 2;

private const MYSQLI_BOTH = 3;

private ?Type $rowType = null;

public static function newWithRowType(Type $rowType): self
Expand All @@ -29,6 +39,38 @@ public function getRowType(): Type
return $this->rowType;
}

public static function getRowTypeForMode(Type $rowType, int $mode = self::MYSQLI_NUM): ?Type
{
if (! \in_array($mode, [self::MYSQLI_NUM, self::MYSQLI_ASSOC, self::MYSQLI_BOTH], true)) {
return null;
}

$rowTypes = [];
foreach ($rowType->getConstantArrays() as $arrayType) {
$builder = ConstantArrayTypeBuilder::createEmpty();
$numericIndex = 0;

foreach ($arrayType->getKeyTypes() as $index => $keyType) {
$valueType = $arrayType->getValueTypes()[$index];
if ($mode === self::MYSQLI_ASSOC || $mode === self::MYSQLI_BOTH) {
$builder->setOffsetValueType($keyType, $valueType);
}

if ($mode === self::MYSQLI_NUM || $mode === self::MYSQLI_BOTH) {
$builder->setOffsetValueType(new ConstantIntegerType($numericIndex), $valueType);
$numericIndex++;
}
}
$rowTypes[] = $builder->getArray();
}

if (count($rowTypes) === 0) {
return null;
}

return TypeCombinator::union(...$rowTypes);
}

public function getIterableValueType(): Type
{
return $this->getRowType();
Expand Down
151 changes: 151 additions & 0 deletions src/Rules/MysqliFetchColumnRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

declare(strict_types=1);

namespace staabm\PHPStanDba\Rules;

use PhpParser\Node;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use staabm\PHPStanDba\MysqliReflection\MysqliResultObjectType;

/** @implements Rule<CallLike> */
final class MysqliFetchColumnRule implements Rule
{
private ReflectionProvider $reflectionProvider;

private bool $reportMaybes;

public function __construct(
ReflectionProvider $reflectionProvider,
#[AutowiredParameter]
bool $reportMaybes
) {
$this->reflectionProvider = $reflectionProvider;
$this->reportMaybes = $reportMaybes;
}

public function getNodeType(): string
{
return CallLike::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (! $node instanceof FuncCall && ! $node instanceof MethodCall) {
return [];
}

foreach ($node->getArgs() as $arg) {
if ($arg->unpack) {
return [];
}
}

if ($node instanceof FuncCall) {
if (! $node->name instanceof Node\Name
|| ! $this->reflectionProvider->hasFunction($node->name, $scope)
) {
return [];
}

$reflection = $this->reflectionProvider->getFunction($node->name, $scope);
if (strtolower($reflection->getName()) !== 'mysqli_fetch_column') {
return [];
}

$acceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$node->getArgs(),
$reflection->getVariants(),
$reflection->getNamedArgumentsVariants(),
);
$call = ArgumentsNormalizer::reorderFuncArguments($acceptor, $node);
if ($call === null) {
return [];
}

$args = $call->getArgs();
if (! isset($args[0], $args[1])) {
return [];
}

$resultType = $scope->getType($args[0]->value);
if (! ($resultType instanceof MysqliResultObjectType)) {
return [];
}

$columnArg = $args[1];
} else { // MethodCall
if (! $node->name instanceof Node\Identifier
|| strtolower($node->name->toString()) !== 'fetch_column'
) {
return [];
}

$resultType = $scope->getType($node->var);
if (! $resultType instanceof MysqliResultObjectType) {
return [];
}

$reflection = $scope->getMethodReflection($resultType, $node->name->toString());
if ($reflection === null) {
return [];
}

$acceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$node->getArgs(),
$reflection->getVariants(),
$reflection->getNamedArgumentsVariants(),
);
$call = ArgumentsNormalizer::reorderMethodArguments($acceptor, $node);
if ($call === null) {
return [];
}

$args = $call->getArgs();
if (! isset($args[0])) {
return [];
}
$columnArg = $args[0];
}


$rowType = $resultType->getRowType();
$numericRowType = MysqliResultObjectType::getRowTypeForMode($rowType);
if ($numericRowType === null) {
return [];
}

$columnType = $scope->getType($columnArg->value);

$hasOffset = $numericRowType->hasOffsetValueType($columnType);
if (! $this->reportMaybes && $hasOffset->maybe()) {
return [];
}
if (! $hasOffset->yes()) {
return [
RuleErrorBuilder::message(sprintf(
'Column offset %s %s out of range; valid offsets are %s.',
$columnType->describe(VerbosityLevel::value()),
$hasOffset->maybe() ? 'may be' : 'is',
$numericRowType->getIterableKeyType()->describe(VerbosityLevel::value()),
))
->identifier('dba.columnOffset')
->line($columnArg->value->getStartLine())
->build(),
];
}
return [];
}
}
15 changes: 0 additions & 15 deletions tests/default/config/.phpunit-phpstan-dba-mysqli.cache

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading