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

PHP 8.1 - Intersection type issue #5870

Closed
agustingomes opened this issue Nov 1, 2021 · 8 comments
Closed

PHP 8.1 - Intersection type issue #5870

agustingomes opened this issue Nov 1, 2021 · 8 comments

Comments

@agustingomes
Copy link

agustingomes commented Nov 1, 2021

Bug report

Code snippet that reproduces the problem

<?php

declare(strict_types=1);

interface ReadRepository
{
}

interface WriteRepository
{
}

final class Repository implements  ReadRepository, WriteRepository
{
}

final class CommandHandler
{
    public function __construct(
        private ReadRepository & WriteRepository $repository
    ) {
    }
}

$commandHandler = new CommandHandler(new Repository());

Reproducible in phpstan.org: https://phpstan.org/r/992affc0-b12c-4f63-84ea-eabb87a8f42b

Expected output

The expectation would be that this expression would be valid as intersection type, but from the stack trace, it seems it's passing it into a place expecting an union type instead.

Did PHPStan help you today? Did it make you happy in any way?

I'm very happy with the release of the 1.0 version, and I'm testing it in PHP 8.1 :)

Configuration:

includes:
  - vendor/phpstan/phpstan/conf/bleedingEdge.neon
parameters:
  level: max
  checkExplicitMixed: true
  paths:
    - agustin/bin/includes/8.1-attributes-poc.php

Stack trace

Note: Using configuration file /opt/project/phpstan.neon.dist.
/opt/project/agustin/bin/includes/8.1-attributes-poc.php
Uncaught TypeError: PHPStan\BetterReflection\Reflection\ReflectionUnionType::__construct(): Argument #1 ($type) must be of type PhpParser\Node\UnionType, PhpParser\Node\IntersectionType given, called in phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflection/ReflectionType.php on line 31 in phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflection/ReflectionUnionType.php:13
#0 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflection/ReflectionType.php(31): PHPStan\BetterReflection\Reflection\ReflectionUnionType->__construct(Object(PhpParser\Node\IntersectionType), false)
#1 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflection/ReflectionProperty.php(313): PHPStan\BetterReflection\Reflection\ReflectionType::createFromTypeAndReflector(Object(PhpParser\Node\IntersectionType))
#2 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/Reflection/Adapter/ReflectionProperty.php(95): PHPStan\BetterReflection\Reflection\ReflectionProperty->getType()
#3 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/Php/PhpClassReflectionExtension.php(231): PHPStan\BetterReflection\Reflection\Adapter\ReflectionProperty->getType()
#4 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/Php/PhpClassReflectionExtension.php(133): PHPStan\Reflection\Php\PhpClassReflectionExtension->createProperty(Object(PHPStan\Reflection\ClassReflection), 'repository', false)
#5 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/ClassReflection.php(398): PHPStan\Reflection\Php\PhpClassReflectionExtension->getNativeProperty(Object(PHPStan\Reflection\ClassReflection), 'repository')
#6 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Rules/Properties/ExistingClassesInPropertiesRule.php(42): PHPStan\Reflection\ClassReflection->getNativeProperty('repository')
#7 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(69): PHPStan\Rules\Properties\ExistingClassesInPropertiesRule->processNode(Object(PHPStan\Node\ClassPropertyNode), Object(PHPStan\Analyser\MutatingScope))
#8 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Node/ClassStatementsGatherer.php(91): PHPStan\Analyser\FileAnalyser->PHPStan\Analyser\{closure}(Object(PHPStan\Node\ClassPropertyNode), Object(PHPStan\Analyser\MutatingScope))
#9 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(389): PHPStan\Node\ClassStatementsGatherer->__invoke(Object(PHPStan\Node\ClassPropertyNode), Object(PHPStan\Analyser\MutatingScope))
#10 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(249): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\ClassMethod), Object(PHPStan\Analyser\MutatingScope), Object(PHPStan\Node\ClassStatementsGatherer))
#11 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(498): PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\Class_), Array, Object(PHPStan\Analyser\MutatingScope), Object(PHPStan\Node\ClassStatementsGatherer))
#12 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(217): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Class_), Object(PHPStan\Analyser\MutatingScope), Object(Closure))
#13 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(149): PHPStan\Analyser\NodeScopeResolver->processNodes(Array, Object(PHPStan\Analyser\MutatingScope), Object(Closure))
#14 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/Analyser.php(52): PHPStan\Analyser\FileAnalyser->analyseFile('/opt/project/ag...', Array, Object(PHPStan\Rules\Registry), NULL)
#15 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Command/AnalyserRunner.php(56): PHPStan\Analyser\Analyser->analyse(Array, Object(Closure), NULL, true, Array)
#16 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Command/AnalyseApplication.php(146): PHPStan\Command\AnalyserRunner->runAnalyser(Array, Array, Object(Closure), NULL, true, true, '/opt/project/ph...', NULL, NULL, Object(_PHPStan_3738fcd98\Symfony\Component\Console\Input\ArgvInput))
#17 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Command/AnalyseApplication.php(78): PHPStan\Command\AnalyseApplication->runAnalyser(Array, Array, true, '/opt/project/ph...', Object(PHPStan\Command\Symfony\SymfonyOutput), Object(PHPStan\Command\Symfony\SymfonyOutput), Object(_PHPStan_3738fcd98\Symfony\Component\Console\Input\ArgvInput))
#18 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/src/Command/AnalyseCommand.php(144): PHPStan\Command\AnalyseApplication->analyse(Array, true, Object(PHPStan\Command\Symfony\SymfonyOutput), Object(PHPStan\Command\Symfony\SymfonyOutput), false, true, '/opt/project/ph...', Array, Object(_PHPStan_3738fcd98\Symfony\Component\Console\Input\ArgvInput))
#19 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Command/Command.php(228): PHPStan\Command\AnalyseCommand->execute(Object(_PHPStan_3738fcd98\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_3738fcd98\Symfony\Component\Console\Output\ConsoleOutput))
#20 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(856): _PHPStan_3738fcd98\Symfony\Component\Console\Command\Command->run(Object(_PHPStan_3738fcd98\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_3738fcd98\Symfony\Component\Console\Output\ConsoleOutput))
#21 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(237): _PHPStan_3738fcd98\Symfony\Component\Console\Application->doRunCommand(Object(PHPStan\Command\AnalyseCommand), Object(_PHPStan_3738fcd98\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_3738fcd98\Symfony\Component\Console\Output\ConsoleOutput))
#22 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(138): _PHPStan_3738fcd98\Symfony\Component\Console\Application->doRun(Object(_PHPStan_3738fcd98\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_3738fcd98\Symfony\Component\Console\Output\ConsoleOutput))
#23 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(82): _PHPStan_3738fcd98\Symfony\Component\Console\Application->run()
#24 phar:///opt/project/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(83): _PHPStan_3738fcd98\{closure}()
#25 /opt/project/vendor/phpstan/phpstan/phpstan(8): require('phar:///opt/pro...')
#26 {main}
@mergeable
Copy link

mergeable bot commented Nov 1, 2021

This bug report is missing a link to reproduction on phpstan.org.

It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

Hi, PHPStan 1.0 runs on PHP 8.1 runtime in the sense there are no deprecations, all tests pass, and the current feature set works as expected. But it doesn't yet support all new PHP 8.1 features. Full PHP 8.1 is coming later this year. (It's a lot of work.)

@agustingomes
Copy link
Author

Hi @ondrejmirtes

Maybe I can help with the PR to add support for this. I'll take a look at the phpstan-src and see if I can get started during the next days

@ondrejmirtes
Copy link
Member

Hi, it's a massive amount of work I think I have to do it myself. First step is to base this https://github.com/ondrejmirtes/BetterReflection fork off of upcoming Roave/BetterReflection 5.0.0 and check that everything works as expected.

@agustingomes
Copy link
Author

I see.

Indeed, I may not have the right knowledge to orchestrate that.

@ondrejmirtes
Copy link
Member

Implemented: phpstan/phpstan-src@be05557

@agustingomes
Copy link
Author

Awesome! Thank you @ondrejmirtes :)

@github-actions
Copy link

github-actions bot commented Dec 8, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants