Skip to content

Commit

Permalink
Revert "Assertions for Integer Range"
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan authored Aug 14, 2021
1 parent 6cc5ba9 commit a9ead63
Show file tree
Hide file tree
Showing 9 changed files with 4 additions and 207 deletions.
1 change: 0 additions & 1 deletion psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
<errorLevel type="suppress">
<referencedClass name="PackageVersions\Versions"/>
<referencedClass name="Psalm\Plugin\Hook\*" />
<referencedClass name="Psalm\Type\Atomic\TPositiveInt" />
</errorLevel>
</DeprecatedClass>

Expand Down
4 changes: 0 additions & 4 deletions src/Psalm/Internal/Algebra.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,6 @@ public static function combineOredClauses(
array $right_clauses,
int $conditional_object_id
): array {
if (count($left_clauses) > 60000 || count($right_clauses) > 60000) {
return [];
}

$clauses = [];

$all_wedges = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static function scrapeAssertions(

if ($var_name) {
if ($candidate_if_types) {
$if_types[$var_name] = [['@' . \json_encode($candidate_if_types[0])]];
$if_types[$var_name] = [['>' . \json_encode($candidate_if_types[0])]];
} else {
$if_types[$var_name] = [['!falsy']];
}
Expand Down Expand Up @@ -1579,42 +1579,6 @@ protected static function hasPositiveNumberCheck(
return false;
}

/**
* @param PhpParser\Node\Expr\BinaryOp\Greater|PhpParser\Node\Expr\BinaryOp\GreaterOrEqual $conditional
* @return false|int
*/
protected static function hasSuperiorNumberCheck(
PhpParser\Node\Expr\BinaryOp $conditional,
?int &$literal_value_comparison
) {
if ($conditional->right instanceof PhpParser\Node\Scalar\LNumber) {
$literal_value_comparison = $conditional->right->value +
($conditional instanceof PhpParser\Node\Expr\BinaryOp\Greater ? 1 : 0);

return self::ASSIGNMENT_TO_RIGHT;
}

return false;
}

/**
* @param PhpParser\Node\Expr\BinaryOp\Smaller|PhpParser\Node\Expr\BinaryOp\SmallerOrEqual $conditional
* @return false|int
*/
protected static function hasInferiorNumberCheck(
PhpParser\Node\Expr\BinaryOp $conditional,
?int &$literal_value_comparison
) {
if ($conditional->right instanceof PhpParser\Node\Scalar\LNumber) {
$literal_value_comparison = $conditional->right->value +
($conditional instanceof PhpParser\Node\Expr\BinaryOp\Smaller ? -1 : 0);

return self::ASSIGNMENT_TO_RIGHT;
}

return false;
}

/**
* @param PhpParser\Node\Expr\BinaryOp\Greater|PhpParser\Node\Expr\BinaryOp\GreaterOrEqual $conditional
* @return false|int
Expand Down Expand Up @@ -3652,8 +3616,6 @@ private static function getGreaterAssertions(
$count_equality_position = self::hasNonEmptyCountEqualityCheck($conditional, $min_count);
$min_comparison = null;
$positive_number_position = self::hasPositiveNumberCheck($conditional, $min_comparison);
$superior_value_comparison = null;
$superior_value_position = self::hasSuperiorNumberCheck($conditional, $superior_value_comparison);
$zero_comparison = null;
$zero_position = self::hasZeroCheck($conditional, $zero_comparison);
$max_count = null;
Expand Down Expand Up @@ -3713,28 +3675,6 @@ private static function getGreaterAssertions(
return $if_types ? [$if_types] : [];
}

if ($superior_value_position) {
if ($superior_value_position === self::ASSIGNMENT_TO_RIGHT) {
$var_name = ExpressionIdentifier::getArrayVarId(
$conditional->left,
$this_class_name,
$source
);
} else {
$var_name = ExpressionIdentifier::getArrayVarId(
$conditional->right,
$this_class_name,
$source
);
}

if ($var_name) {
$if_types[$var_name] = [['=isset'], ['>' . $superior_value_comparison]];
}

return $if_types ? [$if_types] : [];
}

if ($positive_number_position) {
if ($positive_number_position === self::ASSIGNMENT_TO_RIGHT) {
$var_name = ExpressionIdentifier::getArrayVarId(
Expand Down Expand Up @@ -3864,8 +3804,6 @@ private static function getSmallerAssertions(
$min_count = null;
$count_equality_position = self::hasNonEmptyCountEqualityCheck($conditional, $min_count);
$typed_value_position = self::hasTypedValueComparison($conditional, $source);
$inferior_value_comparison = null;
$inferior_value_position = self::hasInferiorNumberCheck($conditional, $inferior_value_comparison);

$max_count = null;
$count_inequality_position = self::hasLessThanCountEqualityCheck($conditional, $max_count);
Expand Down Expand Up @@ -3920,28 +3858,6 @@ private static function getSmallerAssertions(
return $if_types ? [$if_types] : [];
}

if ($inferior_value_position) {
if ($inferior_value_position === self::ASSIGNMENT_TO_RIGHT) {
$var_name = ExpressionIdentifier::getArrayVarId(
$conditional->left,
$this_class_name,
$source
);
} else {
$var_name = ExpressionIdentifier::getArrayVarId(
$conditional->right,
$this_class_name,
$source
);
}

if ($var_name) {
$if_types[$var_name] = [['=isset'], ['<' . $inferior_value_comparison]];
}

return $if_types ? [$if_types] : [];
}

if ($typed_value_position) {
if ($typed_value_position === self::ASSIGNMENT_TO_RIGHT) {
$var_name = ExpressionIdentifier::getArrayVarId(
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Type/AssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static function reconcile(

$original_assertion = $assertion;

if ($assertion[0] === '@') {
if ($assertion[0] === '>') {
$assertion = 'falsy';
$is_negation = true;
}
Expand Down
44 changes: 0 additions & 44 deletions src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,50 +410,6 @@ public static function isContainedBy(
return false;
}

if ($input_type_part instanceof TInt && $container_type_part instanceof TIntRange) {
if ($input_type_part instanceof TPositiveInt) {
if ($container_type_part->min_bound > 1) {
//any positive int can't be pushed inside a range with a min > 1
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_scalar = true;
}

return false;
}

if ($container_type_part->max_bound !== null) {
//any positive int can't be pushed inside a range where the max bound isn't max without coercion
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_scalar = true;
}

return false;
}

return true;
}
if ($input_type_part instanceof TLiteralInt) {
$min_bound = $container_type_part->min_bound;
$max_bound = $container_type_part->max_bound;

return
($min_bound === null || $min_bound <= $input_type_part->value) &&
($max_bound === null || $max_bound >= $input_type_part->value);
}

//any int can't be pushed inside a range without coercion (unless the range is from min to max)
if ($container_type_part->min_bound !== null || $container_type_part->max_bound !== null) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
$atomic_comparison_result->type_coerced_from_scalar = true;
}
}

return false;
}

if (get_class($input_type_part) === TFloat::class && $container_type_part instanceof TLiteralFloat) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
Expand Down
59 changes: 0 additions & 59 deletions src/Psalm/Internal/Type/SimpleAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
use function count;
use function explode;
use function get_class;
use function max;
use function min;
use function strpos;
use function substr;

Expand Down Expand Up @@ -107,20 +105,6 @@ public static function reconcile(
);
}

if ($assertion[0] === '>') {
return self::reconcileSuperiorTo(
$existing_var_type,
substr($assertion, 1)
);
}

if ($assertion[0] === '<') {
return self::reconcileInferiorTo(
$existing_var_type,
substr($assertion, 1)
);
}

if ($assertion === 'falsy' || $assertion === 'empty') {
return self::reconcileFalsyOrEmpty(
$assertion,
Expand Down Expand Up @@ -1574,49 +1558,6 @@ private static function reconcileHasArrayKey(
return $existing_var_type;
}

private static function reconcileSuperiorTo(
Union $existing_var_type,
string $assertion
) : Union {
foreach ($existing_var_type->getAtomicTypes() as $atomic_type) {
if ($atomic_type instanceof Atomic\TIntRange) {
$existing_var_type->removeType($atomic_type->getKey());
if ($atomic_type->min_bound === null) {
$atomic_type->min_bound = (int)$assertion;
} else {
$atomic_type->min_bound = max($atomic_type->min_bound, (int)$assertion);
}
$existing_var_type->addType($atomic_type);
} elseif ($atomic_type instanceof TInt) {
$existing_var_type->removeType('int');
$existing_var_type->addType(new Atomic\TIntRange((int)$assertion, null));
}
}

return $existing_var_type;
}

private static function reconcileInferiorTo(
Union $existing_var_type,
string $assertion
) : Union {
foreach ($existing_var_type->getAtomicTypes() as $atomic_type) {
if ($atomic_type instanceof Atomic\TIntRange) {
$existing_var_type->removeType($atomic_type->getKey());
if ($atomic_type->max_bound === null) {
$atomic_type->max_bound = (int)$assertion;
} else {
$atomic_type->max_bound = min($atomic_type->max_bound, (int)$assertion);
}
$existing_var_type->addType($atomic_type);
} elseif ($atomic_type instanceof TInt) {
$existing_var_type->removeType('int');
$existing_var_type->addType(new Atomic\TIntRange(null, (int)$assertion));
}
}
return $existing_var_type;
}

/**
* @param string[] $suppressed_issues
* @param 0|1|2 $failed_reconciliation
Expand Down
1 change: 0 additions & 1 deletion src/Psalm/Type/Atomic/TPositiveInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

/**
* Denotes an int that is also positive (strictly > 0)
* @deprecated will be removed in Psalm 5, this is replaced by TIntRange when min_bound is at least 1
*/
class TPositiveInt extends TInt
{
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Type/Reconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ public static function reconcileKeyedTypes(
$orred_type = null;

foreach ($new_type_part_parts as $new_type_part_part) {
if ($new_type_part_part[0] === '@'
if ($new_type_part_part[0] === '>'
|| ($new_type_part_part[0] === '!'
&& $new_type_part_part[1] === '@')
&& $new_type_part_part[1] === '>')
) {
if ($new_type_part_part[0] === '!') {
$nested_negated = !$negated;
Expand Down
10 changes: 0 additions & 10 deletions tests/IntRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ function scope(int $a){
return $a;
}',
],
'intReduced' => [
'<?php
function getInt(): int{return 0;}
$a = getInt();
assert($a >= 500);
assert($a < 5000);',
'assertions' => [
'$a===' => 'int<500, 4999>'
]
],
];
}

Expand Down

0 comments on commit a9ead63

Please sign in to comment.