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 Type::isScalar() #2149

Merged
merged 3 commits into from Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryArrayListType.php
Expand Up @@ -322,6 +322,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

public function toNumber(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryLiteralStringType.php
Expand Up @@ -236,6 +236,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

public function traverse(callable $cb): Type
{
return $this;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonEmptyStringType.php
Expand Up @@ -236,6 +236,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

public function traverse(callable $cb): Type
{
return $this;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonFalsyStringType.php
Expand Up @@ -236,6 +236,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

public function traverse(callable $cb): Type
{
return $this;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNumericStringType.php
Expand Up @@ -239,6 +239,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

public function traverse(callable $cb): Type
{
return $this;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetType.php
Expand Up @@ -239,6 +239,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isScalar(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function getKeysArray(): Type
{
return new NonEmptyArrayType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetValueType.php
Expand Up @@ -290,6 +290,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isScalar(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function toNumber(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/NonEmptyArrayType.php
Expand Up @@ -307,6 +307,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

public function toNumber(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/OversizedArrayType.php
Expand Up @@ -306,6 +306,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

public function toNumber(): Type
{
return new ErrorType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ArrayType.php
Expand Up @@ -327,6 +327,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

public function isOffsetAccessible(): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/BooleanType.php
Expand Up @@ -113,6 +113,11 @@ public function isBoolean(): TrinaryLogic
return TrinaryLogic::createYes();
}

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

public function tryRemove(Type $typeToRemove): ?Type
{
if ($typeToRemove instanceof ConstantBooleanType) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/CallableType.php
Expand Up @@ -392,6 +392,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isScalar(): TrinaryLogic
{
return TrinaryLogic::createNo();
herndlm marked this conversation as resolved.
Show resolved Hide resolved
}

public function isCommonCallable(): bool
{
return $this->isCommonCallable;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ClosureType.php
Expand Up @@ -461,6 +461,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

/**
* @param mixed[] $properties
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Type/FloatType.php
Expand Up @@ -191,6 +191,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

public function traverse(callable $cb): Type
{
return $this;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IntegerType.php
Expand Up @@ -123,6 +123,11 @@ public function isInteger(): TrinaryLogic
return TrinaryLogic::createYes();
}

public function isScalar(): 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
Expand Up @@ -534,6 +534,11 @@ public function isVoid(): TrinaryLogic
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isVoid());
}

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

public function isOffsetAccessible(): TrinaryLogic
{
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isOffsetAccessible());
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IterableType.php
Expand Up @@ -330,6 +330,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
{
if ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/JustNullableTypeTrait.php
Expand Up @@ -117,4 +117,9 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isScalar(): TrinaryLogic
{
return TrinaryLogic::createNo();
herndlm marked this conversation as resolved.
Show resolved Hide resolved
}

}
11 changes: 11 additions & 0 deletions src/Type/MixedType.php
Expand Up @@ -776,6 +776,17 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isScalar(): TrinaryLogic
{
if ($this->subtractedType !== null) {
if ($this->subtractedType->isSuperTypeOf(new UnionType([new BooleanType(), new FloatType(), new IntegerType(), new StringType()]))->yes()) {
return TrinaryLogic::createNo();
}
}

return TrinaryLogic::createMaybe();
}

public function tryRemove(Type $typeToRemove): ?Type
{
if ($this->isSuperTypeOf($typeToRemove)->yes()) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NeverType.php
Expand Up @@ -403,6 +403,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

/**
* @param mixed[] $properties
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NullType.php
Expand Up @@ -247,6 +247,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

public function getSmallerType(): Type
{
return new NeverType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ObjectType.php
Expand Up @@ -909,6 +909,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

private function isExtraOffsetAccessibleClass(): TrinaryLogic
{
$classReflection = $this->getClassReflection();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/StaticType.php
Expand Up @@ -496,6 +496,11 @@ public function isVoid(): TrinaryLogic
return $this->getStaticObjectType()->isVoid();
}

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

/**
* @return ParametersAcceptor[]
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Type/StrictMixedType.php
Expand Up @@ -222,6 +222,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

public function isOffsetAccessible(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/StringType.php
Expand Up @@ -209,6 +209,11 @@ public function isClassStringType(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

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

public function hasMethod(string $methodName): TrinaryLogic
{
if ($this->isClassStringType()->yes()) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Traits/LateResolvableTypeTrait.php
Expand Up @@ -380,6 +380,11 @@ public function isVoid(): TrinaryLogic
return $this->resolve()->isVoid();
}

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

public function getSmallerType(): Type
{
return $this->resolve()->getSmallerType();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Traits/ObjectTypeTrait.php
Expand Up @@ -171,6 +171,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createNo();
}

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

public function toNumber(): Type
{
return new ErrorType();
Expand Down
2 changes: 2 additions & 0 deletions src/Type/Type.php
Expand Up @@ -171,6 +171,8 @@ public function isClassStringType(): TrinaryLogic;

public function isVoid(): TrinaryLogic;

public function isScalar(): TrinaryLogic;

public function getSmallerType(): Type;

public function getSmallerOrEqualType(): Type;
Expand Down
5 changes: 5 additions & 0 deletions src/Type/UnionType.php
Expand Up @@ -526,6 +526,11 @@ public function isVoid(): TrinaryLogic
return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isVoid());
}

public function isScalar(): TrinaryLogic
herndlm marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isScalar());
}

public function isOffsetAccessible(): TrinaryLogic
{
return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isOffsetAccessible());
Expand Down
5 changes: 5 additions & 0 deletions src/Type/VoidType.php
Expand Up @@ -169,6 +169,11 @@ public function isVoid(): TrinaryLogic
return TrinaryLogic::createYes();
}

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

public function traverse(callable $cb): Type
{
return $this;
Expand Down
29 changes: 29 additions & 0 deletions tests/PHPStan/Type/MixedTypeTest.php
Expand Up @@ -665,6 +665,35 @@ public function dataSubtractedIsVoid(): array
];
}

/** @dataProvider dataSubtractedIsScalar */
public function testSubtractedIsScalar(MixedType $mixedType, Type $typeToSubtract, TrinaryLogic $expectedResult): void
{
$subtracted = $mixedType->subtract($typeToSubtract);
$actualResult = $subtracted->isScalar();

$this->assertSame(
$expectedResult->describe(),
$actualResult->describe(),
sprintf('%s -> isScalar()', $subtracted->describe(VerbosityLevel::precise())),
);
}

public function dataSubtractedIsScalar(): array
{
return [
[
new MixedType(),
new UnionType([new BooleanType(), new FloatType(), new IntegerType(), new StringType()]),
TrinaryLogic::createNo(),
],
[
new MixedType(),
new StringType(),
TrinaryLogic::createMaybe(),
],
];
}

/**
* @dataProvider dataSubstractedIsLiteralString
*/
Expand Down