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
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
b5745c0
report error on multi-dimensional array given to implode()
clxmstaab 1b156c0
Added test
staabm 393089b
fix
staabm 8f9a174
fix CS
clxmstaab 6ec58a0
Update implode.php
staabm 17d8bf4
added ImplodeFunctionRule
clxmstaab 3a7a02c
Merge branch 'implode-array' of https://github.com/staabm/phpstan-src…
clxmstaab db573c1
revert
clxmstaab 74f1462
Update NodeScopeResolverTest.php
staabm c8818ab
move into level 5
clxmstaab a2a827a
support join alias
clxmstaab 63f6100
Merge branch 'implode-array' of https://github.com/staabm/phpstan-src…
clxmstaab 75ac2e2
support single arg case
clxmstaab 4d4a3b2
strict
clxmstaab a5cca5d
exit case
clxmstaab d6c71ec
Update NodeScopeResolverTest.php
staabm 08e0e6f
fix CS
clxmstaab 3a3cd53
compare against toString()
clxmstaab fccad62
adjusted rule error message
clxmstaab 28b853c
RuleLevelHelper::findTypeToCheck()
clxmstaab d8f2ac9
fix CS
clxmstaab e17c61c
added phpstan level test
clxmstaab fc958b6
fix
clxmstaab 18d1b2b
fix
clxmstaab 4f94d90
fix messages
clxmstaab File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,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) { | ||
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 []; | ||
} | ||
|
||
} |
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,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, | ||
], | ||
]); | ||
} | ||
|
||
} |
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']); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.