### Install PHP-Scoper via Composer Source: https://context7.com/humbug/php-scoper/llms.txt Recommended installation using `bamarni/composer-bin-plugin` to avoid dependency conflicts. ```bash # Recommended approach to avoid dependency conflicts composer require --dev bamarni/composer-bin-plugin composer bin php-scoper require --dev humbug/php-scoper # Run via vendor/bin/php-scoper add-prefix ``` -------------------------------- ### Full PHP-Scoper Configuration Example Source: https://context7.com/humbug/php-scoper/llms.txt This is a comprehensive example of a PHP-Scoper configuration file. It demonstrates setting the namespace prefix, PHP version, output directory, file finders, excluded files, patchers, and various exclusion/exposure rules for namespaces, classes, functions, and constants. ```php 'MyVendorPrefix', // PHP version used for parsing and printing. null = host PHP version for // parsing, 7.2 for printing (ensures broad compatibility). 'php-version' => '8.2', // Output directory. null = 'build'. Overridden by --output-dir option. 'output-dir' => 'dist', // Symfony Finder instances defining which files to scope. // Ignored when using Box (Box controls which files are included). 'finders' => [ $finder::create()->files()->in('src'), $finder::create() ->files() ->ignoreVCS(true) ->notName('/LICENSE|.*\.md|.*\.dist|Makefile|composer\.json|composer\.lock/') ->exclude(['doc', 'test', 'tests', 'Tests', 'vendor-bin']) ->in('vendor'), $finder::create()->append(['bin/my-tool', 'composer.json']), ], // Files whose content is copied untouched (no scoping applied). 'exclude-files' => [ 'src/bootstrap.php', ], // Patchers: callables that post-process scoped file content. // Signature: (string $filePath, string $prefix, string $contents): string 'patchers' => [ static function (string $filePath, string $prefix, string $contents): string { // Fix a dynamic class lookup that PHP-Scoper cannot prefix automatically if ($filePath === '/path/to/src/Factory.php') { return str_replace( '$class = \'Acme\\Service\\' . $type;', '$class = \'' . $prefix . '\\Acme\\Service\\' . $type, $contents, ); } return $contents; }, ], // Symbols treated as PHP-internal (left completely untouched). 'exclude-namespaces' => ['WP', '/^PHPUnit\\Framework$/'], 'exclude-classes' => ['Stringable', '/^WP_.*/'], 'exclude-functions' => ['wp_list_users', 'str_contains'], 'exclude-constants' => ['PHP_EOL', 'ABSPATH'], // Whether global (non-namespaced) symbols are exposed by default. 'expose-global-constants' => true, 'expose-global-classes' => true, 'expose-global-functions' => true, // Symbols that are prefixed but also aliased back to their original name. 'expose-namespaces' => ['MyPublicApi'], 'expose-classes' => ['MyPublicApi\\Client', '/^MyPublicApi\\.*/'], 'expose-functions' => ['MyPublicApi\\send_request'], 'expose-constants' => ['MyPublicApi\\VERSION'], ]; ``` -------------------------------- ### Install PHP-Scoper with Phive Source: https://github.com/humbug/php-scoper/blob/main/docs/installation.md Installs the PHP-Scoper PHAR using Phive. Use --force-accept-unsigned to bypass signature verification if needed. ```bash $ phive install humbug/php-scoper --force-accept-unsigned ``` -------------------------------- ### Install Composer Dependencies (No Dev) Source: https://github.com/humbug/php-scoper/blob/main/README.md Run this command to install only production dependencies, which can speed up the scoping process by excluding unnecessary files. ```bash composer install --no-dev --prefer-dist ``` -------------------------------- ### Install sniccowp/php-scoper-wordpress-excludes Source: https://context7.com/humbug/php-scoper/llms.txt Install the package to exclude WordPress core symbols. ```bash composer require --dev sniccowp/php-scoper-wordpress-excludes ``` -------------------------------- ### Install PHP-Scoper Globally with Composer Source: https://github.com/humbug/php-scoper/blob/main/docs/installation.md Installs PHP-Scoper globally for your system using Composer. This is a convenient way to access the tool from any project. ```bash $ composer global require humbug/php-scoper ``` -------------------------------- ### Install PHP-Scoper via Phive Source: https://context7.com/humbug/php-scoper/llms.txt Install or update PHP-Scoper using the Phive package manager. ```bash phive install humbug/php-scoper --force-accept-unsigned # Upgrade phive update humbug/php-scoper --force-accept-unsigned ``` -------------------------------- ### Install PHP-Scoper via Docker Source: https://context7.com/humbug/php-scoper/llms.txt Pull the PHP-Scoper Docker image and run the `add-prefix` command. ```bash docker pull humbugphp/php-scoper docker run --rm -v "$PWD":/app humbugphp/php-scoper add-prefix --force ``` -------------------------------- ### Symfony Service Configuration Example Source: https://github.com/humbug/php-scoper/blob/main/docs/further-reading.md Example of a Symfony service configuration file using PHP configuration, demonstrating how string literals like service paths might need special handling by PHP-Scoper. ```php services() ->defaults() ->autowire() // Automatically injects dependencies in your services. ->autoconfigure() // Automatically registers your services as commands, event subscribers, etc. ; // makes classes in src/ available to be used as services // this creates a service per class whose id is the fully-qualified class name $services->load('App\', '../src/') ->exclude('../src/{DependencyInjection,Entity,Kernel.php}'); // order is important in this file because service definitions // always *replace* previous ones; add your own service configuration below }; ``` -------------------------------- ### Install PHP-Scoper Locally with Composer and Composer-bin-plugin Source: https://github.com/humbug/php-scoper/blob/main/docs/installation.md Installs PHP-Scoper locally within a project using Composer and the bamarni/composer-bin-plugin. This is recommended for managing project-specific dependencies and avoiding conflicts. ```bash $ composer require --dev bamarni/composer-bin-plugin ``` ```bash $ composer bin php-scoper require --dev humbug/php-scoper ``` ```bash $ vendor/bin/php-scoper ``` -------------------------------- ### Install WordPress Excludes Package Source: https://github.com/humbug/php-scoper/blob/main/docs/further-reading.md Use Composer to install the php-scoper-wordpress-excludes package for WordPress Core integration. ```shell composer require sniccowp/php-scoper-wordpress-excludes ``` -------------------------------- ### Date Symbol Example Source: https://github.com/humbug/php-scoper/blob/main/docs/limitations.md This example shows a date format string that PHP-Scoper might mistake for a class symbol. Use patchers for such cases. ```php const ISO8601_BASIC = 'Ymd\THis\Z'; ``` -------------------------------- ### Complex Heredoc Example Source: https://github.com/humbug/php-scoper/blob/main/docs/limitations.md A more complex heredoc example demonstrating how difficult it is for PHP-Scoper to scope classes within such structures. Patchers are necessary for proper handling. ```php '$hash', 'container.build_id' => '$id', 'container.build_time' => $time, ), __DIR__.\DIRECTORY_SEPARATOR.'Container{$hash}'); EOF; ``` -------------------------------- ### Configuring File Finders for PHP-Scoper Source: https://github.com/humbug/php-scoper/blob/main/docs/configuration.md Define which files should be scoped using Symfony Finder components. This example includes files from 'src' and 'vendor' directories, while excluding specific patterns and directories. ```php [ $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', ]) ], ]; ``` -------------------------------- ### Callable Array Example Source: https://github.com/humbug/php-scoper/blob/main/docs/limitations.md Shows an array used as a callable. PHP-Scoper does not scope the class names within such callables. Use patchers to resolve this. ```php ['Acme\Foo', 'bar']; ``` -------------------------------- ### PHP Scoper Integration Test Specification Structure Source: https://github.com/humbug/php-scoper/blob/main/CONTRIBUTING.md This is an example of a specification file used for integration tests. It defines meta-configuration and individual test specifications with input and expected output payloads. ```php [ 'title' => 'Title of the specification: this is used to quickly identify what is tested/covered by this file', // Default configuration values for this file 'prefix' => 'Humbug', 'expose-global-constants' => false, 'expose-global-classes' => false, 'expose-global-functions' => false, 'expose-namespaces' => [], 'expose-constants' => [], 'expose-classes' => [], 'expose-functions' => [], 'exclude-namespaces' => [], 'exclude-constants' => [], 'exclude-classes' => [], 'exclude-functions' => [], 'expected-recorded-classes' => [], 'expected-recorded-functions' => [], ], // List of specifications [ 'spec' => <<<'SPEC' This is a multiline spec description. It can also be a simple string when more readable. SPEC , // Each configuration setting defined in "meta" can be overridden // here for this specification 'expose-global-constants' => true, // Content of the specification: this should be the content of a plain // PHP file as you can notice by the presence of the opening ` <<<'PHP' <<<'PHP' load('App\', '../src/'); // After scoping with prefix MyPrefix $services->load('MyPrefix\App\', '../src/'); ``` -------------------------------- ### Heredoc Value Example Source: https://github.com/humbug/php-scoper/blob/main/docs/limitations.md Illustrates a heredoc value containing PHP code. PHP-Scoper cannot prefix the content within heredocs due to their dynamic nature. Patchers are required for these scenarios. ```php [ // Only scope PHP source files $finder::create()->files()->name('*.php')->in('src'), // Scope vendor, excluding dev/test artefacts $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'), // Always include the entry-point and composer manifest $finder::create()->append(['bin/my-tool', 'composer.json']), ], ]; ``` -------------------------------- ### String Class Existence Check Source: https://github.com/humbug/php-scoper/blob/main/docs/limitations.md Example of using `class_exists` with a string literal. PHP-Scoper attempts to prefix such strings, but may incorrectly prefix plain strings or global scope classes. Patchers can correct these misinterpretations. ```php class_exists('Acme\Foo'); ``` -------------------------------- ### Define Patcher for String Class Names Source: https://github.com/humbug/php-scoper/blob/main/docs/configuration.md Use patchers to modify code during scoping, especially for string-based class name resolutions that PHP-Scoper cannot automatically handle. This example replaces a dynamically constructed class string with a version that includes the scoped prefix. ```php [ static 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; }, ], ]; ``` -------------------------------- ### Handle Excluded Function Declarations Source: https://github.com/humbug/php-scoper/blob/main/docs/configuration.md When a function is excluded, its usage is left untouched. However, its declaration might still be scoped. This example shows how PHP-Scoper scopes a function declaration even if the function itself is excluded. ```php use function trigger_deprecation; // Will not be turned into Prefix\trigger_deprecation ``` ```php // global namespace! if (!function_exists('trigger_deprecation')) { function trigger_deprecation() {} } ``` ```php namespace Prefix; if (!function_exists('Prefix\trigger_deprecation')) { function trigger_deprecation() {} } ``` -------------------------------- ### List Available Make Commands Source: https://github.com/humbug/php-scoper/blob/main/CONTRIBUTING.md Run this command to see all available commands registered in the Makefile. Use 'make help' for a detailed list. ```bash # Print the list of available commands make # or make help ``` -------------------------------- ### Initialize Scoper Configuration File Source: https://context7.com/humbug/php-scoper/llms.txt Use `init` to generate a starter `scoper.inc.php` configuration file in the current directory or a specified custom path. This provides a template for configuring PHP-Scoper's behavior. ```bash php-scoper init ``` ```bash php-scoper init --config=config/php-scoper.inc.php ``` -------------------------------- ### Build a Scoped PHAR Manually Source: https://context7.com/humbug/php-scoper/llms.txt Step-by-step commands for manually scoping a project into a PHAR without using Box. ```bash # 1. Install production dependencies only composer install --no-dev --prefer-dist # 2. Scope the project into ./build # --force skips the "directory already exists" prompt php-scoper add-prefix --prefix=MyTool --output-dir=build --force # 3. Regenerate the Composer autoloader inside the scoped output composer dump-autoload --working-dir=build --classmap-authoritative # 4. Build the PHAR (example using the `phar` CLI — adjust to your tooling) # Make sure your bootstrap requires vendor/scoper-autoload.php php -d phar.readonly=0 -r " $phar = new Phar('my-tool.phar'); $phar->buildFromDirectory('build'); $phar->setDefaultStub('bin/my-tool'); " ``` -------------------------------- ### Complete PHP-Scoper Configuration Reference Source: https://github.com/humbug/php-scoper/blob/main/docs/configuration.md This is the full configuration structure for `scoper.inc.php`. Use this as a reference for all available settings. ```php null, // string|null 'php-version' => null, // string|null 'output-dir' => null, // string|null 'finders' => [], // list 'patchers' => [], // list 'exclude-files' => [], // list 'exclude-namespaces' => [], // list 'exclude-constants' => [], // list 'exclude-classes' => [], // list 'exclude-functions' => [], // list 'expose-global-constants' => true, // bool 'expose-global-classes' => true, // bool 'expose-global-functions' => true, // bool 'expose-namespaces' => [], // list 'expose-constants' => [], // list 'expose-classes' => [], // list 'expose-functions' => [], // list ]; ``` -------------------------------- ### Run PHP-Scoper with Default Options Source: https://github.com/humbug/php-scoper/blob/main/README.md Execute PHP-Scoper from your project's root directory to scope the entire current working directory to './build'. This command uses default options for output directory and prefix string. ```bash bin/php-scoper add-prefix ``` -------------------------------- ### Add Prefix to Project Files Source: https://context7.com/humbug/php-scoper/llms.txt Use `add-prefix` to scope all namespaces, classes, functions, and constants in the current project to a new namespace. Specify an output directory, prefix, and configuration file as needed. Remember to run `composer dump-autoload` in the output directory afterward. ```bash php-scoper add-prefix ``` ```bash php-scoper add-prefix \ --prefix=MyVendorPrefix \ --output-dir=./dist \ --force ``` ```bash php-scoper add-prefix src/ vendor/ \ --config=custom-scoper.inc.php \ --prefix=Acme ``` ```bash composer dump-autoload --working-dir=build --classmap-authoritative ``` -------------------------------- ### Native Function Call with Use Statement Source: https://github.com/humbug/php-scoper/blob/main/docs/limitations.md Shows how to explicitly import a function using `use function` to avoid ambiguity with native functions when prefixing. This is a recommended alternative to patchers for native function shadowing. ```php 'MyTool_Vendor', ]; ``` -------------------------------- ### Add Prefix to Current Directory Source: https://github.com/humbug/php-scoper/blob/main/README.md Use this command to prefix all relevant namespaces in the current working directory. Prefixed files will be placed in a 'build' folder. Remember to dump the Composer autoloader after prefixing if you rely on Composer. ```bash php-scoper add-prefix ``` -------------------------------- ### Bootstrap PHAR with Scoper Autoloader Source: https://context7.com/humbug/php-scoper/llms.txt Instructions on how to correctly load the `scoper-autoload.php` file in your PHAR bootstrap to include aliases for exposed symbols. Loading `autoload.php` will miss these aliases. ```bash # Load scoper-autoload.php instead of autoload.php in your PHAR bootstrap # require __DIR__ . '/vendor/scoper-autoload.php'; // correct # require __DIR__ . '/vendor/autoload.php'; // misses aliases ``` -------------------------------- ### Inspect Scoped File Output Source: https://context7.com/humbug/php-scoper/llms.txt Use `inspect` to preview the scoped content of a single file and its Symbols Registry without writing to disk. Options include specifying a custom prefix, disabling configuration files, and using quiet mode for only the scoped content. ```bash php-scoper inspect src/MyClass.php ``` ```bash php-scoper inspect src/MyClass.php --prefix=Acme --no-config ``` ```bash php-scoper inspect src/MyClass.php --quiet ``` -------------------------------- ### Appending Paths from Command Line Arguments Source: https://context7.com/humbug/php-scoper/llms.txt Paths provided as command-line arguments to the 'add-prefix' command are automatically appended to the finders defined in the configuration file. ```bash # Paths given as CLI arguments are appended to finders php-scoper add-prefix extra/bootstrap.php --force ``` -------------------------------- ### Overriding Prefix from Command Line Source: https://context7.com/humbug/php-scoper/llms.txt You can override the namespace prefix defined in the configuration file directly from the command line using the --prefix option. ```bash # Override the config-file prefix from the command line php-scoper add-prefix --prefix=MyTool_Vendor --force ``` -------------------------------- ### Configure Exposed Symbols in PHP-Scoper Source: https://github.com/humbug/php-scoper/blob/main/docs/configuration.md Define which global constants, classes, functions, and namespaces should be exposed. Set 'expose-global-constants', 'expose-global-classes', and 'expose-global-functions' to false to prevent exposure by default. Use 'expose-namespaces', 'expose-classes', 'expose-functions', and 'expose-constants' with specific names or regex patterns to control exposure. ```php false, 'expose-global-classes' => false, 'expose-global-functions' => false, 'expose-namespaces' => ['PHPUnit\Framework', '/regex/'], 'expose-classes' => ['PHPUnit\Configuration', '/regex/'], 'expose-functions' => ['PHPUnit\execute_tests', '/regex/'], 'expose-constants' => ['PHPUnit\VERSION', '/regex/'], ]; ``` -------------------------------- ### Dump Composer Autoloader for Scoped Code Source: https://github.com/humbug/php-scoper/blob/main/README.md After scoping code, dump the Composer autoloader for the scoped directory to ensure everything works correctly. This command is crucial for isolated code to function as expected. ```bash composer dump-autoload --working-dir build --classmap-authoritative ``` -------------------------------- ### Inspect Scoping for a Single File Source: https://github.com/humbug/php-scoper/blob/main/docs/configuration.md Use the `inspect` command to check how PHP-Scoper will process a specific file, which is useful for debugging patcher implementations. ```shell php-scoper inspect /path/to/offending/file ``` -------------------------------- ### Configure Laravel View Exclusions and Patching Source: https://github.com/humbug/php-scoper/blob/main/docs/further-reading.md Custom PHP-Scoper configuration for Laravel projects, excluding view files and patching a specific class to handle component loading correctly. ```php // scoper.inc.php $fileInfo->getPathname(), iterator_to_array( $finder::create() ->in('vendor/laravel/framework/src/Illuminate/Console/resources/views') ->files(), false, ), ); return [ 'exclude-files' => [ ...$consoleViewFiles, ], 'patchers' => [ static function (string $filePath, string $prefix, string $contents): string { if (!str_ends_with($filePath, 'vendor/laravel/framework/src/Illuminate/Console/View/Components/Factory.php')) { return $contents; } return str_replace( '$component = \'\\Illuminate\\Console\\View\\Components\\\' . ucfirst($method);', '$component = \'\\'.$prefix.'\\Illuminate\\Console\\View\\Components\\\' . ucfirst($method);', $contents, ); }, ], ]; ``` -------------------------------- ### Exposing Constants in PHP Scoper Source: https://github.com/humbug/php-scoper/blob/main/docs/configuration.md Demonstrates how PHP Scoper transforms a compile-time constant declaration into a runtime define() statement when exposing constants. This is useful for ensuring constants are available after scoping. ```php [$symfonyPatcher], // ... ]; ``` -------------------------------- ### PSR-0 Autoloading Configuration Source: https://github.com/humbug/php-scoper/blob/main/docs/limitations.md PHP-Scoper attempts to transform PSR-0 autoloading to PSR-4. This transformation may not work for top-level files within a PSR-0 namespace. ```json { "autoload": { "psr-0": {"JsonMapper": "src/"} } } ``` ```json { "autoload": { "psr-4": {"PhpScoperPrefix\JsonMapper": "src/"} } } ``` -------------------------------- ### Inspect a Single File with PHP-Scoper Source: https://github.com/humbug/php-scoper/blob/main/README.md Use this command to check the scoping result for a specific file without running the full scoping process. This is useful for debugging patcher configurations. ```shell php-scoper inspect path/to/my-file.php ``` -------------------------------- ### Generated Aliases for Exposed Symbols Source: https://context7.com/humbug/php-scoper/llms.txt Illustrates the `class_alias` and function wrapper entries generated in `vendor/scoper-autoload.php` for exposed symbols. This enables consumers to reference original symbol names. ```php // Class aliases humbug_phpscoper_expose_class('MyLib PublicApi Client', 'MyVendorPrefix MyLib PublicApi Client'); // Function aliases namespace MyLib PublicApi { if (!function_exists('MyLib PublicApi send')) { function send() { return MyVendorPrefix MyLib PublicApi send(...func_get_args()); } } } ``` -------------------------------- ### Scope Unknown Third-Party Function Source: https://github.com/humbug/php-scoper/blob/main/docs/further-reading.md Illustrates how PHP-Scoper prefixes an unknown third-party function. Use exclusion for symbols that should not be prefixed. ```php load()` calls. ```php [$symfonyPatcher], ]; ``` -------------------------------- ### Fix Dynamic Namespace Strings with Custom Patchers Source: https://context7.com/humbug/php-scoper/llms.txt Use patchers to modify string-based namespace lookups and regexes that PHP-Scoper cannot automatically prefix. Ensure the patcher correctly identifies the target file and applies the prefix. ```php [ // Fix a string-concatenated class lookup static function (string $filePath, string $prefix, string $contents): string { if (!str_ends_with($filePath, 'src/DynamicLoader.php')) { return $contents; } return preg_replace( "%\\\$class = 'Acme\\\Service\\\' ". \\\ "\\\$type;%", '\$class = '' . $prefix . '\\\\Acme\\\\Service\\\\'' . \\\ "\\\$type;", $contents, ); }, // Fix a regex that encodes a namespace static function (string $filePath, string $prefix, string $contents): string { if (!str_ends_with($filePath, 'src/Validator.php')) { return $contents; } return str_replace( '/^Acme\\\/', '/^' . $prefix . '\\\\Acme\\\/', $contents, ); }, ], ]; ``` -------------------------------- ### Update PHP-Scoper with Phive Source: https://github.com/humbug/php-scoper/blob/main/docs/installation.md Updates the PHP-Scoper PHAR using Phive. Use --force-accept-unsigned to bypass signature verification if needed. ```bash $ phive update humbug/php-scoper --force-accept-unsigned ``` -------------------------------- ### Configure WordPress Excluded Symbols Source: https://github.com/humbug/php-scoper/blob/main/docs/further-reading.md Define excluded constants, classes, and functions for WordPress Core using JSON files. This configuration is loaded into the php-scoper configuration. ```php // scoper.inc.php function getWpExcludedSymbols(string $fileName): array { $filePath = __DIR__.'/vendor/sniccowp/php-scoper-wordpress-excludes/generated/'.$fileName; return json_decode( file_get_contents($filePath), true, ); } $wpConstants = getWpExcludedSymbols('exclude-wordpress-constants.json'); $wpClasses = getWpExcludedSymbols('exclude-wordpress-classes.json'); $wpFunctions = getWpExcludedSymbols('exclude-wordpress-functions.json'); return [ 'exclude-constants' => $wpConstants, 'exclude-classes' => $wpClasses, 'exclude-functions' => $wpFunctions, // ... ]; ``` -------------------------------- ### Pull Official PHP-Scoper Docker Image Source: https://github.com/humbug/php-scoper/blob/main/docs/installation.md Pulls the official Docker image for PHP-Scoper. This allows you to run PHP-Scoper in a containerized environment. ```shell docker pull humbugphp/php-scoper ``` -------------------------------- ### Expose Symbols with Aliases Source: https://context7.com/humbug/php-scoper/llms.txt Configure PHP-Scoper to expose symbols, making them accessible by their original names via `class_alias` or function wrappers in `scoper-autoload.php`. This allows consumers to use unprefixed names while the internal code is prefixed. ```php false, 'expose-global-classes' => false, 'expose-global-functions' => false, // Expose the public API surface of a library bundled inside the PHAR 'expose-namespaces' => ['MyLib\PublicApi'], 'expose-classes' => ['MyLib\PublicApi\Client'], 'expose-functions' => ['MyLib\PublicApi\send'], 'expose-constants' => ['MyLib\PublicApi\VERSION'], ]; ``` -------------------------------- ### Custom Namespaced Function Exists Check Source: https://github.com/humbug/php-scoper/blob/main/docs/limitations.md PHP-Scoper assumes calls to `function_exists()` with a fully-qualified name refer to the native PHP function. If a custom namespaced `function_exists` is intended, it requires manual intervention with patchers. ```php namespace App; function_exists('NewApp\main'); ``` -------------------------------- ### Inspect Symbol Classification with PHP-Scoper CLI Source: https://context7.com/humbug/php-scoper/llms.txt Use the `inspect-symbol` command to verify if a symbol is correctly classified as internal or exposed by PHP-Scoper. This helps in debugging exclusion and exposure rules. ```bash # Verify a symbol is correctly classified as internal php-scoper inspect-symbol "WP_Error" class # Expected output: # - Internal: true # - Exposed: false ``` -------------------------------- ### Exposing a Class with Class Alias Source: https://github.com/humbug/php-scoper/blob/main/docs/configuration.md When a class is exposed, PHP-Scoper prefixes it and registers a `class_alias` to maintain the original symbol name. This ensures that code referencing the original name can still find the prefixed class. The `class_exists` statement in `scoper-autoload.php` triggers this alias after the Composer autoloader is registered. ```php $fileInfo->getPathname(), iterator_to_array( $finder::create() ->in('vendor/laravel/framework/src/Illuminate/Console/resources/views') ->files(), false, ), ); return [ 'exclude-files' => [...$consoleViewFiles], 'patchers' => [ static function (string $filePath, string $prefix, string $contents): string { if (!str_ends_with($filePath, 'vendor/laravel/framework/src/Illuminate/Console/View/Components/Factory.php')) { return $contents; } return str_replace( '$component = \'\\Illuminate\\Console\\View\\Components\\\' . ucfirst($method);', '$component = \'\\'.$prefix.'\\Illuminate\\Console\\View\\Components\\\' . ucfirst($method);', $contents, ); }, ], ]; ``` -------------------------------- ### Exclude WordPress Symbols with PHP-Scoper Source: https://context7.com/humbug/php-scoper/llms.txt Configure PHP-Scoper to exclude WordPress core symbols by reading from JSON files. ```php getWpExcludedSymbols('exclude-wordpress-constants.json'), 'exclude-classes' => getWpExcludedSymbols('exclude-wordpress-classes.json'), 'exclude-functions' => getWpExcludedSymbols('exclude-wordpress-functions.json'), ]; ``` -------------------------------- ### Exposing a Global Function Source: https://github.com/humbug/php-scoper/blob/main/docs/configuration.md For exposed global functions, PHP-Scoper re-declares the function in the scoped namespace and then defines an alias function in `scoper-autoload.php`. This alias function ensures that calls to the original global function name are redirected to the correctly namespaced version. ```php [ 'WP', '/regex/' ], 'exclude-classes' => ['Stringeable', '/regex/'], 'exclude-functions' => ['str_contains', '/regex/'], 'exclude-constants' => ['PHP_EOL', '/regex/'], ]; ``` -------------------------------- ### Exclude Symbols from Prefixing Source: https://context7.com/humbug/php-scoper/llms.txt Configure PHP-Scoper to treat specific classes, namespaces, functions, or constants as internal by excluding them from the prefixing process. This is useful for symbols that should remain globally accessible or are handled externally. ```php [ 'WP', // WP and all sub-namespaces '/^$/', // global namespace only (regex) ], 'exclude-classes' => ['Stringable', 'WP_Error', '/^WP_.*/'], 'exclude-functions' => ['wp_list_users', 'apply_filters', '/^wp_.*/'], 'exclude-constants' => ['ABSPATH', 'WP_DEBUG', '/^WP_.*/'], ]; ``` -------------------------------- ### Native Function Call in Namespace Source: https://github.com/humbug/php-scoper/blob/main/docs/limitations.md Illustrates calling a native function like `is_array` within a namespace without a `use` statement. PHP-Scoper prefixes these calls, which can break code relying on ` amespace unction_name`. Using a `use function` statement or a patcher resolves this. ```php