Setup recomendado para Laravel apps

Ariel Mejia - Feb 26 '21 - - Dev Community

Initialize Git

after create a new laravel project you must initialize git after install Husky to make it work properly:

git init
Enter fullscreen mode Exit fullscreen mode

agrega el archivo public/js/app.js al gitignore

Si usas el watcher de laravel mix tu archivo public/js/app.js se modificara constantemente por lo cual es bueno sacarlo de control de versiones:

/public/js/app.js
Enter fullscreen mode Exit fullscreen mode

Instalar paquete de cs fixer

composer require friendsofphp/php-cs-fixer
Enter fullscreen mode Exit fullscreen mode

If it fail you can use an specific version 2.16.2 version:

composer require friendsofphp/php-cs-fixer:2.16.2 -W
Enter fullscreen mode Exit fullscreen mode

Instalar paquete Husky

npm install husky@4 --save-dev
Enter fullscreen mode Exit fullscreen mode

Crea un archivo ".php_cs" y agrega el siguiente contenido:

<?php

$finder = PhpCsFixer\Finder::create()
    ->in(__DIR__)
    ->exclude(['bootstrap', 'storage', 'vendor'])
    ->name('*.php')
    ->name('_ide_helper')
    ->notName('*.blade.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

return PhpCsFixer\Config::create()
    ->setRules([
        '@PSR2' => true,
        'array_syntax' => ['syntax' => 'short'],
        'ordered_imports' => ['sortAlgorithm' => 'alpha'],
        'no_unused_imports' => true,
    ])
    ->setUsingCache(false)
    ->setFinder($finder);
Enter fullscreen mode Exit fullscreen mode

Agregar la siguiente llave en tu package.json

    "husky": {
        "hooks": {
            "pre-commit": "vendor/bin/php-cs-fixer fix --config .php_cs --allow-risky=yes --dry-run --verbose",
            "pre-push": "php artisan test"
        }
    },
Enter fullscreen mode Exit fullscreen mode

Con esta configuración cada vez que se realize un commit se revisaran todos los archivos del proyecto, también si se realiza un "push" del código este correra todos los tests por medio del comando php artisan test antes de subir los cambios.

Instalar dependencias con npm

npm install
Enter fullscreen mode Exit fullscreen mode

Correjir código anterior

Si el proyecto ya esta avanzado es posible aplicar también las reglas al código anterior ejecutando este comando:

vendor/bin/php-cs-fixer fix
Enter fullscreen mode Exit fullscreen mode

o

php-cs-fixer fix --config .php_cs --verbose
Enter fullscreen mode Exit fullscreen mode

Instalando Laravel IDE Helper

composer require --dev barryvdh/laravel-ide-helper
Enter fullscreen mode Exit fullscreen mode
Agrega comentarios para los Facades
php artisan ide-helper:generate
Enter fullscreen mode Exit fullscreen mode
Agregar comentarios para los modelos
php artisan ide-helper:models
Enter fullscreen mode Exit fullscreen mode
Genera el archivo Metafile para PHPStorm
php artisan ide-helper:meta
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .