Skip to content

Commit

Permalink
Merge pull request #7 from nicelocal/master
Browse files Browse the repository at this point in the history
Small improvement for integer ranges
  • Loading branch information
orklah authored Mar 20, 2024
2 parents f0e44bf + 32b4cfa commit d9e608e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
},
"require": {
"php": "^7.3|^8.0",
"ext-simplexml": "*"
"ext-simplexml": "*",
"vimeo/psalm": "^4|^5|dev-master"
},
"autoload": {
"psr-4": {
Expand All @@ -25,8 +26,7 @@
}
},
"require-dev": {
"vimeo/psalm": "^4.0|^5",
"nikic/php-parser": "^4.0"
"nikic/php-parser": "^4.0|^5"
},
"config": {
"allow-plugins": {
Expand Down
29 changes: 21 additions & 8 deletions hooks/InsaneComparisonAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
use Psalm\Plugin\EventHandler\AfterExpressionAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent;
use Psalm\Type;
use Psalm\Type\Atomic\TIntRange;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TPositiveInt;

/**
* @api
*/
class InsaneComparisonAnalyzer implements AfterExpressionAnalysisInterface
{
/**
* Called after an expression has been checked
*
* @return null|false
*/
public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $event): ?bool
{
Expand All @@ -35,20 +37,22 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve
&& !$expr instanceof Expr\BinaryOp\Smaller
&& !$expr instanceof Expr\BinaryOp\SmallerOrEqual
) {
return true;
return null;
}

$left_type = $statements_source->getNodeTypeProvider()->getType($expr->left);
$right_type = $statements_source->getNodeTypeProvider()->getType($expr->right);

if ($left_type === null || $right_type === null) {
return true;
return null;
}

//on one hand, we're searching for literal 0
$literal_0 = Type::getInt(false, 0);
$left_contain_0 = false;
$right_contain_0 = false;
$other_operand = null;
$int_operand = null;

if (UnionTypeComparator::isContainedBy(
$codebase,
Expand Down Expand Up @@ -78,17 +82,19 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve

if (!$left_contain_0 && !$right_contain_0) {
// Not interested
return true;
return null;
} elseif ($left_contain_0 && $right_contain_0) {
//This is pretty inconclusive
return true;
return null;
}
assert($other_operand !== null);

//On the other hand, we're searching for any non-numeric non-empty string
if (!$other_operand->hasString()) {
//we can stop here, there's no string in here
return true;
return null;
}
assert($int_operand !== null);

$string_operand = $other_operand;

Expand All @@ -100,6 +106,12 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve
} elseif ($possibly_int instanceof TPositiveInt) {
//not interested
continue;
} elseif ($possibly_int instanceof TIntRange
&& ($possibly_int->min_bound > 0
|| $possibly_int->max_bound < 0
)) {
//not interested
continue;
} elseif ($possibly_int instanceof Type\Atomic\TInt) {
// we found a general Int, it may contain 0
$eligible_int = $possibly_int;
Expand Down Expand Up @@ -137,11 +149,12 @@ public static function afterExpressionAnalysis(AfterExpressionAnalysisEvent $eve
}
}

return true;
return null;
}
}


/** @api */
class InsaneComparison extends PluginIssue
{
}
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
>
<projectFiles>
<file name="sandbox.php"/>
<directory name="hooks"/>
</projectFiles>
<plugins>
<plugin filename="hooks/InsaneComparisonAnalyzer.php" />
Expand Down

0 comments on commit d9e608e

Please sign in to comment.