mmoreram/php-formatter

PHP custom formatter

Installs: 19 966

Dependents: 17

Stars: 81

Watchers: 6

Forks: 8

Language: PHP

v1.0.5 2015-04-18 19:40 UTC

README

Build Status Scrutinizer Code Quality Latest Stable Version Latest Unstable Version

This PHP formatter aims to provide you some bulk actions for you PHP projects to ensure their consistency. None of them fixes PSR rules. If you want to fix PSR rules, please check fabpot/php-cs-fixer.

Tags

  • Use last unstable version ( alias of dev-master ) to stay in last commit
  • Use last stable version tag to stay in a stable release.
  • Latest Unstable Version Latest Stable Version

Install

Install PHP Formatter in this way:

$ composer global require mmoreram/php-formatter=dev-master

If it is the first time you globally install a dependency then make sure you include ~/.composer/vendor/bin in $PATH as shown here.

Always keep your PHP Formatter installation up to date:

$ composer global update mmoreram/php-formatter

.phar file

You can also use already last built .phar.

$ git clone git@github.com:mmoreram/php-formatter.git
$ cd php-formatter
$ php build/php-formatter.phar

You can copy the .phar file as a global script

$ cp build/php-formatter.phar /usr/local/bin/php-formatter

Compile

Finally you can also compile your own version of the package. ( You need set phar.readonly = Off in your php.ini ).

$ git clone git@github.com:mmoreram/php-formatter.git
$ cd php-formatter
$ composer update
$ php bin/compile
$ sudo chmod +x build/php-formatter.phar
$ build/php-formatter.phar

You can copy the .phar file as a global script

$ cp build/php-formatter.phar /usr/local/bin/php-formatter

Config

You can place a file named .formatter.yml in the root of your project. In every command execution, this will be the priority of the definitions.

If an option is set in the command, this will be used. Otherwise, if is defined in a found config file, this will be used. Otherwise, default value will be used.

This is the config reference

use-sort:
    group:
        - _main
        - Mmoreram
    group-type: each
    sort-type: alph
    sort-direction: asc

header: |
    /*
     * This file is part of the php-formatter package
     *
     * Copyright (c) 2014 Marc Morera
     *
     * For the full copyright and license information, please view the LICENSE
     * file that was distributed with this source code.
     *
     * Feel free to edit as you please, and have fun.
     *
     * @author Marc Morera <yuhu@mmoreram.com>
     */

you can also define where to search the .formatter.yml file using the --config|-c option

$ php-formatter formatter:use:sort src/ --config="src/"

Commands

PHP Formatter is a set of commands useful for your PHP projects. They do not consider any kind of Common Coding Standard, like PSR-0 or PSR-1, is more like a useful way of working for developers and reviewers.

Console Tool

Usage:
  [options] command [arguments]

Options:
  --help           -h Display this help message.
  --quiet          -q Do not output any message.
  --verbose        -v|vv|vvv Increase the verbosity of messages
  --version        -V Display this application version.
  --ansi              Force ANSI output.
  --no-ansi           Disable ANSI output.
  --no-interaction -n Do not ask any interactive question.

Available commands:
  help                   Displays help for a command
  list                   Lists commands
formatter
  formatter:header:fix   Ensures that all PHP files has header defined in config file
  formatter:use:sort     Sort Use statements

Sort all Use Statements

You can sort your Use Statements in different ways. For this command you must provide as an argument the path where to look for the PHP files you want to process.

  • command: php-formatter formatter:use:sort
  • argument: path
  • option: --group [multiple]
  • option: --group-type=one|each
  • option: --sort-type=alph|length
  • option: --sort-direction=asc|desc
  • option: --dry-run [no value]
Dry run

You can use this tool just to test the files will be modified, using option --dry-run

$ php-formatter formatter:use:sort src/ --dry-run
Group

You can sort your Use statements using as many groups as you want (--group). It means that you can group lines with same root (Symfony\) in a specific order.

Common group is named _main and if is not specified, is placed in the beginning. You can define where to place this group with the --group option

$ php-formatter formatter:use:sort src/ --group="Mmoreram" --group="_main" --group="Symfony"

This command will sort the code like this

use Mmoreram\MyClass;
use Mmoreram\MySecondClass;

use OneBundle\OneClass;
use AnotherBundle\AnotherClass;

use Symfony\OneClass;
use Symfony\AnotherClass;

As you can see, a blank line is placed between groups. If any group is defined, one big group is created with all namespaces.

When using a .formatter.yml you can also specify subgroups by entering an array

use-sort:
    group:
        - [Symfony\Component\HttpKernel, Symfony]
        - _main

This will create a Symfony group placing all Symfony\Component\HttpKernel classes on top.

Finally, the --group-type defines if you want one use literal in every namespace line

$ php-formatter formatter:use:sort src/ --group="Mmoreram" --group-type="each"

This command will sort the code like this

use AnotherBundle\AnotherClass;
use OneBundle\OneClass;
use Symfony\AnotherClass;
use Symfony\OneClass;

use Mmoreram\MyClass;
use Mmoreram\MySecondClass;

or if you want only one use for all groups.

$ php-formatter formatter:use:sort src/ --group="Mmoreram" --group-type="one"

This command will sort the code like this

use AnotherBundle\AnotherClass,
    OneBundle\OneClass,
    Symfony\AnotherClass,
    Symfony\OneClass;

use Mmoreram\MyClass,
    Mmoreram\MySecondClass;
Sort

There are two options of sorting. You can sort your namespaces alphabetically (default value)

$ php-formatter formatter:use:sort src/ --sort-type="alph"

This command will sort the code like this

use AnotherBundle\AnotherClass;
use Mmoreram\MyClass;
use Mmoreram\MySecondClass;
use OneBundle\OneClass;
use Symfony\AnotherClass;
use Symfony\OneClass;

or by length (two namespaces with same length will be sorted alphabetically)

$ php-formatter formatter:use:sort src/ --sort-type="length"

This command will sort the code like this

use AnotherBundle\AnotherClass;
use Mmoreram\MySecondClass;
use Symfony\AnotherClass;
use OneBundle\OneClass;
use Mmoreram\MyClass;
use Symfony\OneClass;

You can also define the direction of the sorting. This can be ascending (default value)

$ php-formatter formatter:use:sort src/ --sort-direction="asc"

This command will sort the code like this

use AnotherBundle\AnotherClass;
use Mmoreram\MyClass;
use Mmoreram\MySecondClass;
use OneBundle\OneClass;
use Symfony\AnotherClass;
use Symfony\OneClass;

or descending

$ php-formatter formatter:use:sort src/ --sort-direction="desc"

This command will sort the code like this

use Symfony\OneClass;
use Symfony\AnotherClass;
use OneBundle\OneClass;
use Mmoreram\MySecondClass;
use Mmoreram\MyClass;
use AnotherBundle\AnotherClass;

Fix all PHP headers

You can define your PHP header in your .formatter.yml file and this command will check and fix that all PHP files have it properly.

  • command: php-formatter formatter:header:fix
  • argument: path
  • option: --dry-run [no value]
Dry run

You can use this tool just to test the files will be modified, using the option --dry-run

$ php-formatter formatter:header:fix src/ --dry-run

Your PHP Header definition must have this format.

header: |
    /*
     * This file is part of the php-formatter package
     *
     * Copyright (c) 2014 Marc Morera
     *
     * For the full copyright and license information, please view the LICENSE
     * file that was distributed with this source code.
     *
     * Feel free to edit as you please, and have fun.
     *
     * @author Marc Morera <yuhu@mmoreram.com>
     */