Skip to content

Commit

Permalink
Refactor PreInc and PreDec
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 1, 2021
1 parent c737095 commit a6a23dc
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Analyser/MutatingScope.php
Expand Up @@ -1763,6 +1763,7 @@ private function resolveType(Expr $node): Type
} elseif ($node instanceof Expr\PreInc || $node instanceof Expr\PreDec) {
$varType = $this->getType($node->var);
$varScalars = TypeUtils::getConstantScalars($varType);
$stringType = new StringType();
if (count($varScalars) > 0) {
$newTypes = [];

Expand All @@ -1777,19 +1778,18 @@ private function resolveType(Expr $node): Type
$newTypes[] = $this->getTypeFromValue($varValue);
}
return TypeCombinator::union(...$newTypes);
} elseif ($varType instanceof IntegerRangeType) {
return $varType->shift($node instanceof Expr\PreInc ? +1 : -1);
}

$stringType = new StringType();
if ($stringType->isSuperTypeOf($varType)->yes()) {
} elseif ($stringType->isSuperTypeOf($varType)->yes()) {
if ($varType->isLiteralString()->yes()) {
return new IntersectionType([$stringType, new AccessoryLiteralStringType()]);
}
return $stringType;
}

return $varType->toNumber();
if ($node instanceof Expr\PreInc) {
return $this->getType(new BinaryOp\Plus($node->var, new LNumber(1)));
}

return $this->getType(new BinaryOp\Minus($node->var, new LNumber(1)));
} elseif ($node instanceof Expr\Yield_) {
$functionReflection = $this->getFunction();
if ($functionReflection === null) {
Expand Down

2 comments on commit a6a23dc

@staabm
Copy link
Contributor

@staabm staabm commented on a6a23dc Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one makes me think, whether we could do similar stuff here:

if ($leftType instanceof ConstantIntegerType) {
if ($expr->right instanceof Expr\PostInc) {
$result = $result->unionWith($this->createRangeTypes(
$expr->right->var,
IntegerRangeType::fromInterval($leftType->getValue(), null, $offset + 1),
$context
));
} elseif ($expr->right instanceof Expr\PostDec) {
$result = $result->unionWith($this->createRangeTypes(
$expr->right->var,
IntegerRangeType::fromInterval($leftType->getValue(), null, $offset - 1),
$context
));
} elseif ($expr->right instanceof Expr\PreInc || $expr->right instanceof Expr\PreDec) {
$result = $result->unionWith($this->createRangeTypes(
$expr->right->var,
IntegerRangeType::fromInterval($leftType->getValue(), null, $offset),
$context
));
}
}
if ($rightType instanceof ConstantIntegerType) {
if ($expr->left instanceof Expr\PostInc) {
$result = $result->unionWith($this->createRangeTypes(
$expr->left->var,
IntegerRangeType::fromInterval(null, $rightType->getValue(), -$offset + 1),
$context
));
} elseif ($expr->left instanceof Expr\PostDec) {
$result = $result->unionWith($this->createRangeTypes(
$expr->left->var,
IntegerRangeType::fromInterval(null, $rightType->getValue(), -$offset - 1),
$context
));
} elseif ($expr->left instanceof Expr\PreInc || $expr->left instanceof Expr\PreDec) {
$result = $result->unionWith($this->createRangeTypes(
$expr->left->var,
IntegerRangeType::fromInterval(null, $rightType->getValue(), -$offset),
$context
));
}
}

@ondrejmirtes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want ++$i < 5 to be the same as ($i + 1) < 5 then yeah, definitely.

Please sign in to comment.