Skip to content
Closed
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
11 changes: 11 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3436,6 +3436,17 @@ public function removeTypeFromExpression(Expr $expr, Type $typeToRemove): self
);
}

/**
* Asks the scope for a node's narrowing, so callers do not need to hold
* a TypeSpecifier themselves.
*
* @internal
*/
public function specifyTypesOfNewWorldHandlerNode(Expr $node, TypeSpecifierContext $context): SpecifiedTypes
{
return $this->typeSpecifier->specifyTypesInCondition($this, $node, $context);
}

/**
* @api
*/
Expand Down
5 changes: 1 addition & 4 deletions src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
Expand Down Expand Up @@ -48,7 +47,6 @@ final class ImpossibleCheckTypeHelper

public function __construct(
private ReflectionProvider $reflectionProvider,
private TypeSpecifier $typeSpecifier,
#[AutowiredParameter]
private bool $treatPhpDocTypesAsCertain,
)
Expand Down Expand Up @@ -315,7 +313,7 @@ private function getSpecifiedType(
}

$typeSpecifierScope = $this->treatPhpDocTypesAsCertain ? $scope : $scope->doNotTreatPhpDocTypesAsCertain();
$specifiedTypes = $this->typeSpecifier->specifyTypesInCondition($typeSpecifierScope, $node, $this->determineContext($typeSpecifierScope, $node));
$specifiedTypes = $typeSpecifierScope->specifyTypesOfNewWorldHandlerNode($node, $this->determineContext($typeSpecifierScope, $node));

// don't validate types on overwrite
if ($specifiedTypes->shouldOverwrite()) {
Expand Down Expand Up @@ -505,7 +503,6 @@ public function doNotTreatPhpDocTypesAsCertain(): self

return new self(
$this->reflectionProvider,
$this->typeSpecifier,
false,
);
}
Expand Down
13 changes: 2 additions & 11 deletions src/Type/Php/TypeSpecifyingFunctionsDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
Expand All @@ -18,11 +16,9 @@
use function in_array;

#[AutowiredService]
final class TypeSpecifyingFunctionsDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension, TypeSpecifierAwareExtension
final class TypeSpecifyingFunctionsDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

private TypeSpecifier $typeSpecifier;

private ?ImpossibleCheckTypeHelper $helper = null;

public function __construct(
Expand All @@ -33,11 +29,6 @@ public function __construct(
{
}

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), [
Expand Down Expand Up @@ -71,7 +62,7 @@ public function getTypeFromFunctionCall(

private function getHelper(): ImpossibleCheckTypeHelper
{
return $this->helper ??= new ImpossibleCheckTypeHelper($this->reflectionProvider, $this->typeSpecifier, $this->treatPhpDocTypesAsCertain);
return $this->helper ??= new ImpossibleCheckTypeHelper($this->reflectionProvider, $this->treatPhpDocTypesAsCertain);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeFunctionCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -49,7 +48,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -62,7 +60,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeStaticMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeFunctionCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -48,7 +47,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -61,7 +59,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeStaticMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeFunctionCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -49,7 +48,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -62,7 +60,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeStaticMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeFunctionCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->shouldTreatPhpDocTypesAsCertain(),
),
new PossiblyImpureTipHelper(true),
Expand All @@ -43,7 +42,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->shouldTreatPhpDocTypesAsCertain(),
),
new PossiblyImpureTipHelper(true),
Expand All @@ -56,7 +54,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeStaticMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->shouldTreatPhpDocTypesAsCertain(),
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeFunctionCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -49,7 +48,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -62,7 +60,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeStaticMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeFunctionCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -45,7 +44,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -58,7 +56,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeStaticMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeFunctionCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public function getRule(): Rule
return new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
true,
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public function getRule(): Rule
return new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
true,
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function getRule(): Rule
new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function getRule(): Rule
new ImpossibleCheckTypeStaticMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function getRule(): TRule
new ImpossibleCheckTypeFunctionCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->shouldTreatPhpDocTypesAsCertain(),
),
new PossiblyImpureTipHelper(true),
Expand All @@ -44,7 +43,6 @@ protected function getRule(): TRule
new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->shouldTreatPhpDocTypesAsCertain(),
),
new PossiblyImpureTipHelper(true),
Expand All @@ -57,7 +55,6 @@ protected function getRule(): TRule
new ImpossibleCheckTypeStaticMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->shouldTreatPhpDocTypesAsCertain(),
),
new PossiblyImpureTipHelper(true),
Expand Down
3 changes: 0 additions & 3 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeFunctionCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -44,7 +43,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -57,7 +55,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeStaticMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeFunctionCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -45,7 +44,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand All @@ -58,7 +56,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeStaticMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->treatPhpDocTypesAsCertain,
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeFunctionCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->shouldTreatPhpDocTypesAsCertain(),
),
new PossiblyImpureTipHelper(true),
Expand All @@ -43,7 +42,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->shouldTreatPhpDocTypesAsCertain(),
),
new PossiblyImpureTipHelper(true),
Expand All @@ -56,7 +54,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeStaticMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->shouldTreatPhpDocTypesAsCertain(),
),
new PossiblyImpureTipHelper(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeFunctionCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->shouldTreatPhpDocTypesAsCertain(),
),
new PossiblyImpureTipHelper(true),
Expand All @@ -43,7 +42,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->shouldTreatPhpDocTypesAsCertain(),
),
new PossiblyImpureTipHelper(true),
Expand All @@ -56,7 +54,6 @@ protected function getRule(): Rule
new ImpossibleCheckTypeStaticMethodCallRule(
new ImpossibleCheckTypeHelper(
self::createReflectionProvider(),
$this->getTypeSpecifier(),
$this->shouldTreatPhpDocTypesAsCertain(),
),
new PossiblyImpureTipHelper(true),
Expand Down
Loading