Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add psalm-flow for string functions from sscanf to wordwrap (#4591)
* Add string functions from sscanf to wordwrap This should conclude all string functions from https://www.php.net/manual/en/book.strings.php Continuation of #4576 Ref #3636 * Add StrTrReturnTypeProvider * Fix psalm error * phpcs * Line length * Ignore false return on vsprintf Co-authored-by: Matthew Brown <github@muglug.com>
- Loading branch information
1 parent
bf873b2
commit 3943b55
Showing
4 changed files
with
228 additions
and
2 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
69 changes: 69 additions & 0 deletions
69
src/Psalm/Internal/Provider/ReturnTypeProvider/StrTrReturnTypeProvider.php
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,69 @@ | ||
<?php | ||
namespace Psalm\Internal\Provider\ReturnTypeProvider; | ||
|
||
use PhpParser; | ||
use Psalm\CodeLocation; | ||
use Psalm\Context; | ||
use Psalm\StatementsSource; | ||
use Psalm\Type; | ||
use Psalm\Internal\DataFlow\DataFlowNode; | ||
|
||
class StrTrReturnTypeProvider implements \Psalm\Plugin\Hook\FunctionReturnTypeProviderInterface | ||
{ | ||
public static function getFunctionIds() : array | ||
{ | ||
return [ | ||
'strtr', | ||
]; | ||
} | ||
|
||
/** | ||
* @param list<PhpParser\Node\Arg> $call_args | ||
*/ | ||
public static function getFunctionReturnType( | ||
StatementsSource $statements_source, | ||
string $function_id, | ||
array $call_args, | ||
Context $context, | ||
CodeLocation $code_location | ||
) : Type\Union { | ||
if (!$statements_source instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) { | ||
throw new \UnexpectedValueException(); | ||
} | ||
|
||
$type = Type::getString(); | ||
|
||
if ($statements_source->data_flow_graph | ||
&& !\in_array('TaintedInput', $statements_source->getSuppressedIssues())) { | ||
$function_return_sink = DataFlowNode::getForMethodReturn( | ||
$function_id, | ||
$function_id, | ||
null, | ||
$code_location | ||
); | ||
|
||
$statements_source->data_flow_graph->addNode($function_return_sink); | ||
foreach ($call_args as $i => $_) { | ||
$function_param_sink = DataFlowNode::getForMethodArgument( | ||
$function_id, | ||
$function_id, | ||
$i, | ||
null, | ||
$code_location | ||
); | ||
|
||
$statements_source->data_flow_graph->addNode($function_param_sink); | ||
|
||
$statements_source->data_flow_graph->addPath( | ||
$function_param_sink, | ||
$function_return_sink, | ||
'arg' | ||
); | ||
} | ||
|
||
$type->parent_nodes = [$function_return_sink->id => $function_return_sink]; | ||
} | ||
|
||
return $type; | ||
} | ||
} |
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