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 8 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
1 change: 1 addition & 0 deletions conf/config.level0.neon
Expand Up @@ -55,6 +55,7 @@ rules:
- PHPStan\Rules\Functions\ExistingClassesInTypehintsRule
- PHPStan\Rules\Functions\FunctionAttributesRule
- PHPStan\Rules\Functions\InnerFunctionRule
- PHPStan\Rules\Functions\ImplodeFunctionRule
staabm marked this conversation as resolved.
Show resolved Hide resolved
- PHPStan\Rules\Functions\ParamAttributesRule
- PHPStan\Rules\Functions\PrintfParametersRule
- PHPStan\Rules\Functions\ReturnNullsafeByRefRule
Expand Down
64 changes: 64 additions & 0 deletions src/Rules/Functions/ImplodeFunctionRule.php
@@ -0,0 +1,64 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\FuncCall>
*/
class ImplodeFunctionRule implements \PHPStan\Rules\Rule
{
public function getNodeType(): string
{
return FuncCall::class;
}

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

$name = strtolower((string) $node->name);
if ($name !== 'implode') {
staabm marked this conversation as resolved.
Show resolved Hide resolved
return [];
}

$args = $node->getArgs();
if (count($args) !== 2) {
staabm marked this conversation as resolved.
Show resolved Hide resolved
return [];
}

$arrayType = $scope->getType($args[1]->value);
if ($arrayType->getIterableValueType()->isArray()->yes()) {
staabm marked this conversation as resolved.
Show resolved Hide resolved
return [
RuleErrorBuilder::message(
'Call to implode with invalid nested array argument.',
)->build()
];
}
if ($arrayType->getIterableValueType() instanceof UnionType) {
foreach ($arrayType->getIterableValueType()->getTypes() as $subType) {
if ($subType->isArray()->yes()) {
return [
RuleErrorBuilder::message(
'Call to implode with invalid nested array argument in union type.',
)->build()
];
}
}
}

return [];
}

}
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -510,6 +510,8 @@ public function dataFileAsserts(): iterable

yield from $this->gatherAssertTypes(__DIR__ . '/data/eval-implicit-throw.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5628.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/implode.php');
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/PHPStan/Rules/Functions/ImplodeFunctionRuleTest.php
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

use PHPStan\Php\PhpVersion;
use const PHP_VERSION_ID;

/**
* @extends \PHPStan\Testing\RuleTestCase<ImplodeFunctionRule>
*/
class ImplodeFunctionRuleTest extends \PHPStan\Testing\RuleTestCase
{

protected function getRule(): \PHPStan\Rules\Rule
{
return new ImplodeFunctionRule();
}

public function testFile(): void
{
$this->analyse([__DIR__ . '/data/implode.php'], [
[
'Call to implode with invalid nested array argument in union type.',
9,
],
[
'Call to implode with invalid nested array argument.',
10,
],
]);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Functions/data/implode.php
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace ImplodeFunction;

class Foo
{
public function invalidArgs(): void
{
implode('', ['12', '123', ['1234', '12345']]);
implode('', [['1234', '12345']]);
}

public function valid() {
implode('', ['12', '345']);
}
}