php-scoper icon indicating copy to clipboard operation
php-scoper copied to clipboard

Class `Composer\InstalledVersions` not found (again)

Open maks-rafalko opened this issue 3 years ago • 0 comments

Bug report

Question Answer
PHP-Scoper version 3.16.0, 4.0.2
PHP version 8.1.7
Platform with version Manjaro (ArchLinux)
Composer 2.2.1
Github Repo https://github.com/martinssipenko/php-scoper-issue

This is basically a reopen of https://github.com/humbug/php-scoper/issues/532 with new details. It still doesn't work.

How to reproduce?

git clone [email protected]:martinssipenko/php-scoper-issue.git
cd php-scoper-issue

composer install

wget -q https://github.com/humbug/box/releases/download/4.0.2/box.phar --output-document=./box
chmod +x box

./box compile

# and test generated PHAR
./test.phar

Result:

PHP Fatal error:  Uncaught Error: Class "_HumbugBoxc78d51bc45e9\Composer\InstalledVersions" not found in phar:///tmp/temp/php-scoper-issue/test.phar/cli.php:8
Stack trace:
#0 /tmp/temp/php-scoper-issue/test.phar(14): require()
#1 {main}
 thrown in phar:///tmp/temp/php-scoper-issue/test.phar/cli.php on line 8

Workaround

If I add the following scoper.inc.php:

return [
    'exclude-classes' => [\Composer\InstalledVersions::class],
    'exclude-files' => ['vendor/composer/InstalledVersions.php'],
];

then, after generating new test.phar everything works as expected.

I think that PHP-Scoper (or Box) doesn't properly handle vendor/composer/InstalledVersions.php file. Without scoper.inc.php, this file is prefixed (has a prefix in a namespace), however vendor/composer/autoload_classmap.php contains not prefixed key:

<?php

// autoload_classmap.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',

    // ^^^^^^ look at this, it's not prefixed
);

Related to https://github.com/infection/infection/pull/1708

maks-rafalko avatar Jul 28 '22 19:07 maks-rafalko