pxlrbt/php-scoper-prefix-remover

此软件包最新版本(0.2.0)没有可用的许可信息。

0.2.0 2021-11-30 19:08 UTC

This package is auto-updated.

Last update: 2024-08-29 05:43:32 UTC


README

PHP-Scoper 的修复程序,用于去除全局函数/类/特性的前缀。

安装

composer require --dev pxlrbt/php-scoper-prefix-remover

使用方法

手动或通过 composer 自动加载器导入所需的类

require __DIR__ . '/vendor/autoload.php';

use pxlrbt\PhpScoper\PrefixRemover\IdentifierExtractor;
use pxlrbt\PhpScoper\PrefixRemover\RemovePrefixPatcher;

从存根文件中提取您想去除前缀的函数/类/特性/接口

$identifiers = (new IdentifierExtractor())
  ->addStub('stub-file.php')
  ->extract();

在 PHPScoper 的修复程序部分添加修复程序,并传递您之前提取的标识符

'patchers' => [
    // Some patcher
    (new RemovePrefixPatcher($identifiers)),
    // More patchers

WordPress

要去除 WordPress 函数/类的前缀,请安装 WordPress 存根

composer require --dev php-stubs/wordpress-stubs

然后将存根文件添加到提取器

use pxlrbt\PhpScoper\PrefixRemover\IdentifierExtractor;

$identifiers = (new IdentifierExtractor())
  ->addStub('vendor/php-stubs/wordpress-stubs/wordpress-stubs.php')
  ->extract();

特定 PHP 版本

可能存在与新保留关键字(例如,PHP 8.1 中 readonly 作为新关键字和 WordPress 函数名称)相关的问题。您可以通过提供非默认解析器来设置目标 PHP 版本,如下所示

use pxlrbt\PhpScoper\PrefixRemover\IdentifierExtractor;
use PhpParser\Lexer\Emulative;

$identifiers = (new IdentifierExtractor())
    ->addStub('vendor/php-stubs/wordpress-stubs/wordpress-stubs.php')
    ->setLexer(new Emulative(['phpVersion' => '8.0']))
    ->extract();