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

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Feb 10, 2022

closes phpstan/phpstan#6620

I am not 100% happy with the test-case.
the test fails without the fix and passes with it - as expected.

but when running the test without the fix I get this error:

1) PHPStan\Rules\Comparison\ImpossibleCheckTypeGenericOverwriteRuleTest::testNoReportedErrorOnOverwrite
TypeError: PHPStan\Type\Generic\GenericObjectType::PHPStan\Type\Generic\{closure}(): Argument #1 ($type) must be of type PHPStan\Type\Type, string given

C:\dvl\Workspace\phpstan-src-staabm\src\Type\Generic\GenericObjectType.php:50
C:\dvl\Workspace\phpstan-src-staabm\src\Type\ObjectType.php:433
C:\dvl\Workspace\phpstan-src-staabm\src\Type\ObjectType.php:241
C:\dvl\Workspace\phpstan-src-staabm\src\Type\Generic\GenericObjectType.php:119
C:\dvl\Workspace\phpstan-src-staabm\src\Type\Generic\GenericObjectType.php:110
C:\dvl\Workspace\phpstan-src-staabm\src\Rules\Comparison\ImpossibleCheckTypeHelper.php:201
C:\dvl\Workspace\phpstan-src-staabm\src\Rules\Comparison\ImpossibleCheckTypeMethodCallRule.php:39
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\FileAnalyser.php:92
C:\dvl\Workspace\phpstan-src-staabm\src\Node\ClassStatementsGatherer.php:108
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\NodeScopeResolver.php:503
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\NodeScopeResolver.php:2840
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\NodeScopeResolver.php:1689
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\NodeScopeResolver.php:568
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\NodeScopeResolver.php:259
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\NodeScopeResolver.php:519
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\NodeScopeResolver.php:259
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\NodeScopeResolver.php:624
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\NodeScopeResolver.php:259
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\NodeScopeResolver.php:588
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\NodeScopeResolver.php:219
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\FileAnalyser.php:208
C:\dvl\Workspace\phpstan-src-staabm\src\Analyser\Analyser.php:61
C:\dvl\Workspace\phpstan-src-staabm\src\Testing\RuleTestCase.php:100
C:\dvl\Workspace\phpstan-src-staabm\tests\PHPStan\Rules\Comparison\ImpossibleCheckTypeGenericOverwriteRuleTest.php:37

it seems there is something missing which makes the test end properly (without the fix), to render a proper repro-case.

the test errors here

$isSuperType = $resultType->isSuperTypeOf($argumentType);

because it seems it cannot find some part of the involved objects within $scope - I guess.

TypeSpecifierContext $context
): SpecifiedTypes
{
$newType = new GenericObjectType(\GenericTypeOverride\Foo::class, [\GenericTypeOverride\Bar::class]);
Copy link
Member

Choose a reason for hiding this comment

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

This isn't how the constructor should be called. The constructor looks like this:

	/**
	 * @api
	 * @param array<int, Type> $types
	 */
	public function __construct(
		string $mainType,
		private array $types,
		?Type $subtractedType = null,
		private ?ClassReflection $classReflection = null,
	)

You need new ObjectType(\GenericTypeOverride\Bar::class) instead.

Copy link
Contributor Author

@staabm staabm Feb 11, 2022

Choose a reason for hiding this comment

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

thx for catching. with this fix, the unit test works like expected:

$ vendor/bin/phpunit tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeGenericOverwriteRuleTest.php
Xdebug: [Step Debug] Time-out connecting to debugging client, waited: 200 ms. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(
PHPUnit 9.5.7 by Sebastian Bergmann and contributors.

Warning:       XDEBUG_MODE=coverage or xdebug.mode=coverage has to be set

F                                                                   1 / 1 (100%)

Time: 00:00.424, Memory: 52.00 MB

There was 1 failure:

1) PHPStan\Rules\Comparison\ImpossibleCheckTypeGenericOverwriteRuleTest::testNoReportedErrorOnOverwrite
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'
+'13: Call to method GenericTypeOverride\Foo<int>::setFetchMode() will always evaluate to false.
 '

C:\dvl\Workspace\phpstan-src-staabm\src\Testing\RuleTestCase.php:140
C:\dvl\Workspace\phpstan-src-staabm\tests\PHPStan\Rules\Comparison\ImpossibleCheckTypeGenericOverwriteRuleTest.php:34

I am wondering whether phpstan should have errored about this when scanning the test-case?
as can be seen in the github actions, it did not catch this problem

@staabm staabm marked this pull request as ready for review February 11, 2022 08:50
@herndlm
Copy link
Contributor

herndlm commented Feb 11, 2022

I wonder if this is related or will fix cases over on webmozart-assert too. Need to check later :)


public function testNoReportedErrorOnOverwrite(): void
{
$this->treatPhpDocTypesAsCertain = false;
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't need to be a property if you're not testing true/false scenarios. So please put "true" directly in the constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@ondrejmirtes ondrejmirtes merged commit 425dee7 into phpstan:master Feb 11, 2022
@ondrejmirtes
Copy link
Member

Thank you.

@staabm staabm deleted the overwrite branch February 11, 2022 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants