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

No longer verify types, when TypeSpecifyingExtension uses $overwrite=true #1011

Merged
merged 5 commits into from Feb 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Expand Up @@ -158,6 +158,12 @@ public function findSpecifiedType(
}

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

// don't validate types on overwrite
if ($specifiedTypes->shouldOverwrite()) {
return null;
}

$sureTypes = $specifiedTypes->getSureTypes();
$sureNotTypes = $specifiedTypes->getSureNotTypes();

Expand Down
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Comparison;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<ImpossibleCheckTypeMethodCallRule>
*/
class ImpossibleCheckTypeGenericOverwriteRuleTest extends RuleTestCase
{

public function getRule(): Rule
{
return new ImpossibleCheckTypeMethodCallRule(
new ImpossibleCheckTypeHelper(
$this->createReflectionProvider(),
$this->getTypeSpecifier(),
[],
true,
),
true,
true,
);
}

public function testNoReportedErrorOnOverwrite(): void
{
$this->analyse([__DIR__ . '/data/generic-type-override.php'], []);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/impossible-check-type-generic-overwrite.neon',
];
}

}
@@ -0,0 +1,58 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Comparison;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\MethodTypeSpecifyingExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Generic\GenericObjectType;

class GenericTypeOverride implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
{

/** @var TypeSpecifier */
private $typeSpecifier;

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

public function getClass(): string
{
return \GenericTypeOverride\Foo::class;
}

public function isMethodSupported(
MethodReflection $methodReflection,
MethodCall $node,
TypeSpecifierContext $context
): bool
{
return $methodReflection->getName() === 'setFetchMode';
}

public function specifyTypes(
MethodReflection $methodReflection,
MethodCall $node,
Scope $scope,
TypeSpecifierContext $context
): SpecifiedTypes
{
$newType = new GenericObjectType(\GenericTypeOverride\Foo::class, [new ObjectType(\GenericTypeOverride\Bar::class)]);

return $this->typeSpecifier->create(
$node->var,
$newType,
TypeSpecifierContext::createTruthy(),
true
);
}

}
39 changes: 39 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/generic-type-override.php
@@ -0,0 +1,39 @@
<?php

namespace GenericTypeOverride;

use function PHPStan\Testing\assertType;

class Test {
public function doFoo() {
$foo = $this->createGenericFoo();
assertType('Foo<int>', $foo);

// $foo generic will be overridden via MethodTypeSpecifyingExtension
$foo->setFetchMode();
assertType('Foo<Bar>', $foo);
}

/**
* @return Foo<int>
*/
public function createGenericFoo() {

}
}


/**
* @template T
*/
class Foo
{
public function setFetchMode() {

}
}


class Bar
{
}
@@ -0,0 +1,5 @@
services:
-
class: PHPStan\Rules\Comparison\GenericTypeOverride
tags:
- phpstan.typeSpecifier.methodTypeSpecifyingExtension