Skip to content

implement isInteger() on Type #1971

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

Merged
merged 2 commits into from
Nov 6, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $ri

$resultType = TypeCombinator::union($leftNumberType, $rightNumberType);
if ($expr instanceof Expr\BinaryOp\Div) {
if ($types instanceof MixedType || $resultType instanceof IntegerType) {
if ($types instanceof MixedType || $resultType->isInteger()->yes()) {
return new BenevolentUnionType([new IntegerType(), new FloatType()]);
}

Expand Down Expand Up @@ -1925,7 +1925,7 @@ public function getBitwiseNotType(Expr $expr, callable $getTypeCallback): Type

return TypeCombinator::intersect(...$accessories);
}
if ($type instanceof IntegerType || $type instanceof FloatType) {
if ($type->isInteger()->yes() || $type instanceof FloatType) {
return new IntegerType(); //no const types here, result depends on PHP_INT_SIZE
}
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryArrayListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createYes();
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryLiteralStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ public function toArrayKey(): Type
return $this;
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonEmptyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ public function toArrayKey(): Type
return $this;
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonFalsyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ public function toArrayKey(): Type
return $this;
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNumericStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ public function toArrayKey(): Type
return new IntegerType();
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/NonEmptyArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/OversizedArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ public function isOversizedArray(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ public function traverse(callable $cb): Type
);
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
7 changes: 6 additions & 1 deletion src/Type/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getReferencedClasses(): array

public function accepts(Type $type, bool $strictTypes): TrinaryLogic
{
if ($type instanceof self || $type instanceof IntegerType) {
if ($type instanceof self || $type->isInteger()->yes()) {
return TrinaryLogic::createYes();
}

Expand Down Expand Up @@ -121,6 +121,11 @@ public function toArrayKey(): Type
return new IntegerType();
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
6 changes: 6 additions & 0 deletions src/Type/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type;

use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand Down Expand Up @@ -87,6 +88,11 @@ public function toArrayKey(): Type
return $this;
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createYes();
}

public function tryRemove(Type $typeToRemove): ?Type
{
if ($typeToRemove instanceof IntegerRangeType || $typeToRemove instanceof ConstantIntegerType) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@ public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isSmallerThanOrEqual($otherType));
}

public function isInteger(): TrinaryLogic
{
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isInteger());
}

public function isGreaterThan(Type $otherType): TrinaryLogic
{
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $otherType->isSmallerThan($type));
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IterableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ public function getLastIterableValueType(): Type
return $this->getItemType();
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/JustNullableTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function traverse(callable $cb): Type
return $this;
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
11 changes: 11 additions & 0 deletions src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,17 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isInteger(): TrinaryLogic
{
if ($this->subtractedType !== null) {
if ($this->subtractedType->isSuperTypeOf(new IntegerType())->yes()) {
return TrinaryLogic::createNo();
}
}

return TrinaryLogic::createMaybe();
}

public function isString(): TrinaryLogic
{
if ($this->subtractedType !== null) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NeverType.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ public function traverse(callable $cb): Type
return $this;
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NullType.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ public function traverse(callable $cb): Type
return $this;
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,11 @@ public function getLastIterableValueType(): Type
return $this->getIterableValueType();
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Php/BcMathStringOrNullReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

$secondArgument = $scope->getType($functionCall->getArgs()[1]->value);
$secondArgumentIsNumeric = ($secondArgument instanceof ConstantScalarType && is_numeric($secondArgument->getValue())) || $secondArgument instanceof IntegerType;
$secondArgumentIsNumeric = ($secondArgument instanceof ConstantScalarType && is_numeric($secondArgument->getValue())) || $secondArgument->isInteger()->yes();

if ($secondArgument instanceof ConstantScalarType && ($this->isZero($secondArgument->getValue()) || !$secondArgumentIsNumeric)) {
if ($this->phpVersion->throwsTypeErrorForInternalFunctions()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Type/Php/FilterVarDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function getTypeFromFunctionCall(
private function determineExactType(Type $in, int $filterValue, Type $defaultType, ?Arg $flagsArg, Scope $scope): ?Type
{
if (($filterValue === $this->getConstant('FILTER_VALIDATE_BOOLEAN') && $in instanceof BooleanType)
|| ($filterValue === $this->getConstant('FILTER_VALIDATE_INT') && $in instanceof IntegerType)
|| ($filterValue === $this->getConstant('FILTER_VALIDATE_INT') && $in->isInteger()->yes())
|| ($filterValue === $this->getConstant('FILTER_VALIDATE_FLOAT') && $in instanceof FloatType)) {
return $in;
}
Expand All @@ -229,7 +229,7 @@ private function determineExactType(Type $in, int $filterValue, Type $defaultTyp
return preg_match('/\A[+-]?(?:0|[1-9][0-9]*)\z/', $value) === 1 ? $in->toInteger() : $defaultType;
}

if ($filterValue === $this->getConstant('FILTER_VALIDATE_FLOAT') && $in instanceof IntegerType) {
if ($filterValue === $this->getConstant('FILTER_VALIDATE_FLOAT') && $in->isInteger()->yes()) {
return $in->toFloat();
}

Expand Down
5 changes: 5 additions & 0 deletions src/Type/StaticType.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ public function isList(): TrinaryLogic
return $this->getStaticObjectType()->isList();
}

public function isInteger(): TrinaryLogic
{
return $this->getStaticObjectType()->isInteger();
}

public function isString(): TrinaryLogic
{
return $this->getStaticObjectType()->isString();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/StrictMixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ public function getIterableValueType(): Type
return $this;
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/StringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public function toArrayKey(): Type
return $this;
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Traits/LateResolvableTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic
return $this->resolve()->isSmallerThanOrEqual($otherType);
}

public function isInteger(): TrinaryLogic
{
return $this->resolve()->isInteger();
}

public function isString(): TrinaryLogic
{
return $this->resolve()->isString();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Traits/ObjectTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public function isCloneable(): TrinaryLogic
return TrinaryLogic::createYes();
}

public function isInteger(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isString(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
2 changes: 2 additions & 0 deletions src/Type/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public function isSmallerThan(Type $otherType): TrinaryLogic;

public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic;

public function isInteger(): TrinaryLogic;

public function isString(): TrinaryLogic;

public function isNumericString(): TrinaryLogic;
Expand Down
Loading