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

Use dedicated Type methods over isSuperTypeOf() #2772

Merged
merged 3 commits into from
Nov 26, 2023
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
16 changes: 8 additions & 8 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,8 @@ private function resolveType(string $exprString, Expr $node): Type

if ($node instanceof Expr\Empty_) {
$result = $this->issetCheck($node->expr, static function (Type $type): ?bool {
$isNull = (new NullType())->isSuperTypeOf($type);
$isFalsey = (new ConstantBooleanType(false))->isSuperTypeOf($type->toBoolean());
$isNull = $type->isNull();
$isFalsey = $type->toBoolean()->isFalse();
if ($isNull->maybe()) {
return null;
}
Expand Down Expand Up @@ -1504,7 +1504,7 @@ private function resolveType(string $exprString, Expr $node): Type

$filteringExprType = $matchScope->getType($filteringExpr);

if (!(new ConstantBooleanType(false))->isSuperTypeOf($filteringExprType)->yes()) {
if (!$filteringExprType->isFalse()->yes()) {
$truthyScope = $matchScope->filterByTruthyValue($filteringExpr);
$types[] = $truthyScope->getType($arm->body);
}
Expand All @@ -1519,7 +1519,7 @@ private function resolveType(string $exprString, Expr $node): Type
$issetResult = true;
foreach ($node->vars as $var) {
$result = $this->issetCheck($var, static function (Type $type): ?bool {
$isNull = (new NullType())->isSuperTypeOf($type);
$isNull = $type->isNull();
if ($isNull->maybe()) {
return null;
}
Expand Down Expand Up @@ -1552,7 +1552,7 @@ private function resolveType(string $exprString, Expr $node): Type
$leftType = $this->getType($node->left);

$result = $this->issetCheck($node->left, static function (Type $type): ?bool {
$isNull = (new NullType())->isSuperTypeOf($type);
$isNull = $type->isNull();
if ($isNull->maybe()) {
return null;
}
Expand Down Expand Up @@ -2433,7 +2433,7 @@ public function isInClassExists(string $className): bool
new Arg(new String_(ltrim($className, '\\'))),
]);

return (new ConstantBooleanType(true))->isSuperTypeOf($this->getType($expr))->yes();
return $this->getType($expr)->isTrue()->yes();
}

/** @api */
Expand All @@ -2443,7 +2443,7 @@ public function isInFunctionExists(string $functionName): bool
new Arg(new String_(ltrim($functionName, '\\'))),
]);

return (new ConstantBooleanType(true))->isSuperTypeOf($this->getType($expr))->yes();
return $this->getType($expr)->isTrue()->yes();
}

/** @api */
Expand Down Expand Up @@ -2993,7 +2993,7 @@ private function expressionTypeIsUnchangeable(ExpressionTypeHolder $typeHolder):
&& $expr->name->toLowerString() === 'function_exists'
&& isset($expr->getArgs()[0])
&& count($this->getType($expr->getArgs()[0]->value)->getConstantStrings()) === 1
&& (new ConstantBooleanType(true))->isSuperTypeOf($type)->yes();
&& $type->isTrue()->yes();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ public function specifyTypesInCondition(
} elseif (
$expr instanceof Expr\BinaryOp\Coalesce
&& $context->true()
&& ((new ConstantBooleanType(false))->isSuperTypeOf($scope->getType($expr->right))->yes())
&& $scope->getType($expr->right)->isFalse()->yes()
) {
return $this->create(
$expr->left,
Expand All @@ -801,7 +801,7 @@ public function specifyTypesInCondition(
} elseif (
$expr instanceof Expr\Ternary
&& !$context->null()
&& ((new ConstantBooleanType(false))->isSuperTypeOf($scope->getType($expr->else))->yes())
&& $scope->getType($expr->else)->isFalse()->yes()
) {
$conditionExpr = $expr->cond;
if ($expr->if !== null) {
Expand Down
14 changes: 9 additions & 5 deletions src/Rules/IssetCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Rules\Properties\PropertyDescriptor;
use PHPStan\Rules\Properties\PropertyReflectionFinder;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function is_string;
Expand Down Expand Up @@ -44,11 +45,14 @@ public function check(Expr $expr, Scope $scope, string $operatorDescription, cal
return null;
}

return $this->generateError(
$this->treatPhpDocTypesAsCertain ? $scope->getType($expr) : $scope->getNativeType($expr),
sprintf('Variable $%s %s always exists and', $expr->name, $operatorDescription),
$typeMessageCallback,
);
$type = $this->treatPhpDocTypesAsCertain ? $scope->getType($expr) : $scope->getNativeType($expr);
if (!$type instanceof NeverType) {
return $this->generateError(
$type,
sprintf('Variable $%s %s always exists and', $expr->name, $operatorDescription),
$typeMessageCallback,
);
}
Comment on lines +48 to +55
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this changes address the comments in #2772 (comment)

everything else in this PR was a mechnical change from
((new ConstantBooleanType(false))->isSuperTypeOf($type)->yes()) to $type->isFalse()->yes() etc.

}

return RuleErrorBuilder::message(sprintf('Variable $%s %s is never defined.', $expr->name, $operatorDescription))->build();
Expand Down
6 changes: 2 additions & 4 deletions src/Rules/Variables/EmptyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IssetCheck;
use PHPStan\Rules\Rule;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;

/**
Expand All @@ -28,11 +26,11 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope): array
{
$error = $this->issetCheck->check($node->expr, $scope, 'in empty()', static function (Type $type): ?string {
$isNull = (new NullType())->isSuperTypeOf($type);
$isFalsey = (new ConstantBooleanType(false))->isSuperTypeOf($type->toBoolean());
$isNull = $type->isNull();
if ($isNull->maybe()) {
return null;
}
$isFalsey = $type->toBoolean()->isFalse();
if ($isFalsey->maybe()) {
return null;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Variables/IssetRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IssetCheck;
use PHPStan\Rules\Rule;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;

/**
Expand All @@ -29,7 +28,7 @@ public function processNode(Node $node, Scope $scope): array
$messages = [];
foreach ($node->vars as $var) {
$error = $this->issetCheck->check($var, $scope, 'in isset()', static function (Type $type): ?string {
$isNull = (new NullType())->isSuperTypeOf($type);
$isNull = $type->isNull();
if ($isNull->maybe()) {
return null;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/Variables/NullCoalesceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IssetCheck;
use PHPStan\Rules\Rule;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;

/**
Expand All @@ -27,7 +26,7 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope): array
{
$typeMessageCallback = static function (Type $type): ?string {
$isNull = (new NullType())->isSuperTypeOf($type);
$isNull = $type->isNull();
if ($isNull->maybe()) {
return null;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Type/Php/ArrayColumnFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
Expand Down Expand Up @@ -147,7 +146,7 @@ private function handleConstantArray(ConstantArrayType $arrayType, Type $columnT

private function getOffsetOrProperty(Type $type, Type $offsetOrProperty, Scope $scope, bool $allowMaybe): ?Type
{
$offsetIsNull = (new NullType())->isSuperTypeOf($offsetOrProperty);
$offsetIsNull = $offsetOrProperty->isNull();
if ($offsetIsNull->yes()) {
return $type;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Type/Php/ArrayMapFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
Expand All @@ -38,7 +37,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

$singleArrayArgument = !isset($functionCall->getArgs()[2]);
$callableType = $scope->getType($functionCall->getArgs()[0]->value);
$callableIsNull = (new NullType())->isSuperTypeOf($callableType)->yes();
$callableIsNull = $callableType->isNull()->yes();

if ($callableType->isCallable()->yes()) {
$valueType = new NeverType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public function getTypeFromFunctionCall(
return new BenevolentUnionType([new StringType(), new ConstantBooleanType(false)]);
}

$isTrueType = (new ConstantBooleanType(true))->isSuperTypeOf($argType);
$isFalseType = (new ConstantBooleanType(false))->isSuperTypeOf($argType);
$isTrueType = $argType->isTrue();
$isFalseType = $argType->isFalse();
$compareTypes = $isTrueType->compareTo($isFalseType);
if ($compareTypes === $isTrueType) {
return new UnionType([new StringType(), new ConstantBooleanType(false)]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\FloatType;
Expand Down Expand Up @@ -44,8 +43,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

$argType = $scope->getType($functionCall->getArgs()[0]->value);
$isTrueType = (new ConstantBooleanType(true))->isSuperTypeOf($argType);
$isFalseType = (new ConstantBooleanType(false))->isSuperTypeOf($argType);
$isTrueType = $argType->isTrue();
$isFalseType = $argType->isFalse();
$compareTypes = $isTrueType->compareTo($isFalseType);
if ($compareTypes === $isTrueType) {
return $floatType;
Expand Down
5 changes: 2 additions & 3 deletions src/Type/Php/HrtimeFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\FloatType;
Expand Down Expand Up @@ -35,8 +34,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

$argType = $scope->getType($functionCall->getArgs()[0]->value);
$isTrueType = (new ConstantBooleanType(true))->isSuperTypeOf($argType);
$isFalseType = (new ConstantBooleanType(false))->isSuperTypeOf($argType);
$isTrueType = $argType->isTrue();
$isFalseType = $argType->isFalse();
$compareTypes = $isTrueType->compareTo($isFalseType);
if ($compareTypes === $isTrueType) {
return $numberType;
Expand Down
3 changes: 1 addition & 2 deletions src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\TypeCombinator;
Expand Down Expand Up @@ -41,7 +40,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
$argsCount = count($node->getArgs());
if ($argsCount >= 3) {
$strictNodeType = $scope->getType($node->getArgs()[2]->value);
$isStrictComparison = (new ConstantBooleanType(true))->isSuperTypeOf($strictNodeType)->yes();
$isStrictComparison = $strictNodeType->isTrue()->yes();
}

if ($argsCount < 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function in_array;
Expand Down Expand Up @@ -57,8 +55,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

$argType = $scope->getType($functionCall->getArgs()[0]->value);
$isString = $argType->isString();
$isNull = (new NullType())->isSuperTypeOf($argType);
$isInteger = (new IntegerType())->isSuperTypeOf($argType);
$isNull = $argType->isNull();
$isInteger = $argType->isInteger();

if ($isString->no() && $isNull->no() && $isInteger->no()) {
if ($this->phpVersion->throwsTypeErrorForInternalFunctions()) {
Expand Down
5 changes: 2 additions & 3 deletions src/Type/Php/MicrotimeFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\FloatType;
use PHPStan\Type\MixedType;
Expand All @@ -30,8 +29,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

$argType = $scope->getType($functionCall->getArgs()[0]->value);
$isTrueType = (new ConstantBooleanType(true))->isSuperTypeOf($argType);
$isFalseType = (new ConstantBooleanType(false))->isSuperTypeOf($argType);
$isTrueType = $argType->isTrue();
$isFalseType = $argType->isFalse();
$compareTypes = $isTrueType->compareTo($isFalseType);
if ($compareTypes === $isTrueType) {
return new FloatType();
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public function testVariableCertaintyInIsset(): void
116,
],
[
'Variable $variableInSecondCase in isset() always exists and is always null.',
'Variable $variableInSecondCase in isset() is never defined.',
117,
],
[
Expand Down