PHP-CS-FIXER

Automatizando o CodeReview

Alessandro Feitoza

  • FullStack Darkmira
  • Community Manager PHP com Rapadura
  • Community Manager DarkmiraTour
  • Teacher iwtraining

VAMO
AO
POBREMA

PSR

  • PHP
  • Standards
  • Recommendatios

"Negada, vamo seguir as PSR"

"Perda de tempo!"

De fato demora um certo tempo pra tá fazendo Code Review dos padrões.

 

Por Isso

PHP-CS-FIXER

  • Open Source
  • Mantido pelo pessoal da SensioLabs
  • Criador principal: Fabien Potencier

Instalação

composer global require friendsofphp/php-cs-fixer

 

ou

composer require friendsofphp/php-cs-fixer --dev

 

ou

wget https://cs.symfony.com/download/php-cs-fixer-v2.phar -O php-cs-fixer

 

Como usar

php-cs-fixer fix --dry-run --diff -vvv path/to/file

 

PSR 1 - PSR 2

Configurando

.php_cs.dist

<?php

declare(strict_types=1);

return PhpCsFixer\Config::create();

Configurando - Finder

.php_cs.dist

<?php

declare(strict_types=1);

$finder = PhpCsFixer\Finder::create();

$csFixer = PhpCsFixer\Config::create();
$csFixer
->setFinder($finder)
->setUsingCache(false)
->setRiskyAllowed(true);

return $csFixer;

Configurando - Finder

.php_cs.dist

<?php

declare(strict_types=1);

$finder = PhpCsFixer\Finder::create();
$finder
  ->in('src')
  ->notPath('index.php');

$csFixer = PhpCsFixer\Config::create();
$csFixer->setFinder($finder);

return $csFixer;

Configurando - Rules

<?php

declare(strict_types=1);

$rules = [
    '@Symfony' => true,
    '@PHP71Migration' => true,
    'array_syntax' => ['syntax' => 'short'],
    'declare_strict_types' => true,
    'void_return' => true,
    'yoda_style' => false,
    'increment_style' => ['style' => 'post'],
];

$csFixer = PhpCsFixer\Config::create();
$csFixer->setRules($rules);

return $csFixer;

Configurando - Completa

<?php

declare(strict_types=1);

$finder = PhpCsFixer\Finder::create();
$finder->in('src')->notPath('index.php');


return PhpCsFixer\Config::create()
    ->setRules([
        '@Symfony' => true,
        '@PHP71Migration' => true,
        'array_syntax' => ['syntax' => 'short'],
        'declare_strict_types' => true,
        'void_return' => true,
        'yoda_style' => false,
        'increment_style' => ['style' => 'post'],
    ])
    ->setFinder($finder)
    ->setUsingCache(false)
    ->setRiskyAllowed(true);

Onde encontrar as Rules?

https://mlocati.github.io/php-cs-fixer-configurator/

E agora josé?

 

Dúvidas?

Valeu o bói.

Valeu Vaqueiro

slides.com/alessandrofeitoza

PHP-CS-FIXER: Automatizando o CodeReview

By Alessandro Feitoza

PHP-CS-FIXER: Automatizando o CodeReview

  • 86
Loading comments...

More from Alessandro Feitoza