You put in any PHP file and get out a several PHP files that contain a list of fully-namespaced:
- class names
- interface names
- trait names
- function names
- constant names
Let's assume you have the following stub file:
namespace WP_CLI
{
trait FooTrait
{
}
class Autoloader
{
public function foo()
{
}
}
function foo_func()
{
}
define('FOO', 'BAR');
const BAZ = 'BIZ';
}
namespace WP_CLI\Utils
{
function wp_not_installed()
{
}
const BAM = 'boom';
}
namespace WP_CLI\Bootstrap
{
interface BootstrapInterface
{
}
trait BarTrait
{
}
abstract class AutoloaderStep
{
}
}
The generated exclusion lists would be:
// exclude-wp-cli-classes.php
<?php return array (
0 => 'WP_CLI\\Autoloader',
1 => 'WP_CLI\\Bootstrap\\AutoloaderStep',
);
// exclude-wp-cli-functions.php
<?php return array (
0 => 'WP_CLI\\foo_func',
1 => 'WP_CLI\\Utils\\wp_not_installed',
);
// exclude-wp-cli-constants.php
<?php return array (
0 => 'FOO',
1 => 'WP_CLI\\BAZ',
2 => 'WP_CLI\\Utils\\BAM',
);
// exclude-wp-cli-interfaces.php
<?php return array (
0 => 'WP_CLI\\Bootstrap\\BootstrapInterface',
);
// exclude-wp-cli-interfaces.php
<?php return array (
0 => 'WP_CLI\\FooTrait',
1 => 'WP_CLI\\Bootstrap\\BarTrait',
);
After generating your necessary files you can use them in combination with php-scopers excluded-symbols feature.
We already generated exclusion lists for WordPress, WP-CLI and WooCommerce. You can find them here:
- sniccowp/php-scoper-wordpress-excludes
- sniccowp/php-scoper-wp-cli-excludes
- sniccowp/php-scoper-woocommerce-excludes
These are automatically generated using the published stubs from: https://github.com/php-stubs
It's however not a requirement to use stubs files. Any PHP code will work.
composer/require sniccowp/php-scoper-excludes --dev
Generates a configuration file in the current working directory:
vendor/bin/generate-excludes generate-config
// generate-excludes.inc.php
return [
Option::EMULATE_PHP_VERSION => Option::PHP_8_0,
Option::OUTPUT_DIR => __DIR__.'/excludes',
Option::FILES => [
Finder::create()->files()
->in(__DIR__.'/vendor/php-stubs')
->depth('< 3')
->name('*.php'),
__DIR__.'/foo.php',
[__DIR__.'/bar.php', __DIR__.'/baz.php']
],
];
Using symfony/finder
is totally optional. You can also provide a list of strings or any iterable.
vendor/bin/generate-excludes
None of this would be possible without the brilliant parser by Nikita Popov nikic/php-parser.