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 18 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
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\Reflection\ReflectionProvider;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ErrorType;
use PHPStan\Type\UnionType;

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

private \PHPStan\Reflection\ReflectionProvider $reflectionProvider;

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

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) {
$arrayType = $scope->getType($args[0]->value);
} elseif (count($args) === 2) {
$arrayType = $scope->getType($args[1]->value);
} else {
return [];
}

if ($arrayType->getIterableValueType()->toString() instanceof ErrorType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also employ RuleLevelHelper::findTypeToCheck so that partially-wrong unions aren't reported until level 7.

Ther are many usages of that method, look through the codebase.

Copy link
Contributor Author

@staabm staabm Sep 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adjusted. seems to work. does this require some kind of unit-test?

I am not sure yet, which case this new logic supports, which the previous didn't.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out LevelsIntegrationTest, add something in acceptTypes.php, and re-run this test case. The JSON snapshots will be updated.

Copy link
Contributor Author

@staabm staabm Sep 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx, I guess it works now.

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

return [];
}

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

namespace PHPStan\Rules\Functions;

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

protected function getRule(): \PHPStan\Rules\Rule
{
$reflectionProvider = $this->createReflectionProvider();
return new ImplodeFunctionRule($reflectionProvider);
}

public function testFile(): void
{
$this->analyse([__DIR__ . '/data/implode.php'], [
[
'Call to implode with invalid non-string argument.',
9,
],
[
'Call to implode with invalid non-string argument.',
11,
],
[
'Call to implode with invalid non-string argument.',
12,
],
[
'Call to implode with invalid non-string argument.',
13,
],
[
'Call to implode with invalid non-string argument.',
15,
],
[
'Call to join with invalid non-string argument.',
16,
],
]);
}

}
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Functions/data/implode.php
@@ -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']);
}
}