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

class not found on class-constants #7365

Closed
staabm opened this issue May 31, 2022 · 19 comments
Closed

class not found on class-constants #7365

staabm opened this issue May 31, 2022 · 19 comments

Comments

@staabm
Copy link
Contributor

staabm commented May 31, 2022

Bug report

Stacktrace:

-------------------------------------------------------------------------------------------------------------------
     Error
 -- -------------------------------------------------------------------------------------------------------------------
     Internal error: Internal error: Class "Fare_Warengruppen" not found in file
     C:\dvl\Workspace\fare\application\fare.deRocket\controllers\DbhController.php

     Post the following stack trace to https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md:
     #0
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-re
     flection/src/SourceLocator/SourceStubber/ReflectionSourceStubber.php(294):
     ReflectionClass->getDefaultProperties()
     #1
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-re
     flection/src/SourceLocator/SourceStubber/ReflectionSourceStubber.php(97):
     PHPStan\BetterReflection\SourceLocator\SourceStubber\ReflectionSourceStubber->addProperties()
     #2
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/BetterReflecti
     on/SourceLocator/ReflectionClassSourceLocator.php(37):
     PHPStan\BetterReflection\SourceLocator\SourceStubber\ReflectionSourceStubber->generateClassStub()
     #3
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/BetterReflecti
     on/SourceLocator/AutoloadFunctionsSourceLocator.php(37):
     PHPStan\Reflection\BetterReflection\SourceLocator\ReflectionClassSourceLocator->locateIdentifier()
     #4
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-re
     flection/src/SourceLocator/Type/AggregateSourceLocator.php(28):
     PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadFunctionsSourceLocator->locateIdentifier()
     #5
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-re
     flection/src/SourceLocator/Type/MemoizingSourceLocator.php(33):
     PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator->locateIdentifier()
     #6
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/ondrejmirtes/better-re
     flection/src/Reflector/DefaultReflector.php(32):
     PHPStan\BetterReflection\SourceLocator\Type\MemoizingSourceLocator->locateIdentifier()
     #7
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/BetterReflecti
     on/Reflector/MemoizingReflector.php(45): PHPStan\BetterReflection\Reflector\DefaultReflector->reflectClass()
     #8
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/BetterReflecti
     on/BetterReflectionProvider.php(166):
     PHPStan\Reflection\BetterReflection\Reflector\MemoizingReflector->reflectClass()
     #9
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/src/Reflection/ReflectionProv
     ider/MemoizingReflectionProvider.php(43):
     PHPStan\Reflection\BetterReflection\BetterReflectionProvider->getClass()
     #10
     C:\dvl\Workspace\fare\vendor-bin\phpstan\vendor\phpstan\phpstan-deprecation-rules\src\Rules\Deprecations\Implemen
     tationOfDeprecatedInterfaceRule.php(42):
     PHPStan\Reflection\ReflectionProvider\MemoizingReflectionProvider->getClass()
     #11
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php
     (102): PHPStan\Rules\Deprecations\ImplementationOfDeprecatedInterfaceRule->processNode()
     #12
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolve
     r.php(380): PHPStan\Analyser\FileAnalyser->PHPStan\Analyser\{closure}()
     #13
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolve
     r.php(296): PHPStan\Analyser\NodeScopeResolver->processStmtNode()
     #14
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php
     (189): PHPStan\Analyser\NodeScopeResolver->processNodes()
     #15
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php
     (143): PHPStan\Analyser\FileAnalyser->analyseFile()
     #16
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/sr
     c/Evenement/EventEmitterTrait.php(97): PHPStan\Command\WorkerCommand->PHPStan\Command\{closure}()
     #17
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/clue/ndjson-react/src/
     Decoder.php(110): _PHPStan_9df114220\Evenement\EventEmitter->emit()
     #18
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/sr
     c/Evenement/EventEmitterTrait.php(97): _PHPStan_9df114220\Clue\React\NDJson\Decoder->handleData()
     #19
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/Util.
     php(62): _PHPStan_9df114220\Evenement\EventEmitter->emit()
     #20
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/sr
     c/Evenement/EventEmitterTrait.php(97):
     _PHPStan_9df114220\React\Stream\Util::_PHPStan_9df114220\React\Stream\{closure}()
     #21
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/Duple
     xResourceStream.php(154): _PHPStan_9df114220\Evenement\EventEmitter->emit()
     #22
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/S
     treamSelectLoop.php(201): _PHPStan_9df114220\React\Stream\DuplexResourceStream->handleData()
     #23
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/S
     treamSelectLoop.php(173): _PHPStan_9df114220\React\EventLoop\StreamSelectLoop->waitForStreamActivity()
     #24
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php
     (106): _PHPStan_9df114220\React\EventLoop\StreamSelectLoop->run()
     #25
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Comman
     d/Command.php(259): PHPStan\Command\WorkerCommand->execute()
     #26
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Applic
     ation.php(856): _PHPStan_9df114220\Symfony\Component\Console\Command\Command->run()
     #27
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Applic
     ation.php(259): _PHPStan_9df114220\Symfony\Component\Console\Application->doRunCommand()
     #28
     phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Applic
     ation.php(157): _PHPStan_9df114220\Symfony\Component\Console\Application->doRun()
     #29 phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(120):
     _PHPStan_9df114220\Symfony\Component\Console\Application->run()
     #30 phar://C:/dvl/Workspace/fare/vendor-bin/phpstan/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(121):
     _PHPStan_9df114220\{closure}()
     #31 C:\dvl\Workspace\fare\vendor-bin\phpstan\vendor\phpstan\phpstan\phpstan(8): require('...')
     #32 C:\dvl\Workspace\fare\vendor\bin\phpstan(117): include('...')
     #33 {main}
     Child process error (exit code 1):

code in question looks like

<?php


class DbhController 
{
    protected $warengruppenid = [
        Fare_Warengruppen::WARENGRUPPE_STOCKSCHIRME,
        Fare_Warengruppen::WARENGRUPPE_TASCHENSCHIRME,
    ];
}

Code snippet that reproduces the problem

I am in the process of creating a repro repo

Expected output

no error

@mergeable
Copy link

mergeable bot commented May 31, 2022

This bug report is missing a link to reproduction at phpstan.org/try.

It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

I think this is your mistake. If you use your own autoloader to load a class, you need to make sure by yourself the definition isn't broken.

@staabm
Copy link
Contributor Author

staabm commented May 31, 2022

I am not sure, because the class can only not be found when the class-constant is used within a property.

it works in other classes of the same codebase when used within method bodies

@ondrejmirtes
Copy link
Member

Try to create a simple PHP script, require just your own autoloader, and ask for ReflectionClass->getDefaultProperties() of that class. It's gonna crash the same way :)

@ondrejmirtes
Copy link
Member

But feel free to create the repo, I might work around it.

@staabm
Copy link
Contributor Author

staabm commented May 31, 2022

when I change

<?php


class DbhController 
{
    protected $warengruppenid = [
        Fare_Warengruppen::WARENGRUPPE_STOCKSCHIRME,
        Fare_Warengruppen::WARENGRUPPE_TASCHENSCHIRME,
    ];
}

to

<?php

class DbhController 
{
    private function isButtonViewAllowed(): bool
    {
        // nur auf Fare.de ist der Link grundsätzlich erlaubt
        if (!Fare_Portal_ShopLayout::isFare()) {
            return false;
        }

        if (!$this->isLoggedIn) {
            return false;
        }

        $artMapper = new Application_Model_Mapper_Article();

        // Warengruppen, die grundsätzlich für den VE-Button freigeschaltet sind.
        return null !== $artMapper->fetchByWarengruppenIdVeFaehig($this->article->getArtid(), [
                Fare_Warengruppen::WARENGRUPPE_STOCKSCHIRME,
                Fare_Warengruppen::WARENGRUPPE_TASCHENSCHIRME,
                Fare_Warengruppen::WARENGRUPPE_SONNENSCHIRME,
            ]);
    }

it no longer errors... I need to investigate the details

@ondrejmirtes
Copy link
Member

That's not surprising if you actually look at what the stack trace tries to do.

@staabm
Copy link
Contributor Author

staabm commented May 31, 2022

I feel its suprising, as the 2nd example seems to successfully autoload the Fare_Warengruppen while the first does not?

at least I would assume that code like

        return null !== $artMapper->fetchByWarengruppenIdVeFaehig($this->article->getArtid(), [
                Fare_Warengruppen::WARENGRUPPE_STOCKSCHIRME,
                Fare_Warengruppen::WARENGRUPPE_TASCHENSCHIRME,
                Fare_Warengruppen::WARENGRUPPE_SONNENSCHIRME,
            ]);

also triggers the autoloader?

@ondrejmirtes
Copy link
Member

No, it doesn't.

@staabm
Copy link
Contributor Author

staabm commented May 31, 2022

hmm interessting.. as I tried reproducing it in https://github.com/staabm/phpstan-bug7365 (and intentionally do not provide a proper autoloader) I am getting proper errors instead of a internal fatal error:

vendor/bin/phpstan analyze  -c app/phpstan.neon -vvv app/src/MyApp.php
Result cache not used because only files were passed as analysed paths.
  5      Access to constant WARENGRUPPE_STOCKSCHIRME on an unknown class ExternalClass.
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols
  6      Access to constant WARENGRUPPE_TASCHENSCHIRME on an unknown class ExternalClass.
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols
 ------ ----------------------------------------------------------------------------------

@ondrejmirtes
Copy link
Member

Again, everything as expected :) My message still stands:

If you use your own autoloader to load a class, you need to make sure by yourself the definition isn't broken.

@ondrejmirtes
Copy link
Member

When your autoloader gets executed, it needs to be able to load Fare_Warengruppen as well. (Or hve a different autoloader at that point registered so that it can load that class.)

@ondrejmirtes
Copy link
Member

But I have an idea, I just need your reproduction now :)

@ondrejmirtes
Copy link
Member

It's gonna be solved by phpstan/phpstan-src#1373

@staabm
Copy link
Contributor Author

staabm commented May 31, 2022

I don't know what is different between my sample repo at https://github.com/staabm/phpstan-bug7365 and my local real world project. I have no idea what needs to be changed in the repro to make the internal error be thrown

If you use your own autoloader to load a class, you need to make sure by yourself the definition isn't broken.

I am not sure what you mean by that. AFAICT my autoloader is not throwing this exception

@ondrejmirtes
Copy link
Member

This is the situation in your autoloader that makes it crash: afbe366 The ClassWithConstants is unknown to your app at that point.

@ondrejmirtes
Copy link
Member

Fixed by: phpstan/phpstan-src#1373

@staabm
Copy link
Contributor Author

staabm commented May 31, 2022

Thank you!

@github-actions
Copy link

github-actions bot commented Jul 2, 2022

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 Jul 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants