Skip to content

Commit

Permalink
Fix #3660 - ensure mixed percentage for file never drops below 0
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Aug 14, 2021
1 parent 1b19670 commit a447976
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/Psalm/Internal/Analyzer/Statements/Block/IfElse/IfAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,21 @@ private static function handleMicDrop(
if (!$post_if_context->collect_initializations
&& !$post_if_context->collect_mutations
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
&& (!(($parent_source = $statements_analyzer->getSource())
instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer)
|| !$parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer)
) {
$codebase = $statements_analyzer->getCodebase();
$codebase->analyzer->decrementMixedCount($statements_analyzer->getFilePath());
$parent_source = $statements_analyzer->getSource();

$functionlike_storage = $parent_source instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
? $parent_source->getFunctionLikeStorage()
: null;

if (!$parent_source instanceof \Psalm\Internal\Analyzer\FunctionLikeAnalyzer
|| (!$parent_source->getSource() instanceof \Psalm\Internal\Analyzer\TraitAnalyzer
&& (!$functionlike_storage
|| !isset($functionlike_storage->param_lookup[substr($var_id, 1)])))
) {
$codebase = $statements_analyzer->getCodebase();
$codebase->analyzer->decrementMixedCount($statements_analyzer->getFilePath());
}
}

IssueBuffer::remove(
Expand Down
6 changes: 5 additions & 1 deletion src/Psalm/Internal/Codebase/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,10 @@ public function decrementMixedCount(string $file_path): void
return;
}

if ($this->mixed_counts[$file_path][0] === 0) {
return;
}

--$this->mixed_counts[$file_path][0];
}

Expand Down Expand Up @@ -1403,7 +1407,7 @@ public function getNonMixedStats(): string
[$path_mixed_count, $path_nonmixed_count] = $this->mixed_counts[$file_path];

if ($path_mixed_count + $path_nonmixed_count) {
$stats .= number_format(100 * $path_nonmixed_count / ($path_mixed_count + $path_nonmixed_count), 0)
$stats .= number_format(100 * $path_nonmixed_count / ($path_mixed_count + $path_nonmixed_count), 3)
. '% ' . $this->config->shortenFileName($file_path)
. ' (' . $path_mixed_count . ' mixed)' . "\n";
}
Expand Down

0 comments on commit a447976

Please sign in to comment.