Skip to content

Commit

Permalink
Detect mixed echo argument when unused variable detection is turned on
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Mar 21, 2021
1 parent b4fab67 commit d174039
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
22 changes: 10 additions & 12 deletions src/Psalm/Internal/Analyzer/Statements/EchoAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,17 @@ public static function analyze(

$expr_type = $statements_analyzer->node_data->getType($expr);

if ($statements_analyzer->data_flow_graph
&& $expr_type
) {
$expr_type = CastAnalyzer::castStringAttempt(
$statements_analyzer,
$context,
$expr_type,
$expr,
false
);
}

if ($statements_analyzer->data_flow_graph instanceof TaintFlowGraph) {
if ($expr_type) {
$expr_type = CastAnalyzer::castStringAttempt(
$statements_analyzer,
$context,
$expr_type,
$expr,
false
);
}

$call_location = new CodeLocation($statements_analyzer->getSource(), $stmt);

$echo_param_sink = TaintSink::getForMethodArgument(
Expand Down
8 changes: 7 additions & 1 deletion tests/UnusedVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ function bar(string $type) : ArrayObject {
$i = 1;
};
$a();
/** @psalm-suppress MixedArgument */
echo $i;',
],
'regularVariableClosureUseInAddition' => [
Expand Down Expand Up @@ -1853,7 +1854,10 @@ function foo(string $a): void {
$hue = "goodbye";
}
/** @psalm-suppress PossiblyUndefinedVariable */
/**
* @psalm-suppress PossiblyUndefinedVariable
* @psalm-suppress MixedArgument
*/
echo $hue;
}'
],
Expand Down Expand Up @@ -1928,6 +1932,7 @@ function foo(int $i): void {
}
if (isset($j)) {
/** @psalm-suppress MixedArgument */
echo $j;
}
}'
Expand Down Expand Up @@ -2012,6 +2017,7 @@ function verifyLoad(bool $b) : bool {
'<?php
/**
* @psalm-suppress MixedAssignment
* @psalm-suppress MixedArgument
* @param iterable<mixed, int> $keys
*/
function foo(iterable $keys, int $colno) : void {
Expand Down

0 comments on commit d174039

Please sign in to comment.