PHP-CS-FIXER

Automatizando um pouco do CodeReview

  • PHP 8.2 **
  • PHP 8.1
  • PHP 8.0
  • PHP 7.4
  • PHP 7.2
  • PHP 7
  • PHP 5.4

Lançada em 1994

79% da web usa PHP

https://kinsta.com/php-market-share/

Title Text

Vantagens

 

  • Simples*
  • Rápida
  • Flexível
  • Estável
  • Atualizada

Ecossistema

É
NECESSÁRIO
ORGANIZAÇÃO

PSR

  • PHP
  • Standards
  • Recommendations

"Perda de tempo!"

De fato demora um certo tempo pra tá organizando o código.

 

Por Isso

PHP-CS-FIXER

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

Instalação

(GNU/Linux)

$ sudo mkdir --parents /opt/php-cs-fixer

$ sudo composer require --working-dir=/opt/php-cs-fixer 
friendsofphp/php-cs-fixer

$ sudo ln -s /opt/php-cs-fixer/vendor/bin/php-cs-fixer 
/usr/bin/php-cs-fixer

Como usar

Configurando

.php_cs.dist

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;

Cuida

  • instagram.com/alessandro_feitoza
  • youtube.com/c/AlessandroFeitozaComPutaria
  • linkedin.com/in/AlessandroFeitoza
  • twitter.com/FeitozAle

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

E agora?

Yoda Style

<?php

$word = 'Acordeon';

//sem yoda style
if ($word === 'Acordeon') {

}

//com yoda style
if ('Acordeon' === $word) {

}

Yoda Style

<?php

$persons = [
    'Chiquim',
    'Zezim',
    'Joaozim',
];

//sem yoda style
if (in_array('Zezim', $persons)) {

}

//com yoda style
if (true === in_array('Zezim', $persons)) {

}

Void Return

antes

Void Return

depois

Array Syntax

Braces

antes

Braces

depois

combine_consecutive_issets

combine_consecutive_unsets

combine_nested_dirname

declare_strict_types

Onde encontrar mais Regras?

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

Dúvidas?

Valeu o bói.

Valeu Vaqueiro

slides.com/alessandrofeitoza

www.alessandrofeitoza.eu

twitter.com/FeitozaAle

PHP-CS-FIXER: Automatizando um pouco do CodeReview

By Alessandro Feitoza

PHP-CS-FIXER: Automatizando um pouco do CodeReview

Palestra ministrada no PHPeste 2019 em Recife.

  • 639