Skip to content

Improve impossible type checker for void-returning functions #1857

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
Oct 17, 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
1 change: 1 addition & 0 deletions conf/bleedingEdge.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ parameters:
curlSetOptTypes: true
listType: true
missingMagicSerializationRule: true
nullContextForVoidReturningFunctions: true
4 changes: 4 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ parameters:
curlSetOptTypes: false
listType: false
missingMagicSerializationRule: false
nullContextForVoidReturningFunctions: false
fileExtensions:
- php
checkAdvancedIsset: false
Expand Down Expand Up @@ -261,6 +262,7 @@ parametersSchema:
curlSetOptTypes: bool()
listType: bool()
missingMagicSerializationRule: bool()
nullContextForVoidReturningFunctions: bool()
])
fileExtensions: listOf(string())
checkAdvancedIsset: bool()
Expand Down Expand Up @@ -909,6 +911,7 @@ services:
arguments:
universalObjectCratesClasses: %universalObjectCratesClasses%
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
nullContextForVoidReturningFunctions: %featureToggles.nullContextForVoidReturningFunctions%

-
class: PHPStan\Rules\Exceptions\DefaultExceptionTypeResolver
Expand Down Expand Up @@ -1711,6 +1714,7 @@ services:
arguments:
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
universalObjectCratesClasses: %universalObjectCratesClasses%
nullContextForVoidReturningFunctions: %featureToggles.nullContextForVoidReturningFunctions%
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

Expand Down
49 changes: 48 additions & 1 deletion src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantArrayType;
Expand All @@ -22,6 +24,7 @@
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
use function array_map;
use function array_pop;
use function count;
Expand All @@ -42,6 +45,7 @@ public function __construct(
private TypeSpecifier $typeSpecifier,
private array $universalObjectCratesClasses,
private bool $treatPhpDocTypesAsCertain,
private bool $nullContextForVoidReturningFunctions,
)
{
}
Expand Down Expand Up @@ -178,7 +182,7 @@ public function findSpecifiedType(
}
}

$specifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $node, TypeSpecifierContext::createTruthy());
$specifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $node, $this->determineContext($scope, $node));

// don't validate types on overwrite
if ($specifiedTypes->shouldOverwrite()) {
Expand Down Expand Up @@ -311,7 +315,50 @@ public function doNotTreatPhpDocTypesAsCertain(): self
$this->typeSpecifier,
$this->universalObjectCratesClasses,
false,
$this->nullContextForVoidReturningFunctions,
);
}

private function determineContext(Scope $scope, Expr $node): TypeSpecifierContext
{
if (!$this->nullContextForVoidReturningFunctions) {
return TypeSpecifierContext::createTruthy();
}

if ($node instanceof FuncCall && $node->name instanceof Node\Name) {
if ($this->reflectionProvider->hasFunction($node->name, $scope)) {
$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $node->getArgs(), $functionReflection->getVariants());
$returnType = TypeUtils::resolveLateResolvableTypes($parametersAcceptor->getReturnType());

return $returnType instanceof VoidType ? TypeSpecifierContext::createNull() : TypeSpecifierContext::createTruthy();
}
} elseif ($node instanceof MethodCall && $node->name instanceof Node\Identifier) {
$methodCalledOnType = $scope->getType($node->var);
$methodReflection = $scope->getMethodReflection($methodCalledOnType, $node->name->name);
if ($methodReflection !== null) {
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $node->getArgs(), $methodReflection->getVariants());
$returnType = TypeUtils::resolveLateResolvableTypes($parametersAcceptor->getReturnType());

return $returnType instanceof VoidType ? TypeSpecifierContext::createNull() : TypeSpecifierContext::createTruthy();
}
} elseif ($node instanceof StaticCall && $node->name instanceof Node\Identifier) {
if ($node->class instanceof Node\Name) {
$calleeType = $scope->resolveTypeByName($node->class);
} else {
$calleeType = $scope->getType($node->class);
}

$staticMethodReflection = $scope->getMethodReflection($calleeType, $node->name->name);
if ($staticMethodReflection !== null) {
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $node->getArgs(), $staticMethodReflection->getVariants());
$returnType = TypeUtils::resolveLateResolvableTypes($parametersAcceptor->getReturnType());

return $returnType instanceof VoidType ? TypeSpecifierContext::createNull() : TypeSpecifierContext::createTruthy();
}
}

return TypeSpecifierContext::createTruthy();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TypeSpecifyingFunctionsDynamicReturnTypeExtension implements DynamicFuncti
/**
* @param string[] $universalObjectCratesClasses
*/
public function __construct(private ReflectionProvider $reflectionProvider, private bool $treatPhpDocTypesAsCertain, private array $universalObjectCratesClasses)
public function __construct(private ReflectionProvider $reflectionProvider, private bool $treatPhpDocTypesAsCertain, private array $universalObjectCratesClasses, private bool $nullContextForVoidReturningFunctions)
{
}

Expand Down Expand Up @@ -83,7 +83,7 @@ public function getTypeFromFunctionCall(
private function getHelper(): ImpossibleCheckTypeHelper
{
if ($this->helper === null) {
$this->helper = new ImpossibleCheckTypeHelper($this->reflectionProvider, $this->typeSpecifier, $this->universalObjectCratesClasses, $this->treatPhpDocTypesAsCertain);
$this->helper = new ImpossibleCheckTypeHelper($this->reflectionProvider, $this->typeSpecifier, $this->universalObjectCratesClasses, $this->treatPhpDocTypesAsCertain, $this->nullContextForVoidReturningFunctions);
}

return $this->helper;
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/array_values.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array_keys.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-8174.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Comparison/data/bug-8169.php');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function getRule(): Rule
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
$this->treatPhpDocTypesAsCertain,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function getRule(): Rule
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
$this->treatPhpDocTypesAsCertain,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function getRule(): Rule
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
$this->treatPhpDocTypesAsCertain,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function getRule(): Rule
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
$this->treatPhpDocTypesAsCertain,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function getRule(): Rule
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
$this->treatPhpDocTypesAsCertain,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function getRule(): Rule
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
$this->treatPhpDocTypesAsCertain,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected function getRule(): Rule
$this->getTypeSpecifier(),
[stdClass::class],
$this->treatPhpDocTypesAsCertain,
true,
),
$this->checkAlwaysTrueCheckTypeFunctionCall,
$this->treatPhpDocTypesAsCertain,
Expand Down Expand Up @@ -581,6 +582,10 @@ public function testConditionalTypesInference(): void
'Call to function testIsNotInt() with int will always evaluate to false.',
72,
],
[
'Call to function assertIsInt() with int will always evaluate to true.',
78,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function getRule(): Rule
$this->getTypeSpecifier(),
[],
true,
true,
),
true,
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function getRule(): Rule
$this->getTypeSpecifier(),
[],
true,
true,
),
true,
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function getRule(): Rule
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
true,
$this->treatPhpDocTypesAsCertain,
Expand Down Expand Up @@ -186,6 +187,25 @@ public function testReportPhpDoc(): void
]);
}

public function testBug8169(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-8169.php'], [
[
'Call to method Bug8169\HelloWorld::assertString() with string will always evaluate to true.',
19,
],
[
'Call to method Bug8169\HelloWorld::assertString() with string will always evaluate to true.',
26,
],
[
'Call to method Bug8169\HelloWorld::assertString() with int will always evaluate to false.',
33,
],
]);
}

public static function getAdditionalConfigFiles(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function getRule(): Rule
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
true,
$this->treatPhpDocTypesAsCertain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function getRule(): Rule
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
$this->treatPhpDocTypesAsCertain,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function getRule(): Rule
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
$this->treatPhpDocTypesAsCertain,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function getRule(): Rule
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
$this->treatPhpDocTypesAsCertain,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function getRule(): Rule
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
$this->treatPhpDocTypesAsCertain,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function getRule(): Rule
$this->getTypeSpecifier(),
[],
$this->treatPhpDocTypesAsCertain,
true,
),
$this->treatPhpDocTypesAsCertain,
),
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-8169.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace Bug8169;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/** @phpstan-assert string $var */
public function assertString(mixed $var): void
{
}

public function test(mixed $foo): void
{
assertType('mixed', $foo);
$this->assertString($foo);
assertType('string', $foo);
$this->assertString($foo); // should report as always evaluating to true?
assertType('string', $foo);
}

public function test2(string $foo): void
{
assertType('string', $foo);
$this->assertString($foo); // should report as always evaluating to true?
assertType('string', $foo);
}

public function test3(int $foo): void
{
assertType('int', $foo);
$this->assertString($foo); // should report as always evaluating to false?
assertType('*NEVER*', $foo);
}
}