jhoff/phpeditor

Library for editing PHP files

0.0.2 2018-05-16 18:28 UTC

This package is auto-updated.

Last update: 2024-04-12 15:47:31 UTC


README

Simplified, opinionated php-based editor for PSR-4 php files, leveraging the nikic/PHP-Parser library.

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

About

The PHPEditor library is useful for making minor changes to existing PSR4 PHP files. It's assumed that each file will have a single namespaced class. New methods will be added to the end of the class. Use statements will automatically be de-duplicated and sorted by length.

Installation

Use composer to install:

composer require jhoff/phpeditor

Usage

There are a few static helpers to help you find the proper file to edit:

// 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)

Once you've opened or created the file, you can use fluent methods to make modifications and then write them to disk.

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

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

    $file->write();

Additionally, you can use the getNewFileContents method if you don't want to write the changes to disk.

The underlying nikic/PHP-Parser library will attempt to preserve any existing formatting in the file, but you may need some additional processing to make small formatting tweaks.

Docblocks

The addMethod method ( or any of the variants ) accepts a final docblock argument in the form of an associative array. Optionally provide a message or description and any other properties will automatically be formatted into proper tags. Simple tags without any text can be added by setting their value to true.

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