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

Add PhpVersion param on isSmallerThanOrEqual and isGreaterThanOrEqual #3478

Merged
merged 7 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,15 +751,15 @@ private function resolveType(string $exprString, Expr $node): Type
}

if ($node instanceof Expr\BinaryOp\SmallerOrEqual) {
return $this->getType($node->left)->isSmallerThanOrEqual($this->getType($node->right))->toBooleanType();
return $this->getType($node->left)->isSmallerThanOrEqual($this->getType($node->right), $this->phpVersion)->toBooleanType();
}

if ($node instanceof Expr\BinaryOp\Greater) {
return $this->getType($node->right)->isSmallerThan($this->getType($node->left))->toBooleanType();
}

if ($node instanceof Expr\BinaryOp\GreaterOrEqual) {
return $this->getType($node->right)->isSmallerThanOrEqual($this->getType($node->left))->toBooleanType();
return $this->getType($node->right)->isSmallerThanOrEqual($this->getType($node->left), $this->phpVersion)->toBooleanType();
}

if ($node instanceof Expr\BinaryOp\Equal) {
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,15 @@ public function getType(Expr $expr, InitializerExprContext $context): Type
}

if ($expr instanceof Expr\BinaryOp\SmallerOrEqual) {
return $this->getType($expr->left, $context)->isSmallerThanOrEqual($this->getType($expr->right, $context))->toBooleanType();
return $this->getType($expr->left, $context)->isSmallerThanOrEqual($this->getType($expr->right, $context), $this->phpVersion)->toBooleanType();
}

if ($expr instanceof Expr\BinaryOp\Greater) {
return $this->getType($expr->right, $context)->isSmallerThan($this->getType($expr->left, $context))->toBooleanType();
}

if ($expr instanceof Expr\BinaryOp\GreaterOrEqual) {
return $this->getType($expr->right, $context)->isSmallerThanOrEqual($this->getType($expr->left, $context))->toBooleanType();
return $this->getType($expr->right, $context)->isSmallerThanOrEqual($this->getType($expr->left, $context), $this->phpVersion)->toBooleanType();
}

if ($expr instanceof Expr\BinaryOp\LogicalXor) {
Expand Down
3 changes: 2 additions & 1 deletion src/Type/CompoundType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type;

use PHPStan\Php\PhpVersion;
use PHPStan\TrinaryLogic;

/** @api */
Expand All @@ -16,6 +17,6 @@ public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes):

public function isGreaterThan(Type $otherType): TrinaryLogic;
Copy link
Member

Choose a reason for hiding this comment

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

What about here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, var_dump('' < 0); is true on PHP 8 but false on PHP 7.4


public function isGreaterThanOrEqual(Type $otherType): TrinaryLogic;
public function isGreaterThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic;

}
3 changes: 2 additions & 1 deletion src/Type/Enum/EnumCaseObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Enum;

use PHPStan\Php\PhpVersion;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
Expand Down Expand Up @@ -184,7 +185,7 @@ public function isSmallerThan(Type $otherType): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
return TrinaryLogic::createNo();
}
Expand Down
12 changes: 6 additions & 6 deletions src/Type/IntegerRangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,18 @@ public function isSmallerThan(Type $otherType): TrinaryLogic
return TrinaryLogic::extremeIdentity($minIsSmaller, $maxIsSmaller);
}

public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
if ($this->min === null) {
$minIsSmaller = TrinaryLogic::createYes();
} else {
$minIsSmaller = (new ConstantIntegerType($this->min))->isSmallerThanOrEqual($otherType);
$minIsSmaller = (new ConstantIntegerType($this->min))->isSmallerThanOrEqual($otherType, $phpVersion);
}

if ($this->max === null) {
$maxIsSmaller = TrinaryLogic::createNo();
} else {
$maxIsSmaller = (new ConstantIntegerType($this->max))->isSmallerThanOrEqual($otherType);
$maxIsSmaller = (new ConstantIntegerType($this->max))->isSmallerThanOrEqual($otherType, $phpVersion);
}

return TrinaryLogic::extremeIdentity($minIsSmaller, $maxIsSmaller);
Expand All @@ -359,18 +359,18 @@ public function isGreaterThan(Type $otherType): TrinaryLogic
return TrinaryLogic::extremeIdentity($minIsSmaller, $maxIsSmaller);
}

public function isGreaterThanOrEqual(Type $otherType): TrinaryLogic
public function isGreaterThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
if ($this->min === null) {
$minIsSmaller = TrinaryLogic::createNo();
} else {
$minIsSmaller = $otherType->isSmallerThanOrEqual((new ConstantIntegerType($this->min)));
$minIsSmaller = $otherType->isSmallerThanOrEqual((new ConstantIntegerType($this->min)), $phpVersion);
}

if ($this->max === null) {
$maxIsSmaller = TrinaryLogic::createYes();
} else {
$maxIsSmaller = $otherType->isSmallerThanOrEqual((new ConstantIntegerType($this->max)));
$maxIsSmaller = $otherType->isSmallerThanOrEqual((new ConstantIntegerType($this->max)), $phpVersion);
}

return TrinaryLogic::extremeIdentity($minIsSmaller, $maxIsSmaller);
Expand Down
8 changes: 4 additions & 4 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,9 +807,9 @@ public function isSmallerThan(Type $otherType): TrinaryLogic
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isSmallerThan($otherType));
}

public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isSmallerThanOrEqual($otherType));
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isSmallerThanOrEqual($otherType, $phpVersion));
}

public function isNull(): TrinaryLogic
Expand Down Expand Up @@ -881,9 +881,9 @@ public function isGreaterThan(Type $otherType): TrinaryLogic
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $otherType->isSmallerThan($type));
}

public function isGreaterThanOrEqual(Type $otherType): TrinaryLogic
public function isGreaterThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $otherType->isSmallerThanOrEqual($type));
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $otherType->isSmallerThanOrEqual($type, $phpVersion));
}

public function getSmallerType(): Type
Expand Down
4 changes: 2 additions & 2 deletions src/Type/NullType.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ public function isSmallerThan(Type $otherType): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
if ($otherType instanceof ConstantScalarType) {
return TrinaryLogic::createFromBoolean(null <= $otherType->getValue());
}

if ($otherType instanceof CompoundType) {
return $otherType->isGreaterThanOrEqual($this);
return $otherType->isGreaterThanOrEqual($this, $phpVersion);
}

return TrinaryLogic::createMaybe();
Expand Down
4 changes: 2 additions & 2 deletions src/Type/Traits/ConstantScalarTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ public function isSmallerThan(Type $otherType): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
if ($otherType instanceof ConstantScalarType) {
return TrinaryLogic::createFromBoolean($this->value <= $otherType->getValue());
}

if ($otherType instanceof CompoundType) {
return $otherType->isGreaterThanOrEqual($this);
return $otherType->isGreaterThanOrEqual($this, $phpVersion);
}

return TrinaryLogic::createMaybe();
Expand Down
10 changes: 5 additions & 5 deletions src/Type/Traits/LateResolvableTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,9 @@ public function isSmallerThan(Type $otherType): TrinaryLogic
return $this->resolve()->isSmallerThan($otherType);
}

public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
return $this->resolve()->isSmallerThanOrEqual($otherType);
return $this->resolve()->isSmallerThanOrEqual($otherType, $phpVersion);
}

public function isNull(): TrinaryLogic
Expand Down Expand Up @@ -550,15 +550,15 @@ public function isGreaterThan(Type $otherType): TrinaryLogic
return $otherType->isSmallerThan($result);
}

public function isGreaterThanOrEqual(Type $otherType): TrinaryLogic
public function isGreaterThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
$result = $this->resolve();

if ($result instanceof CompoundType) {
return $result->isGreaterThanOrEqual($otherType);
return $result->isGreaterThanOrEqual($otherType, $phpVersion);
}

return $otherType->isSmallerThanOrEqual($result);
return $otherType->isSmallerThanOrEqual($result, $phpVersion);
}

public function exponentiate(Type $exponent): Type
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Traits/UndecidedComparisonCompoundTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Traits;

use PHPStan\Php\PhpVersion;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;

Expand All @@ -15,7 +16,7 @@ public function isGreaterThan(Type $otherType): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isGreaterThanOrEqual(Type $otherType): TrinaryLogic
public function isGreaterThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Traits/UndecidedComparisonTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Traits;

use PHPStan\Php\PhpVersion;
use PHPStan\TrinaryLogic;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
Expand All @@ -14,7 +15,7 @@ public function isSmallerThan(Type $otherType): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function toArrayKey(): Type;

public function isSmallerThan(Type $otherType): TrinaryLogic;
Copy link
Member

Choose a reason for hiding this comment

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

What about here?


public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic;
public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic;

/**
* Is Type of a known constant value? Includes literal strings, integers, floats, true, false, null, and array shapes.
Expand Down
8 changes: 4 additions & 4 deletions src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,9 +788,9 @@ public function isSmallerThan(Type $otherType): TrinaryLogic
return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isSmallerThan($otherType));
}

public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isSmallerThanOrEqual($otherType));
return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isSmallerThanOrEqual($otherType, $phpVersion));
}

public function isNull(): TrinaryLogic
Expand Down Expand Up @@ -868,9 +868,9 @@ public function isGreaterThan(Type $otherType): TrinaryLogic
return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $otherType->isSmallerThan($type));
}

public function isGreaterThanOrEqual(Type $otherType): TrinaryLogic
public function isGreaterThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
{
return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $otherType->isSmallerThanOrEqual($type));
return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $otherType->isSmallerThanOrEqual($type, $phpVersion));
}

public function toBoolean(): BooleanType
Expand Down
12 changes: 6 additions & 6 deletions tests/PHPStan/Rules/Api/ApiClassImplementsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,32 @@ public function testRuleOutOfPhpStan(): void
$this->analyse([__DIR__ . '/data/class-implements-out-of-phpstan.php'], [
[
'Implementing PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider is not covered by backward compatibility promise. The interface might change in a minor PHPStan version.',
19,
20,
$tip,
],
[
'Implementing PHPStan\Type\Type is not covered by backward compatibility promise. The interface might change in a minor PHPStan version.',
53,
54,
$tip,
],
[
'Implementing PHPStan\Reflection\ReflectionProvider is not covered by backward compatibility promise. The interface might change in a minor PHPStan version.',
338,
339,
$tip,
],
[
'Implementing PHPStan\Analyser\Scope is not covered by backward compatibility promise. The interface might change in a minor PHPStan version.',
343,
344,
$tip,
],
[
'Implementing PHPStan\Reflection\FunctionReflection is not covered by backward compatibility promise. The interface might change in a minor PHPStan version.',
348,
349,
$tip,
],
[
'Implementing PHPStan\Reflection\ExtendedMethodReflection is not covered by backward compatibility promise. The interface might change in a minor PHPStan version.',
352,
353,
$tip,
],
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\FunctionReflection;
Expand Down Expand Up @@ -252,7 +253,7 @@ public function isSmallerThan(Type $otherType): \PHPStan\TrinaryLogic
// TODO: Implement isSmallerThan() method.
}

public function isSmallerThanOrEqual(Type $otherType): \PHPStan\TrinaryLogic
public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): \PHPStan\TrinaryLogic
{
// TODO: Implement isSmallerThanOrEqual() method.
}
Expand Down
Loading