Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve specified type in comparisons #390

Merged
merged 4 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 41 additions & 12 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ public function specifyTypesInCondition(
);

} elseif ($expr instanceof Node\Expr\BinaryOp\Smaller || $expr instanceof Node\Expr\BinaryOp\SmallerOrEqual) {
$offset = $expr instanceof Node\Expr\BinaryOp\Smaller ? 1 : 0;
$orEqual = $expr instanceof Node\Expr\BinaryOp\SmallerOrEqual;
$offset = $orEqual ? 0 : 1;
$leftType = $scope->getType($expr->left);
$rightType = $scope->getType($expr->right);

Expand Down Expand Up @@ -426,12 +427,6 @@ public function specifyTypesInCondition(
$context
));
}

$result = $result->unionWith($this->createRangeTypes(
$expr->right,
IntegerRangeType::fromInterval($leftType->getValue(), null, $offset),
$context
));
}

if ($rightType instanceof ConstantIntegerType) {
Expand All @@ -454,12 +449,46 @@ public function specifyTypesInCondition(
$context
));
}
}

$result = $result->unionWith($this->createRangeTypes(
$expr->left,
IntegerRangeType::fromInterval(null, $rightType->getValue(), -$offset),
$context
));
if ($context->truthy()) {
if (!$expr->left instanceof Node\Scalar) {
$result = $result->unionWith(
$this->create(
$expr->left,
$orEqual ? $rightType->getSmallerOrEqualType() : $rightType->getSmallerType(),
TypeSpecifierContext::createTruthy()
)
);
}
if (!$expr->right instanceof Node\Scalar) {
$result = $result->unionWith(
$this->create(
$expr->right,
$orEqual ? $leftType->getGreaterOrEqualType() : $leftType->getGreaterType(),
TypeSpecifierContext::createTruthy()
)
);
}
} elseif ($context->falsey()) {
if (!$expr->left instanceof Node\Scalar) {
$result = $result->unionWith(
$this->create(
$expr->left,
$orEqual ? $rightType->getGreaterType() : $rightType->getGreaterOrEqualType(),
TypeSpecifierContext::createTruthy()
)
);
}
if (!$expr->right instanceof Node\Scalar) {
$result = $result->unionWith(
$this->create(
$expr->right,
$orEqual ? $leftType->getSmallerType() : $leftType->getSmallerOrEqualType(),
TypeSpecifierContext::createTruthy()
)
);
}
}

return $result;
Expand Down
35 changes: 35 additions & 0 deletions src/Type/Constant/ConstantBooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use PHPStan\Type\BooleanType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\Traits\ConstantScalarTypeTrait;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
Expand All @@ -30,6 +33,38 @@ public function describe(VerbosityLevel $level): string
return $this->value ? 'true' : 'false';
}

public function getSmallerType(): Type
{
if ($this->value) {
return StaticTypeFactory::falsey();
}
return new NeverType();
}

public function getSmallerOrEqualType(): Type
{
if ($this->value) {
return new MixedType();
}
return StaticTypeFactory::falsey();
}

public function getGreaterType(): Type
{
if ($this->value) {
return new NeverType();
}
return StaticTypeFactory::truthy();
}

public function getGreaterOrEqualType(): Type
{
if ($this->value) {
return StaticTypeFactory::truthy();
}
return new MixedType();
}

public function toBoolean(): BooleanType
{
return $this;
Expand Down
2 changes: 2 additions & 0 deletions src/Type/Constant/ConstantFloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Type\CompoundType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\FloatType;
use PHPStan\Type\Traits\ConstantNumericComparisonTypeTrait;
use PHPStan\Type\Traits\ConstantScalarTypeTrait;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
Expand All @@ -15,6 +16,7 @@ class ConstantFloatType extends FloatType implements ConstantScalarType

use ConstantScalarTypeTrait;
use ConstantScalarToBooleanTrait;
use ConstantNumericComparisonTypeTrait;

private float $value;

Expand Down
2 changes: 2 additions & 0 deletions src/Type/Constant/ConstantIntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\Traits\ConstantNumericComparisonTypeTrait;
use PHPStan\Type\Traits\ConstantScalarTypeTrait;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
Expand All @@ -16,6 +17,7 @@ class ConstantIntegerType extends IntegerType implements ConstantScalarType

use ConstantScalarTypeTrait;
use ConstantScalarToBooleanTrait;
use ConstantNumericComparisonTypeTrait;

private int $value;

Expand Down
62 changes: 62 additions & 0 deletions src/Type/Constant/ConstantStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
use PHPStan\Type\ErrorType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StringType;
use PHPStan\Type\Traits\ConstantScalarTypeTrait;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\VerbosityLevel;

class ConstantStringType extends StringType implements ConstantScalarType
Expand Down Expand Up @@ -299,6 +302,65 @@ public function generalize(): Type
return new StringType();
}

public function getSmallerType(): Type
{
$subtractedTypes = [
new ConstantBooleanType(true),
IntegerRangeType::createAllGreaterThanOrEqualTo((float) $this->value),
];

if ($this->value === '') {
$subtractedTypes[] = new NullType();
$subtractedTypes[] = new StringType();
}

if (!(bool) $this->value) {
$subtractedTypes[] = new ConstantBooleanType(false);
}

return TypeCombinator::remove(new MixedType(), TypeCombinator::union(...$subtractedTypes));
}

public function getSmallerOrEqualType(): Type
{
$subtractedTypes = [
IntegerRangeType::createAllGreaterThan((float) $this->value),
];

if (!(bool) $this->value) {
$subtractedTypes[] = new ConstantBooleanType(true);
}

return TypeCombinator::remove(new MixedType(), TypeCombinator::union(...$subtractedTypes));
}

public function getGreaterType(): Type
{
$subtractedTypes = [
new ConstantBooleanType(false),
IntegerRangeType::createAllSmallerThanOrEqualTo((float) $this->value),
];

if ((bool) $this->value) {
$subtractedTypes[] = new ConstantBooleanType(true);
}

return TypeCombinator::remove(new MixedType(), TypeCombinator::union(...$subtractedTypes));
}

public function getGreaterOrEqualType(): Type
{
$subtractedTypes = [
IntegerRangeType::createAllSmallerThan((float) $this->value),
];

if ((bool) $this->value) {
$subtractedTypes[] = new ConstantBooleanType(false);
}

return TypeCombinator::remove(new MixedType(), TypeCombinator::union(...$subtractedTypes));
}

/**
* @param mixed[] $properties
* @return Type
Expand Down