jhoff / phpeditor

用于编辑PHP文件的库

0.0.2 2018-05-16 18:28 UTC

This package is auto-updated.

Last update: 2024-09-12 16:39:45 UTC


README

一个针对PSR-4的PHP文件的简化、具有意见的基于PHP的编辑器,利用 PSR-4nikic/PHP-Parser 库。

Latest Stable Version Total Downloads MIT License Build Status Code Coverage Scrutinizer Code Quality

关于

PHPEditor库适用于对现有的PSR4 PHP文件进行小幅度修改。假设每个文件将有一个单名称空间类。新方法将被添加到类的末尾。使用声明将自动去重并按长度排序。

安装

使用 composer 进行安装

composer require jhoff/phpeditor

使用

有几个静态辅助函数可以帮助你找到要编辑的正确文件

// Open an existing file using a relative or absolute path
File::open($filename)

// Create a new class with the provided filename, namespace and class
File::create($filename, $namespace, $class)

// Either open or create, based on if the file exists already
File::openOrCreate($filename, $namespace, $class)

// Use reflection to find the file that defines the provided class
File::fromClass($class)

一旦你打开了或创建了文件,你可以使用流畅的方法进行修改,然后将它们写入磁盘。

    $file = \Jhoff\PhpEditor\File::open('MyClass.php');

    $file->addUse('Awesome\Library\Tool')
        ->addPublicMethod(
            'newMethod',
            'return true;'
        )

    $file->write();

此外,如果你不想将更改写入磁盘,可以使用 getNewFileContents 方法。

底层 nikic/PHP-Parser 库将尝试保留文件中任何现有的格式,但你可能需要进行一些额外的处理以进行小的格式调整。

Docblocks

addMethod 方法(或任何变体)接受一个关联数组形式的最终docblock参数。可选提供 messagedescription,任何其他属性都将自动格式化为正确的标签。可以添加不带文本的简单标签,通过将它们的值设置为 true

    [
        'message' => 'This is the docblock message',
        'description' => 'Some information about the method',
        'param' => [
            'string $paramOne',
            'array $paramTwo',
        ],
        'return' => 'void',
        'internal' => true,
    ]