mallardduck/prettier-php-runner

Reformat code using Prettier in PHP

1.0.0 2022-02-21 22:10 UTC

This package is auto-updated.

Last update: 2024-04-22 02:53:10 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Prettier is an opinionated code formatter based on NodeJS that supports a variety of languages. Now you can use the power of Prettier with your PHP projects too!

\MallardDuck\PrettierPhp\PrettierHtml::format(
    html: '<html><body><div><h1>Heading</h1></div></body></html>'
);

Installation

You can install the package via composer:

composer require mallardduck/prettier-php-runner

In your project, you should have the JavaScript package prettier installed. You can install it via npm...

npm install prettier

... or Yarn.

yarn add prettier

Make sure you have installed Node 10 or higher.

Usage

Here's an example where we are going to highlight some PHP code.

use Spatie\ShikiPhp\Prettier;

PrettierHtml::format(
    '<html><body><div><h1>Heading</h1></div></body></html>'
);

The output is this chunk of HTML which will render beautifully in the browser:

<html>
    <body>
        <div><h1>Heading</h1></div>
    </body>
</html>

Using Node Version Manager

Under the hood, this package will run a node command beautify your HTML input.

If you use NVM during development, then the package will be unlikely to find your version of node as it looks for the node executable in /usr/local/bin and /opt/homebrew/bin. If this is the case for you, then you should create a symlink between the node distributable in your NVM folder.

Such a command might look like this:

sudo ln -s /home/some-user/.nvm/versions/node/v17.3.1/bin/node /usr/local/bin/node

Creating this symlink will allow the package to find your NPM executable. Please note, if you change your NPM version you will have to update your symlinks accordingly.

Testing

You can run all the tests with this command:

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.