Skip to content

ClassScanner fails on non-standard files #920

Closed

Description

@przepompownia

I cannot create an issue for https://github.com/phpactor/class-to-file directly.

It is side efect of searching for another bug. I search what causes Could not find source with \class\name.

From phpactor project directory run this test on a file with class definitions inside if statement:

<?php

use Phpactor\ClassFileConverter\Domain\FilePath;
use Phpactor\ClassFileConverter\ClassToFileConverter;
use Phpactor\ClassFileConverter\Adapter\Simple\ClassScanner;

$autoloader = require(__DIR__ . '/vendor/autoload.php');
$converter = ClassToFileConverter::fromComposerAutoloader($autoloader);
$filename = __DIR__.'/vendor/friendsofphp/php-cs-fixer/tests/TestCase.php';
$candidates = $converter->fileToClassCandidates(FilePath::fromString($filename));
printf("%s\n\n", $candidates->getIterator()->current()->name());

echo (new ClassScanner)->getClassNameFromFile($filename);

Effect:

TestCase

PHP Notice:  Uninitialized string offset: 1 in /home/user/tmp/vim-test-envs/dist/coc+phpactor+user+pack/pack/bundle/start/phpactor/vendor/phpactor/class-to-file/lib/Adapter/Simple/ClassScanner.php on line 62
PHP Notice:  Uninitialized string offset: 1 in /home/user/tmp/vim-test-envs/dist/coc+phpactor+user+pack/pack/bundle/start/phpactor/vendor/phpactor/class-to-file/lib/Adapter/Simple/ClassScanner.php on line 62
use PHPUnitGoodPractices\Traits\ProphecyOverMockObjectTrait;
use PHPUnitGoodPractices\Traits\ProphesizeOnlyInterfaceTrait;

if (trait_exists(ProphesizeOnlyInterfaceTrait::class)) {

Two tokens after ProphesizeOnlyInterfaceTrait::class there is no class name.

I do not know whether it has cause any bug in RPC communication (displaying context menu). Is possible that notices from PHP can pollute the response? They are displayed in stderr.

The example in a README file of that project has several typos.

Activity

  1. przepompownia commented on Mar 15, 2020

    @przepompownia
    ContributorAuthor

    Error from vim:

    Error detected while processing function phpactor#ContextMenu[1]..phpactor#rpc:                                                                                               
    line   13:                                                                                                                                                                    
    E605: Exception not caught: Could not parse response from Phpactor: Vim(let):E474: Unidentified byte: PHP Notice:  Uninitialized string offset: 1 in /home/tmp/vim-test-
    envs/dist/min2/pack/bundle/start/phpactor/vendor/phpactor/class-to-file/lib/Adapter/Simple/ClassScanner.php on line 62^@PHP Notice:  Uninitialized string offset: 1 in /home/tmp/vim-test-envs/dist/min2/pack/bundle/start/phpactor/vendor/phpactor/class-to-file/lib/Adapter/Simple/ClassScanner.php on line 62^@{"version":"1.0.0","action":"input_callback","parameters":{"inputs":[{"name":"action","type":"choice","parameters":{"default":null,"label":"Method \"setUp\":","choices":{"cycle_visibility":"cycle_visibility","find_references":"find_references","generate_method":"generate_method","goto_definition":"goto_definition","replace_references":"replace_references","hover":"hover"},"keyMap":{"cycle_visibility":"v","find_references":"f","generate_method":"m","goto_definition":"g","replace_references":"r","hover":null}}}],"callback":{"action":"co
    

    when I tried to open the context menu on pack/bundle/start/phpactor/vendor/phpactor/worse-reflection/tests/Unit/Core/SourceCodeLocator/ChainSourceLocatorTest.php inside setUp().

  2. dantleech commented on Mar 15, 2020

    @dantleech
    Collaborator

    It's certainly possible that PHP pollutes the response:

    The vimscript is:

        let result = system(cmd, json_encode(request))

    So if we can find a solution for both vim+neovim there that would be good.

    But otherwise it would be better to convert all errors/warnings/notices to exceptions so that they are handled.

  3. przepompownia commented on Mar 15, 2020

    @przepompownia
    ContributorAuthor

    Conversion to exceptions seems to be good idea.

    I did not inspect this vim code yet but at the first look it seems that we can use jobstart (job_start) which can handle stdin and stderr separately instead of obsolete system.

  4. dantleech commented on Mar 16, 2020

    @dantleech
    Collaborator

    Yes I started a similar topic to add Async support to the VIM plugin (have since decided it's better to get this working on the LS at least initially):

    https://github.com/phpactor/phpactor/pull/890/files#diff-a977b9cdd05b02c5ec29fbc68a7b6adfR17

    The annoying thing is the API may be different in VIM

  5. danrot commented on Oct 6, 2022

    @danrot

    I think I am running into the same issue with Pimcore, but not absolutely sure what causes it... I get either this error:

    Phpactor returned an error: PHP Fatal error:  Uncaught TypeError: Phpactor\ClassFileConverter\Domain\ClassName::fromString(): Argument #1 ($string) must be of type string, null given, called in /Users/danielrotter/.local/share/nvim/plugged/p
    hpactor/vendor/phpactor/class-to-file/lib/Adapter/Simple/SimpleClassToFile.php on line 46 and defined in /Users/danielrotter/.local/share/nvim/plugged/phpactor/vendor/phpactor/class-to-file/lib/Domain/ClassName.php:22
    

    Or the same as above, just with a different line (guess it changed since the creation of that issue):

    Phpactor returned an error: PHP Warning:  Uninitialized string offset 1 in /Users/danielrotter/.local/share/nvim/plugged/phpactor/vendor/phpactor/class-to-file/lib/Adapter/Simple/ClassScanner.php on line 59
    

    It's certainly possible that PHP pollutes the response:

    @dantleech What exactly do you mean by that? Are you talking about PHP Warnings etc.?

  6. dantleech commented on Oct 6, 2022

    @dantleech
    Collaborator

    yes, that error was more about VIM parsing the RPC JSON response but getting a warning instead.

    The error you hav looks much more like an issue with f.e. the parser returning NULL where it is not expected. Can you provide a stack trace?

  7. dantleech commented on Oct 6, 2022

    @dantleech
    Collaborator

    but almost certainly 2 different sources of an error, possibly due to an incomplete class/trait/enum/interface definiiton?

  8. danrot commented on Oct 6, 2022

    @danrot

    Of course I can!

    Phpactor returned an error: PHP Fatal error:  Uncaught TypeError: Phpactor\ClassFileConverter\Domain\ClassName::fromString(): Argument #1 ($string) must be of type string, null given, called in /Users/danielrotter/.local/share/nvim/plugged/p
    hpactor/vendor/phpactor/class-to-file/lib/Adapter/Simple/SimpleClassToFile.php on line 46 and defined in /Users/danielrotter/.local/share/nvim/plugged/phpactor/vendor/phpactor/class-to-file/lib/Domain/ClassName.php:22                        
    Stack trace:                                                                                                                                                                                                                                     
    #0 /Users/danielrotter/.local/share/nvim/plugged/phpactor/vendor/phpactor/class-to-file/lib/Adapter/Simple/SimpleClassToFile.php(46): Phpactor\ClassFileConverter\Domain\ClassName::fromString(NULL)                                             
    #1 /Users/danielrotter/.local/share/nvim/plugged/phpactor/vendor/phpactor/class-to-file/lib/Domain/ChainClassToFile.php(23): Phpactor\ClassFileConverter\Adapter\Simple\SimpleClassToFile->classToFileCandidates(Object(Phpactor\ClassFileConvert
    er\Domain\ClassName))                                                                                                                                                                                                                            
    #2 /Users/danielrotter/.local/share/nvim/plugged/phpactor/vendor/phpactor/class-to-file/lib/Domain/ClassToFileFileToClass.php(23): Phpactor\ClassFileConverter\Domain\ChainClassToFile->classToFileCandidates(Object(Phpactor\ClassFileConverter\
    Domain\ClassName))                                                                                                                                                                                                                               
    #3 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/WorseReflection/Bridge/Phpactor/ClassToFileSourceLocator.php(23): Phpactor\ClassFileConverter\Domain\ClassToFileFileToClass->classToFileCandidates(Object(Phpactor\ClassFileConvert
    er\Domain\ClassName))                                                                                                                                                                                                                            
    #4 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/WorseReflection/Core/SourceCodeLocator/ChainSourceLocator.php(39): Phpactor\WorseReflection\Bridge\Phpactor\ClassToFileSourceLocator->locate(Object(Phpactor\WorseReflection\Core\C
    lassName))                                                                                                                                                                                                                                       
    #5 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/WorseReflection/Core/SourceCodeLocator/ChainSourceLocator.php(39): Phpactor\WorseReflection\Core\SourceCodeLocator\ChainSourceLocator->locate(Object(Phpactor\WorseReflection\Core\
    ClassName))                                                                                                                                                                                                                                      
    #6 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/WorseReflection/Core/Reflector/CoreReflector.php(152): Phpactor\WorseReflection\Core\SourceCodeLocator\ChainSourceLocator->locate(Object(Phpactor\WorseReflection\Core\ClassName)) 
    #7 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/WorseReflection/Core/Reflector/ClassReflector/MemonizedReflector.php(86): Phpactor\WorseReflection\Core\Reflector\CoreReflector->reflectClassLike(Object(Phpactor\WorseReflection\C
    ore\ClassName), Array)                                                                                                                                                                                                                           
    #8 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/WorseReflection/Core/Reflector/ClassReflector/MemonizedReflector.php(128): Phpactor\WorseReflection\Core\Reflector\ClassReflector\MemonizedReflector->Phpactor\WorseReflection\Core
    \Reflector\ClassReflector\{closure}()                                                                                                                                                                                                            
    #9 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/WorseReflection/Core/Cache/TtlCache.php(50): Phpactor\WorseReflection\Core\Reflector\ClassReflector\MemonizedReflector->Phpactor\WorseReflection\Core\Reflector\ClassReflector\{clo
    sure}()                                                                                                                                                                                                                                          
    #10 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/WorseReflection/Core/Reflector/ClassReflector/MemonizedReflector.php(133): Phpactor\WorseReflection\Core\Cache\TtlCache->getOrSet('__class_like__C...', Object(Closure))          
    #11 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/WorseReflection/Core/Reflector/ClassReflector/MemonizedReflector.php(87): Phpactor\WorseReflection\Core\Reflector\ClassReflector\MemonizedReflector->getOrSet('__class_like__C...'
    , Object(Closure))                                                                                                                                                                                                                               
    #12 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/WorseReflection/Core/Reflector/CompositeReflector.php(67): Phpactor\WorseReflection\Core\Reflector\ClassReflector\MemonizedReflector->reflectClassLike('ContainerG3Xneu...', Array
    )                                                                                                                                                                                                                                                
    #13 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/Completion/Bridge/WorseReflection/SuggestionDocumentor/WorseSuggestionDocumentor.php(36): Phpactor\WorseReflection\Core\Reflector\CompositeReflector->reflectClassLike('ContainerG
    3Xneu...')                                                                                                                                                                                                                                       
    #14 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/Completion/Core/Suggestion.php(235): Phpactor\Completion\Bridge\WorseReflection\SuggestionDocumentor\WorseSuggestionDocumentor->Phpactor\Completion\Bridge\WorseReflection\Suggest
    ionDocumentor\{closure}()                                                                                                                                                                                                                        
    #15 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/Completion/Core/Suggestion.php(152): Phpactor\Completion\Core\Suggestion->documentation()                                                                                         
    #16 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/Extension/CompletionRpc/Handler/CompleteHandler.php(58): Phpactor\Completion\Core\Suggestion->toArray()                                                                           
    #17 [internal function]: Phpactor\Extension\CompletionRpc\Handler\CompleteHandler->Phpactor\Extension\CompletionRpc\Handler\{closure}(Object(Phpactor\Completion\Core\Suggestion))                                                               
    #18 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/Extension/CompletionRpc/Handler/CompleteHandler.php(59): array_map(Object(Closure), Array)                                                                                        
    #19 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/Extension/Rpc/RequestHandler/RequestHandler.php(30): Phpactor\Extension\CompletionRpc\Handler\CompleteHandler->handle(Array)                                                      
    #20 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/Extension/Rpc/RequestHandler/ExceptionCatchingHandler.php(23): Phpactor\Extension\Rpc\RequestHandler\RequestHandler->handle(Object(Phpactor\Extension\Rpc\Request))               
    #21 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/Extension/Rpc/RequestHandler/LoggingHandler.php(33): Phpactor\Extension\Rpc\RequestHandler\ExceptionCatchingHandler->handle(Object(Phpactor\Extension\Rpc\Request))               
    #22 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/Extension/Rpc/Command/RpcCommand.php(80): Phpactor\Extension\Rpc\RequestHandler\LoggingHandler->handle(Object(Phpactor\Extension\Rpc\Request))                                    
    #23 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/Extension/Rpc/Command/RpcCommand.php(60): Phpactor\Extension\Rpc\Command\RpcCommand->processRequest(Object(Phpactor\Extension\Rpc\Request))                                       
    #24 /Users/danielrotter/.local/share/nvim/plugged/phpactor/vendor/symfony/console/Command/Command.php(298): Phpactor\Extension\Rpc\Command\RpcCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Consol
    e\Output\ConsoleOutput))                                                                                                                                                                                                                         
    #25 /Users/danielrotter/.local/share/nvim/plugged/phpactor/vendor/symfony/console/Application.php(1024): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Outpu
    t\ConsoleOutput))                                                                                                                                                                                                                                
    #26 /Users/danielrotter/.local/share/nvim/plugged/phpactor/vendor/symfony/console/Application.php(299): Symfony\Component\Console\Application->doRunCommand(Object(Phpactor\Extension\Rpc\Command\RpcCommand), Object(Symfony\Component\Console\I
    nput\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))                                                                                                                                                                         
    #27 /Users/danielrotter/.local/share/nvim/plugged/phpactor/lib/Application.php(48): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))      
    #28 /Users/danielrotter/.local/share/nvim/plugged/phpactor/vendor/symfony/console/Application.php(171): Phpactor\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))   
    #29 /Users/danielrotter/.local/share/nvim/plugged/phpactor/bin/phpactor(45): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))               
    #30 {main}                                                                                                                                                                                                                                       
      thrown in /Users/danielrotter/.local/share/nvim/plugged/phpactor/vendor/phpactor/class-to-file/lib/Domain/ClassName.php on line 22
    
  9. danrot commented on Oct 6, 2022

    @danrot

    but almost certainly 2 different sources of an error, possibly due to an incomplete class/trait/enum/interface definiiton?

    Hard to tell 🙈 The error doesn't say anything and the codebase is quite big (and not mine).

  10. danrot commented on Oct 6, 2022

    @danrot

    Any ideas how I can find out which file causes this?

  11. dantleech commented on Oct 6, 2022

    @dantleech
    Collaborator

    You could work around/investigate by hacking the code, I can try and have a look tomorrow

  12. danrot commented on Oct 7, 2022

    @danrot

    Okay, would like to do so, but I feel like I know way too less about the internals of vim plugins to be able to do so 🙈

  13. dantleech commented on Oct 7, 2022

    @dantleech
    Collaborator

    one thing to note is that it's using the "simple" class-to-file converter, which basically brute forces your files to determine where classes are kept - it's not performant. It uses this if there is no composer.json in the directory Phpactor is running in, which is often the directory in which you initially opened VIM.

    you might also want to look into using the Language Server for most Phpactor features, including completion as that will use an indexer and bypass all this messy business.

  14. dantleech commented on Oct 8, 2022

    @dantleech
    Collaborator

    I've updated to fix the issue referenced by @danrot and currently cannot reproduce the original issue with the given example, so closing this ticket now. Please reopen if it's still an issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions