Skip to content

Commit

Permalink
Add better trace for MixedOperand issues
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Mar 28, 2021
1 parent 93743d1 commit f24ef25
Showing 1 changed file with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use PhpParser;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Type\Comparator\AtomicTypeComparator;
use Psalm\Internal\Codebase\VariableUseGraph;
use Psalm\CodeLocation;
use Psalm\Config;
use Psalm\Context;
Expand All @@ -21,6 +22,9 @@
use Psalm\Type\Atomic\TNamedObject;
use function strtolower;
use function strlen;
use function array_merge;
use function count;
use function reset;

/**
* @internal
Expand Down Expand Up @@ -58,20 +62,59 @@ public static function analyze(
}

if ($left_type->hasMixed()) {
$arg_location = new CodeLocation($statements_analyzer->getSource(), $left);

$origin_locations = [];

if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) {
foreach ($left_type->parent_nodes as $parent_node) {
$origin_locations = array_merge(
$origin_locations,
$statements_analyzer->data_flow_graph->getOriginLocations($parent_node)
);
}
}

$origin_location = count($origin_locations) === 1 ? reset($origin_locations) : null;

if ($origin_location && $origin_location->getHash() === $arg_location->getHash()) {
$origin_location = null;
}

if (IssueBuffer::accepts(
new MixedOperand(
'Left operand cannot be mixed',
new CodeLocation($statements_analyzer->getSource(), $left)
$arg_location,
$origin_location
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} else {
$arg_location = new CodeLocation($statements_analyzer->getSource(), $right);
$origin_locations = [];

if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) {
foreach ($right_type->parent_nodes as $parent_node) {
$origin_locations = array_merge(
$origin_locations,
$statements_analyzer->data_flow_graph->getOriginLocations($parent_node)
);
}
}

$origin_location = count($origin_locations) === 1 ? reset($origin_locations) : null;

if ($origin_location && $origin_location->getHash() === $arg_location->getHash()) {
$origin_location = null;
}

if (IssueBuffer::accepts(
new MixedOperand(
'Right operand cannot be mixed',
new CodeLocation($statements_analyzer->getSource(), $right)
$arg_location,
$origin_location
),
$statements_analyzer->getSuppressedIssues()
)) {
Expand Down

0 comments on commit f24ef25

Please sign in to comment.