Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ImplodeFunctionRule #679

Merged
merged 25 commits into from Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions conf/config.level5.neon
Expand Up @@ -11,6 +11,8 @@ parameters:
checkFunctionArgumentTypes: true
checkArgumentsPassedByReference: true

rules:
- PHPStan\Rules\Functions\ImplodeFunctionRule

services:
-
Expand Down
80 changes: 80 additions & 0 deletions src/Rules/Functions/ImplodeFunctionRule.php
@@ -0,0 +1,80 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\FuncCall>
*/
class ImplodeFunctionRule implements \PHPStan\Rules\Rule
{

private RuleLevelHelper $ruleLevelHelper;

private ReflectionProvider $reflectionProvider;

public function __construct(
ReflectionProvider $reflectionProvider,
RuleLevelHelper $ruleLevelHelper
)
{
$this->reflectionProvider = $reflectionProvider;
$this->ruleLevelHelper = $ruleLevelHelper;
}

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

public function processNode(Node $node, Scope $scope): array
{
if (!($node->name instanceof \PhpParser\Node\Name)) {
return [];
}

$functionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope);
if (!in_array($functionName, ['implode', 'join'], true)) {
return [];
}

$args = $node->getArgs();
if (count($args) === 1) {
$arrayArg = $args[0]->value;
} elseif (count($args) === 2) {
$arrayArg = $args[1]->value;
} else {
return [];
}

$typeResult = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$arrayArg,
'',
static function (Type $type): bool {
return !$type->getIterableValueType()->toString() instanceof ErrorType;
}
);

if ($typeResult->getType() instanceof ErrorType
|| !$typeResult->getType()->getIterableValueType()->toString() instanceof ErrorType) {
return [];
}

return [
RuleErrorBuilder::message(
sprintf('Call to function %s() with invalid non-string argument type %s.', $functionName, $typeResult->getType()->getIterableValueType()->describe(VerbosityLevel::typeOnly()))
staabm marked this conversation as resolved.
Show resolved Hide resolved
)->build(),
];
}

}
54 changes: 27 additions & 27 deletions tests/PHPStan/Levels/data/acceptTypes-5.json
Expand Up @@ -66,137 +66,137 @@
},
{
"message": "Parameter #1 $closure of method Levels\\AcceptTypes\\ClosureAccepts::doBar() expects Closure(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface, mixed, mixed): Levels\\AcceptTypes\\FooImpl given.",
"line": 226,
"line": 232,
"ignorable": true
},
{
"message": "Parameter #1 $callable of method Levels\\AcceptTypes\\ClosureAccepts::doBaz() expects callable(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface, mixed, mixed): Levels\\AcceptTypes\\FooImpl given.",
"line": 227,
"line": 233,
"ignorable": true
},
{
"message": "Parameter #1 $closure of method Levels\\AcceptTypes\\ClosureAccepts::doBar() expects Closure(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooImpl): Levels\\AcceptTypes\\FooImpl given.",
"line": 238,
"line": 244,
"ignorable": true
},
{
"message": "Parameter #1 $callable of method Levels\\AcceptTypes\\ClosureAccepts::doBaz() expects callable(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooImpl): Levels\\AcceptTypes\\FooImpl given.",
"line": 239,
"line": 245,
"ignorable": true
},
{
"message": "Parameter #1 $closure of method Levels\\AcceptTypes\\ClosureAccepts::doBar() expects Closure(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface): Levels\\AcceptTypes\\ParentFooInterface given.",
"line": 250,
"line": 256,
"ignorable": true
},
{
"message": "Parameter #1 $callable of method Levels\\AcceptTypes\\ClosureAccepts::doBaz() expects callable(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface): Levels\\AcceptTypes\\ParentFooInterface given.",
"line": 251,
"line": 257,
"ignorable": true
},
{
"message": "Parameter #1 $closure of method Levels\\AcceptTypes\\ClosureAccepts::doBar() expects Closure(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface): Levels\\AcceptTypes\\ParentFooInterface given.",
"line": 319,
"line": 325,
"ignorable": true
},
{
"message": "Parameter #1 $callable of method Levels\\AcceptTypes\\ClosureAccepts::doBaz() expects callable(Levels\\AcceptTypes\\FooInterface, int): Levels\\AcceptTypes\\FooInterface, Closure(Levels\\AcceptTypes\\FooInterface): Levels\\AcceptTypes\\ParentFooInterface given.",
"line": 320,
"line": 326,
"ignorable": true
},
{
"message": "Parameter #1 $i of method Levels\\AcceptTypes\\Baz::doBar() expects int, float given.",
"line": 412,
"line": 418,
"ignorable": true
},
{
"message": "Parameter #1 $i of method Levels\\AcceptTypes\\Baz::doBar() expects int, float|string given.",
"line": 413,
"line": 419,
"ignorable": true
},
{
"message": "Parameter #1 $a of method Levels\\AcceptTypes\\Baz::doLorem() expects int|resource, float|string given.",
"line": 425,
"line": 431,
"ignorable": true
},
{
"message": "Parameter #1 $a of method Levels\\AcceptTypes\\Baz::doLorem() expects int|resource, float|string|null given.",
"line": 426,
"line": 432,
"ignorable": true
},
{
"message": "Parameter #1 $i of method Levels\\AcceptTypes\\Baz::doBar() expects int, null given.",
"line": 431,
"line": 437,
"ignorable": true
},
{
"message": "Parameter #1 $i of method Levels\\AcceptTypes\\Baz::doBarArray() expects array<int>, array<float> given.",
"line": 493,
"line": 499,
"ignorable": true
},
{
"message": "Parameter #1 $i of method Levels\\AcceptTypes\\Baz::doBarArray() expects array<int>, array<float|string> given.",
"line": 494,
"line": 500,
"ignorable": true
},
{
"message": "Parameter #1 $one of method Levels\\AcceptTypes\\ArrayShapes::doBar() expects array('foo' => callable(): mixed), array<int> given.",
"line": 579,
"line": 585,
"ignorable": true
},
{
"message": "Parameter #1 $one of method Levels\\AcceptTypes\\ArrayShapes::doBar() expects array('foo' => callable(): mixed), array() given.",
"line": 580,
"line": 586,
"ignorable": true
},
{
"message": "Parameter #1 $one of method Levels\\AcceptTypes\\ArrayShapes::doBar() expects array('foo' => callable(): mixed), array('foo' => 1) given.",
"line": 582,
"line": 588,
"ignorable": true
},
{
"message": "Parameter #1 $one of method Levels\\AcceptTypes\\ArrayShapes::doBar() expects array('foo' => callable(): mixed), array('foo' => 'nonexistent') given.",
"line": 584,
"line": 590,
"ignorable": true
},
{
"message": "Parameter #1 $one of method Levels\\AcceptTypes\\ArrayShapes::doBar() expects array('foo' => callable(): mixed), array('bar' => 'date') given.",
"line": 585,
"line": 591,
"ignorable": true
},
{
"message": "Parameter #1 $min (0) of function random_int expects lower number than parameter #2 $max (-1).",
"line": 671,
"line": 677,
"ignorable": true
},
{
"message": "Parameter #1 $min (int<11, max>) of function random_int expects lower number than parameter #2 $max (10).",
"line": 678,
"line": 684,
"ignorable": true
},
{
"message": "Parameter #1 $min (340) of function random_int expects lower number than parameter #2 $max (int<min, 339>).",
"line": 685,
"line": 691,
"ignorable": true
},
{
"message": "Parameter #1 $numericString of method Levels\\AcceptTypes\\NumericStrings::doBar() expects string&numeric, 'foo' given.",
"line": 707,
"line": 713,
"ignorable": true
},
{
"message": "Parameter #1 $numericString of method Levels\\AcceptTypes\\NumericStrings::doBar() expects string&numeric, string given.",
"line": 708,
"line": 714,
"ignorable": true
},
{
"message": "Parameter #1 $nonEmpty of method Levels\\AcceptTypes\\AcceptNonEmpty::doBar() expects array&nonEmpty, array() given.",
"line": 733,
"line": 739,
"ignorable": true
},
{
"message": "Parameter #1 $nonEmpty of method Levels\\AcceptTypes\\AcceptNonEmpty::doBar() expects array&nonEmpty, array given.",
"line": 735,
"line": 741,
"ignorable": true
}
]
44 changes: 27 additions & 17 deletions tests/PHPStan/Levels/data/acceptTypes-6.json
Expand Up @@ -64,89 +64,99 @@
"line": 176,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\Foo::partlySupportedUnion() has no return type specified.",
"line": 186,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\Foo::partlySupportedUnion() has parameter $union with no value type specified in iterable type array.",
"line": 186,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\ClosureAccepts::doFoo() has no return type specified.",
"line": 208,
"line": 214,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\ClosureAccepts::doFooUnionClosures() has no return type specified.",
"line": 254,
"line": 260,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\ClosureAccepts::doBar() has no return type specified.",
"line": 332,
"line": 338,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\ClosureAccepts::doBaz() has no return type specified.",
"line": 342,
"line": 348,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\Baz::doFoo() has no return type specified.",
"line": 409,
"line": 415,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\Baz::doBar() has no return type specified.",
"line": 418,
"line": 424,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\Baz::doBaz() has no return type specified.",
"line": 423,
"line": 429,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\Baz::doLorem() has no return type specified.",
"line": 437,
"line": 443,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\Baz::doIpsum() has no return type specified.",
"line": 445,
"line": 451,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\Baz::doFooArray() has no return type specified.",
"line": 490,
"line": 496,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\Baz::doBarArray() has no return type specified.",
"line": 502,
"line": 508,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\Baz::testUnions() has no return type specified.",
"line": 524,
"line": 530,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\Baz::testUnions2() has no return type specified.",
"line": 535,
"line": 541,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\Baz::requireArray() has no return type specified.",
"line": 549,
"line": 555,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\Baz::requireFoo() has no return type specified.",
"line": 554,
"line": 560,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\ArrayShapes::doFoo() has no return type specified.",
"line": 570,
"line": 576,
"ignorable": true
},
{
"message": "Method Levels\\AcceptTypes\\ArrayShapes::doBar() has no return type specified.",
"line": 603,
"line": 609,
"ignorable": true
}
]