Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?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; | ||
$paramNo = 1; | ||
} elseif (count($args) === 2) { | ||
$arrayArg = $args[1]->value; | ||
$paramNo = 2; | ||
} 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('Parameter #%d $array of function %s expects array<string>, %s given.', $paramNo, $functionName, $typeResult->getType()->getIterableValueType()->describe(VerbosityLevel::typeOnly())) | ||
)->build(), | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
use PHPStan\Rules\RuleLevelHelper; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<ImplodeFunctionRule> | ||
*/ | ||
class ImplodeFunctionRuleTest extends \PHPStan\Testing\RuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
$broker = $this->createReflectionProvider(); | ||
return new ImplodeFunctionRule($broker, new RuleLevelHelper($broker, true, false, true, false)); | ||
} | ||
|
||
public function testFile(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/implode.php'], [ | ||
[ | ||
'Parameter #2 $array of function implode expects array<string>, array<int, string>|string given.', | ||
9, | ||
], | ||
[ | ||
'Parameter #1 $array of function implode expects array<string>, array<int, string> given.', | ||
11, | ||
], | ||
[ | ||
'Parameter #1 $array of function implode expects array<string>, array<int, int> given.', | ||
12, | ||
], | ||
[ | ||
'Parameter #1 $array of function implode expects array<string>, array<int, int|true> given.', | ||
13, | ||
], | ||
[ | ||
'Parameter #2 $array of function implode expects array<string>, array<int, string> given.', | ||
15, | ||
], | ||
[ | ||
'Parameter #2 $array of function join expects array<string>, array<int, string> given.', | ||
16, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ImplodeFunction; | ||
|
||
class Foo | ||
{ | ||
public function invalidArgs(): void | ||
{ | ||
implode('', ['12', '123', ['1234', '12345']]); | ||
|
||
implode([['1234', '12345']]); | ||
implode([[1234, 12345]]); | ||
implode([[true, 12345]]); | ||
|
||
implode('', [['1234', '12345']]); | ||
join('', [['1234', '12345']]); | ||
} | ||
|
||
public function valid() { | ||
implode(['12', '345']); | ||
|
||
implode('', ['12', '345']); | ||
join('', ['12', '345']); | ||
} | ||
} |