PHP code example of plesk / php-scoper

1. Go to this page and download the library: Download plesk/php-scoper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

plesk / php-scoper example snippets


 declare(strict_types=1);

// scoper.inc.php

use Isolated\Symfony\Component\Finder\Finder;

return [
    'prefix' => null,                       // string|null
    'finders' => [],                        // Finder[]
    'patchers' => [],                       // callable[]
    'files-whitelist' => [],                // string[]
    'whitelist' => [],                      // string[]
    'expose-global-constants' => true,   // bool
    'expose-global-classes' => true,     // bool
    'expose-global-functions' => true,   // bool
    'exclude-constants' => [],             // string[]
    'exclude-classes' => [],               // string[]
    'exclude-functions' => [],             // string[]
];

 declare(strict_types=1);

// scoper.inc.php

use Isolated\Symfony\Component\Finder\Finder;

return [
    'finders' => [
        Finder::create()->files()->in('src'),
        Finder::create()
            ->files()
            ->ignoreVCS(true)
            ->notName('/LICENSE|.*\\.md|.*\\.dist|Makefile|composer\\.json|composer\\.lock/')
            ->exclude([
                'doc',
                'test',
                'test_old',
                'tests',
                'Tests',
                'vendor-bin',
            ])
            ->in('vendor'),
        Finder::create()->append([
            'bin/php-scoper',
            'composer.json',
        ])
    ],
];

$type = 'Foo'; // determined at runtime
$class = 'Humbug\\Format\\Type\\' . $type;

$type = 'Foo'; // determined at runtime
$scopedPrefix = array_shift(explode('\\', __NAMESPACE__));
$class = $scopedPrefix . '\\Humbug\\Format\\Type\\' . $type;

 declare(strict_types=1);

// scoper.inc.php

return [
    'patchers' => [
        function (string $filePath, string $prefix, string $content): string {
            //
            // PHP-Parser patch conditions for file targets
            //
            if ($filePath === '/path/to/offending/file') {
                return preg_replace(
                    "%\$class = 'Humbug\\\\Format\\\\Type\\\\' . \$type;%",
                    '$class = \'' . $prefix . '\\\\Humbug\\\\Format\\\\Type\\\\\' . $type;',
                    $content
                );
            }
            return $content;
        },
    ],
];

 declare(strict_types=1);

// scoper.inc.php

return [
    'exclude-classes' => ['Stringeable'],
    'exclude-functions' => ['str_contains'],
    'exclude-constants' => ['PHP_EOL'],
];

 declare(strict_types=1);

// scoper.inc.php

return [
    'expose-global-constants' => false,
    'expose-global-classes' => false,
    'expose-global-functions' => false,
];

 declare(strict_types=1);

// scoper.inc.php

return [
    'whitelist' => [
        'PHPUnit\Framework\TestCase',
        'PHPUNIT_VERSION',
        'iter\count',
        'Acme\Foo*', // Warning: will not whitelist sub namespaces like Acme\Foo\Bar but will whitelist symbols like
                     // Acme\Foo or Acme\Fooo
    ],
];



namespace Acme;

class Foo {}



namespace Humbug\Acme;

class Foo {}

\class_alias('Humbug\\Acme\\Foo', 'Acme\\Foo', \false);



// scoper-autoload.php @generated by PhpScoper

$loader = g of
                                    // `Humbug\Acme\Foo` after the
                                    // Composer autoload is registered

return $loader;



namespace Acme;

const FOO = 'X';



namespace Humbug\Acme;

\define('FOO', 'X');



// No namespace: this is the global namespace

if (!function_exists('dd')) {
    function dd() {}
}



namespace PhpScoperPrefix;

if (!function_exists('PhpScoperPrefix\dd')) {
    function dd() {...}
}



// scoper-autoload.php @generated by PhpScoper

$loader = n \PhpScoperPrefix\dd(...func_get_args());
    }
}

return $loader;

 declare(strict_types=1);

// scoper.inc.php

return [
    'whitelist' => [
        'PHPUnit\Framework\*',
    ],
];

 declare(strict_types=1);

// scoper.inc.php

return [
    'whitelist' => [
        '*',
    ],
];

 declare(strict_types=1);

// scoper.inc.php

return [
    'whitelist' => [
        'PHPUnit\F*',   // Will only whitelist symbols such as PHPUnit\F or PHPunit\Foo, but not symbols belonging to
                        // sub-namespaces like PHPunit\Foo\Bar
    ],
];

const ISO8601_BASIC = 'Ymd\THis\Z';



<<<PHP_HEREDOC


use Acme\Foo;

PHP_HEREDOC;



<<<EOF


{$namespaceLine}

// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.

if (\\class_exists(\\Container{$hash}\\{$options['class']}::class, false)) {
    // no-op
} elseif (!tainer{$hash}\\{$options['class']}(array(
    'container.build_hash' => '$hash',
    'container.build_id' => '$id',
    'container.build_time' => $time,
), __DIR__.\\DIRECTORY_SEPARATOR.'Container{$hash}');

EOF;

['Acme\Foo', 'bar'];
'Acme\Foo::bar';

class_exists('Acme\Foo');

// Will be prefixed into:

\class_exists('Humbug\Acme\Foo');



namespace Foo;

is_array([]);




namespace Foo;

use function Foo\is_array;

is_array([]);


const X = 'foo', Y = 'bar';

const X = 'foo';
const Y = 'bar';
bash
$ phive install humbug/php-scoper --force-accept-unsigned
bash
$ phive update humbug/php-scoper --force-accept-unsigned
bash
composer global 
bash
composer  php-scoper config minimum-stability dev
composer bin php-scoper config prefer-stable true 
composer bin php-scoper 
bash
php-scoper add-prefix

php-scoper add-prefix file1.php bin/file2.php
bash
bin/php-scoper add-prefix

src/
  JsonMapper.php
  JsonMapper/
    Exception.php