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

Fails to Process Files with Dynamic Class Instantiation and Direct String Parameters #1108

Open
oskarmodig opened this issue Jan 17, 2025 · 2 comments

Comments

@oskarmodig
Copy link

Bug report

Question Answer
PHP-Scoper version 0.18.16
PHP version 8.2.14
Platform with version Windows 11, running command in WSL, Ubuntu 22.04.3 LTS

php-scoper seems to silently skip files containing dynamic class instantiation when a direct string value is passed as a parameter to the class constructor. The issue occurs when a string literal is passed directly, but the problem does not occur when the string is stored in a variable or within an array. This behavior is inconsistent and leads to skipped files in certain scenarios.

Tested files

Test File 1 (Works):

<?php
namespace Online_Shared\Modules;
$ajax_syncer = new $syncer_class();

Test File 2 (Fails):

<?php
namespace Online_Shared\Modules;
$ajax_syncer = new $syncer_class( 'test' );

Test File 3 (Works - String in Variable):

<?php
namespace Online_Shared\Modules;
$test = 'test';
$ajax_syncer = new $syncer_class( $test );

Test File 4 (Works - String in Array):

<?php
namespace Online_Shared\Modules;
$ajax_syncer = new $syncer_class( array( 'test' ) );

Expected Behavior

  • All four test files should be scoped successfully, prepending the prefix to the namespace in the examples

Actual Behavior

  • Test File 1, Test File 3, and Test File 4 are processed as expected.
  • Test File 2 is entirely skipped during the scoping process, and is added to the output folder unchanged. All other files in the project are scoped properly.
scoper.inc.php
<?php
return array(
   'prefix'            => 'Online_Shared_Sync',
   'php-version'       => '8.2',
   'output-dir'        => 'online-shared',
   'finders'           => array(
   	\Isolated\Symfony\Component\Finder\Finder::create()
   	                                         ->files()
   	                                         ->in( 'vendor/oskarmodig/online-shared' )
   	                                         ->name( '*.php' ),
   ),
);
Output
$ rm -rf online-shared
$ php vendor/humbug/php-scoper/bin/php-scoper add-prefix
>     ____  __  ______     _____
>    / __ \/ / / / __ \   / ___/_________  ____  ___  _____
>   / /_/ / /_/ / /_/ /   \__ \/ ___/ __ \/ __ \/ _ \/ ___/
>  / ____/ __  / ____/   ___/ / /__/ /_/ / /_/ /  __/ /
> /_/   /_/ /_/_/       /____/\___/\____/ .___/\___/_/
>                                      /_/
> 
> PhpScoper version 0.18.16@aff0ef9
> 
>  122/122 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
> 
>                                                                                                                         
>  [OK] Successfully prefixed 122 files.                                                                                  
>                                                                                                                         
> 
>  // Memory usage: 16.25MB (peak: 20.13MB), time: 7.91s
@theofidry
Copy link
Member

Can you try to execute the command in debug mode, i.e. with -vvv to have a more verbose output?

@oskarmodig
Copy link
Author

Oh, right! That showed an error! I removed most of the successful files from the output below. I can note that I counted 119 [OK] files and 1 [NO]. But it still says Successfully prefixed 120 files at the end.

$ rm -rf online-shared
$ php vendor/humbug/php-scoper/bin/php-scoper -vvv add-prefix

> Using the configuration file "/mnt/c/Users/OskarModig/dev/git/sync/scoper.inc.php".
> 
>     ____  __  ______     _____
>    / __ \/ / / / __ \   / ___/_________  ____  ___  _____
>   / /_/ / /_/ / /_/ /   \__ \/ ___/ __ \/ __ \/ _ \/ ___/
>  / ____/ __  / ____/   ___/ / /__/ /_/ / /_/ /  __/ /
> /_/   /_/ /_/_/       /____/\___/\____/ .___/\___/_/
>                                      /_/
> 
> PhpScoper version 0.18.16@aff0ef9
> 
> Input
> -----
> 
> Prefix: Online_Shared_Sync
> Paths: Loaded from config
> Processing
> ----------
> 
>  * [OK] /mnt/c/Users/OskarModig/dev/git/sync/vendor/oskarmodig/online-shared/includes/Abstracts/Data_Model.php
>  ...
>  * [NO] /mnt/c/Users/OskarModig/dev/git/sync/vendor/oskarmodig/online-shared/includes/Modules/Sync_Module.php
>         Could not parse the file "/mnt/c/Users/OskarModig/dev/git/sync/vendor/oskarmodig/online-shared/includes/Modules/Sync_Module.php".: > Humbug\PhpScoper\PhpParser\UnexpectedParsingScenario: Unexpected case. Please report it. in /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/> PhpParser/UnexpectedParsingScenario.php:23
> Stack trace:
> #0 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/PhpParser/NodeVisitor/StringScalarPrefixer.php(216): > Humbug\PhpScoper\PhpParser\UnexpectedParsingScenario::create()
> #1 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/PhpParser/NodeVisitor/StringScalarPrefixer.php(195): > Humbug\PhpScoper\PhpParser\NodeVisitor\StringScalarPrefixer->prefixNewStringArg()        
> #2 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/PhpParser/NodeVisitor/StringScalarPrefixer.php(166): > Humbug\PhpScoper\PhpParser\NodeVisitor\StringScalarPrefixer->prefixStringArg()
> #3 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/PhpParser/NodeVisitor/StringScalarPrefixer.php(135): > Humbug\PhpScoper\PhpParser\NodeVisitor\StringScalarPrefixer->prefixStringScalar()        
> #4 /mnt/c/Users/OskarModig/dev/git/sync/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(114): > Humbug\PhpScoper\PhpParser\NodeVisitor\StringScalarPrefixer->enterNode()
> #5 /mnt/c/Users/OskarModig/dev/git/sync/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(227): PhpParser\NodeTraverser->traverseNode()
> #6 /mnt/c/Users/OskarModig/dev/git/sync/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(98): PhpParser\NodeTraverser->traverseArray()
> #7 /mnt/c/Users/OskarModig/dev/git/sync/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(139): PhpParser\NodeTraverser->traverseNode()
> #8 /mnt/c/Users/OskarModig/dev/git/sync/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(139): PhpParser\NodeTraverser->traverseNode()
> #9 /mnt/c/Users/OskarModig/dev/git/sync/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(227): PhpParser\NodeTraverser->traverseNode()
> #10 /mnt/c/Users/OskarModig/dev/git/sync/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(98): PhpParser\NodeTraverser->traverseArray()
> #11 /mnt/c/Users/OskarModig/dev/git/sync/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(227): PhpParser\NodeTraverser->traverseNode()
> #12 /mnt/c/Users/OskarModig/dev/git/sync/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(98): PhpParser\NodeTraverser->traverseArray()
> #13 /mnt/c/Users/OskarModig/dev/git/sync/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(227): PhpParser\NodeTraverser->traverseNode()
> #14 /mnt/c/Users/OskarModig/dev/git/sync/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(98): PhpParser\NodeTraverser->traverseArray()
> #15 /mnt/c/Users/OskarModig/dev/git/sync/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(227): PhpParser\NodeTraverser->traverseNode()
> #16 /mnt/c/Users/OskarModig/dev/git/sync/vendor/nikic/php-parser/lib/PhpParser/NodeTraverser.php(76): PhpParser\NodeTraverser->traverseArray()
> #17 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/PhpParser/NodeTraverser.php(59): PhpParser\NodeTraverser->traverse()
> #18 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/Scoper/PhpScoper.php(65): Humbug\PhpScoper\PhpParser\NodeTraverser->traverse()
> #19 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/Scoper/PhpScoper.php(52): Humbug\PhpScoper\Scoper\PhpScoper->scopePhp()
> #20 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/Scoper/PatchScoper.php(34): Humbug\PhpScoper\Scoper\PhpScoper->scope()
> #21 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/Console/ConsoleScoper.php(332): Humbug\PhpScoper\Scoper\PatchScoper->scope()
> #22 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/Console/ConsoleScoper.php(201): Humbug\PhpScoper\Console\ConsoleScoper->scopeFile()
> #23 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/Console/ConsoleScoper.php(113): Humbug\PhpScoper\Console\ConsoleScoper->scopeFiles()
> #24 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/Console/ConsoleScoper.php(81): Humbug\PhpScoper\Console\ConsoleScoper->doScope()
> #25 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/src/Console/Command/AddPrefixCommand.php(160): Humbug\PhpScoper\Console\ConsoleScoper->scope()
> #26 /mnt/c/Users/OskarModig/dev/git/sync/vendor/fidry/console/src/Bridge/Command/SymfonyCommand.php(103): Humbug\PhpScoper\Console\Command\AddPrefixCommand->execute()
> #27 /mnt/c/Users/OskarModig/dev/git/sync/vendor/symfony/console/Command/Command.php(279): Fidry\Console\Bridge\Command\SymfonyCommand->execute()
> #28 /mnt/c/Users/OskarModig/dev/git/sync/vendor/symfony/console/Application.php(1076): Symfony\Component\Console\Command\Command->run()
> #29 /mnt/c/Users/OskarModig/dev/git/sync/vendor/symfony/console/Application.php(342): Symfony\Component\Console\Application->doRunCommand()
> #30 /mnt/c/Users/OskarModig/dev/git/sync/vendor/symfony/console/Application.php(193): Symfony\Component\Console\Application->doRun()
> #31 /mnt/c/Users/OskarModig/dev/git/sync/vendor/fidry/console/src/Application/ApplicationRunner.php(83): Symfony\Component\Console\Application->run()
> #32 /mnt/c/Users/OskarModig/dev/git/sync/vendor/fidry/console/src/Application/ApplicationRunner.php(62): Fidry\Console\Application\ApplicationRunner->run()
> #33 /mnt/c/Users/OskarModig/dev/git/sync/vendor/humbug/php-scoper/bin/php-scoper(64): Fidry\Console\Application\ApplicationRunner::runApplication()
> #34 {main}
>  * [OK] /mnt/c/Users/OskarModig/dev/git/sync/vendor/oskarmodig/online-shared/includes/Modules/Template_Loader_Module.php
> ...
> 
> 
>                                                                                                                         
>  [OK] Successfully prefixed 120 files.                                                                                  
>                                                                                                                         
> 
>  // Memory usage: 14.92MB (peak: 21.61MB), time: 10.13s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants