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

The "defined" method not scoped with class constants

Open alexander-schranz opened this issue 3 years ago • 2 comments

Bug report

Question Answer
PHP-Scoper version 0.17.0
PHP version 8.0.5
Platform with version MacOS
Github Repo -

I try to scope a package which uses the guzzlehttp and this snippet of it with defined:

function guzzle_major_version()
{
    static $cache = null;
    if (null !== $cache) {
        return $cache;
    }
    if (\defined('\\GuzzleHttp\\ClientInterface::VERSION')) { // should have Legacy prefix
        $version = (string) ClientInterface::VERSION;
        if ($version[0] === '6') {
            return $cache = 6;
        }
        if ($version[0] === '5') {
            return $cache = 5;
        }
    } elseif (\defined('\\GuzzleHttp\\ClientInterface::MAJOR_VERSION')) { // should have Legacy prefix
        return $cache = ClientInterface::MAJOR_VERSION;
    }
    throw new \RuntimeException('Unable to determine what Guzzle version is installed.');
}

Is not correctly being replaced, by the prefix.

scoper.inc.php
<?php

declare(strict_types=1);

// scoper.inc.php

use Isolated\Symfony\Component\Finder\Finder;

return [
   'prefix' => 'Legacy',
   'finders' => [
       Finder::create()->files()->in('legacy'),
   ],
   'expose-namespaces' => [
       'Finder',
   ],
];

I found https://github.com/humbug/php-scoper/issues/281 / https://github.com/humbug/php-scoper/pull/283 so nut sure why this is not working 🤔

Reproducer: https://github.com/humbug/php-scoper/pull/668

alexander-schranz avatar Mar 11 '22 15:03 alexander-schranz

defined() is definitely handled and used quite a lot. I was however not aware you could use it to refer to a class-like constant so it could be that it's not working as expected in this case.

Meanwhile if this is a blocker for you I recommend you to fix it manually via a patcher.

theofidry avatar Mar 15 '22 10:03 theofidry

@theofidry Thx for response. I was also not aware of that 😄 . A patcher sounds like a great idea as current workaround thank you!

alexander-schranz avatar Mar 15 '22 11:03 alexander-schranz

I think I found the issue :)

theofidry avatar Nov 11 '22 22:11 theofidry

Closed by #745

theofidry avatar Nov 11 '22 22:11 theofidry