-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from boesing/feature/unnecessary-function-call
Feature: unnecessary function call
- Loading branch information
Showing
9 changed files
with
249 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
/vendor/ | ||
/psalm.xml | ||
/phpcs.xml | ||
/tests/**/* | ||
/tests/_output | ||
/tests/_run | ||
/tests/_support/_generated | ||
!/tests/_support | ||
!/tests/acceptance |
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,113 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Boesing\PsalmPluginStringf\EventHandler; | ||
|
||
use Boesing\PsalmPluginStringf\Parser\Psalm\PhpVersion; | ||
use Boesing\PsalmPluginStringf\Parser\TemplatedStringParser\TemplatedStringParser; | ||
use Boesing\PsalmPluginStringf\Psalm\Issue\UnnecessaryFunctionCall; | ||
use InvalidArgumentException; | ||
use PhpParser\Node\Arg; | ||
use Psalm\CodeLocation; | ||
use Psalm\Context; | ||
use Psalm\IssueBuffer; | ||
use Psalm\Plugin\EventHandler\AfterEveryFunctionCallAnalysisInterface; | ||
use Psalm\Plugin\EventHandler\Event\AfterEveryFunctionCallAnalysisEvent; | ||
use Webmozart\Assert\Assert; | ||
|
||
use function in_array; | ||
|
||
final class UnnecessaryFunctionCallValidator implements AfterEveryFunctionCallAnalysisInterface | ||
{ | ||
private const FUNCTIONS = [ | ||
'sprintf', | ||
'printf', | ||
]; | ||
|
||
/** @psalm-var non-empty-string */ | ||
private string $functionName; | ||
|
||
/** @psalm-var non-empty-list<Arg> */ | ||
private array $arguments; | ||
|
||
/** | ||
* @param non-empty-string $functionName | ||
* @param non-empty-list<Arg> $arguments | ||
*/ | ||
public function __construct( | ||
string $functionName, | ||
array $arguments | ||
) { | ||
$this->functionName = $functionName; | ||
$this->arguments = $arguments; | ||
} | ||
|
||
public static function afterEveryFunctionCallAnalysis(AfterEveryFunctionCallAnalysisEvent $event): void | ||
{ | ||
$functionId = $event->getFunctionId(); | ||
if (! in_array($functionId, self::FUNCTIONS, true)) { | ||
return; | ||
} | ||
|
||
$expression = $event->getExpr(); | ||
$arguments = $expression->args; | ||
|
||
$template = $arguments[0] ?? null; | ||
if ($template === null) { | ||
return; | ||
} | ||
|
||
Assert::isNonEmptyList($arguments); | ||
|
||
$context = $event->getContext(); | ||
|
||
(new self( | ||
$functionId, | ||
$arguments | ||
))->assert( | ||
new CodeLocation($event->getStatementsSource(), $expression), | ||
$context, | ||
PhpVersion::fromCodebase($event->getCodebase()) | ||
); | ||
} | ||
|
||
/** | ||
* @psalm-param positive-int $phpVersion | ||
*/ | ||
private function assert( | ||
CodeLocation $codeLocation, | ||
Context $context, | ||
int $phpVersion | ||
): void { | ||
$template = $this->arguments[0]; | ||
|
||
try { | ||
$parsed = TemplatedStringParser::fromArgument( | ||
$this->functionName, | ||
$template, | ||
$context, | ||
$phpVersion | ||
); | ||
} catch (InvalidArgumentException $exception) { | ||
return; | ||
} | ||
|
||
$this->assertFunctionCallMakesSense( | ||
$codeLocation, | ||
$parsed | ||
); | ||
} | ||
|
||
private function assertFunctionCallMakesSense( | ||
CodeLocation $codeLocation, | ||
TemplatedStringParser $parsed | ||
): void { | ||
if ($parsed->getTemplate() !== $parsed->getTemplateWithoutPlaceholder()) { | ||
return; | ||
} | ||
|
||
// TODO: find out how to provide psalter functionality | ||
IssueBuffer::add(new UnnecessaryFunctionCall($codeLocation, $this->functionName), false); | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Boesing\PsalmPluginStringf\Psalm\Issue; | ||
|
||
use Psalm\CodeLocation; | ||
use Psalm\Issue\FunctionIssue; | ||
|
||
final class UnnecessaryFunctionCall extends FunctionIssue | ||
{ | ||
public function __construct(CodeLocation $code_location, string $function_id) | ||
{ | ||
parent::__construct( | ||
'Function call is unnecessary as there is no placeholder within the template.', | ||
$code_location, | ||
$function_id | ||
); | ||
} | ||
} |
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,44 @@ | ||
Feature: unnecessary printf function call | ||
|
||
Background: | ||
Given I have the following config | ||
""" | ||
<?xml version="1.0"?> | ||
<psalm errorLevel="1" findUnusedPsalmSuppress="true"> | ||
<projectFiles> | ||
<directory name="."/> | ||
</projectFiles> | ||
<plugins> | ||
<pluginClass class="Boesing\PsalmPluginStringf\Plugin"> | ||
<experimental> | ||
<ReportUnnecessaryFunctionCalls/> | ||
</experimental> | ||
</pluginClass> | ||
</plugins> | ||
</psalm> | ||
""" | ||
And I have the following code preamble | ||
""" | ||
<?php declare(strict_types=1); | ||
""" | ||
|
||
Scenario: template is empty | ||
Given I have the following code | ||
""" | ||
printf(''); | ||
""" | ||
When I run Psalm | ||
Then I see these errors | ||
| Type | Message | | ||
| UnnecessaryFunctionCall | Function call is unnecessary as there is no placeholder within the template | | ||
|
||
Scenario: template contains no identifier | ||
Given I have the following code | ||
""" | ||
printf('Foo bar baz'); | ||
""" | ||
When I run Psalm | ||
Then I see these errors | ||
| Type | Message | | ||
| UnnecessaryFunctionCall | Function call is unnecessary as there is no placeholder within the template | |
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,44 @@ | ||
Feature: unnecessary sprintf function call | ||
|
||
Background: | ||
Given I have the following config | ||
""" | ||
<?xml version="1.0"?> | ||
<psalm errorLevel="1" findUnusedPsalmSuppress="true"> | ||
<projectFiles> | ||
<directory name="."/> | ||
</projectFiles> | ||
<plugins> | ||
<pluginClass class="Boesing\PsalmPluginStringf\Plugin"> | ||
<experimental> | ||
<ReportUnnecessaryFunctionCalls/> | ||
</experimental> | ||
</pluginClass> | ||
</plugins> | ||
</psalm> | ||
""" | ||
And I have the following code preamble | ||
""" | ||
<?php declare(strict_types=1); | ||
""" | ||
|
||
Scenario: template is empty | ||
Given I have the following code | ||
""" | ||
sprintf(''); | ||
""" | ||
When I run Psalm | ||
Then I see these errors | ||
| Type | Message | | ||
| UnnecessaryFunctionCall | Function call is unnecessary as there is no placeholder within the template | | ||
|
||
Scenario: template contains no identifier | ||
Given I have the following code | ||
""" | ||
sprintf('Foo bar baz'); | ||
""" | ||
When I run Psalm | ||
Then I see these errors | ||
| Type | Message | | ||
| UnnecessaryFunctionCall | Function call is unnecessary as there is no placeholder within the template | |