matt-allan/laravel-code-style

Install Global? [Feature] request (Thanks!!)

kevyworks opened this issue · 2 comments

It happens I have an older Laravel version and installing this directly errors are thrown due to laravel versions. I manage to make it work by: (I include installing php-cs-fixer for completeness)

Install

$ curl -L https://cs.symfony.com/download/php-cs-fixer-v2.phar -o php-cs-fixer
$ sudo chmod a+x php-cs-fixer
$ sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer
$ composer global require matt-allan/laravel-code-style
$ nano /to/my/project/.php_cs

Created .php_cs file to to/my/project

<?php

require $_SERVER['HOME'] . '/.composer/vendor/autoload.php';
require __DIR__.'/vendor/autoload.php';
require __DIR__.'/bootstrap/app.php';

return (new MattAllan\LaravelCodeStyle\Config())
    ->setFinder(
        PhpCsFixer\Finder::create()
            ->in(app_path())
            ->in(config_path())
            ->in(database_path())
            ->notPath(database_path('migrations'))
            ->in(resource_path('lang'))
            ->in(base_path('routes'))
            ->in(base_path('tests'))
    )
    ->setRules([
        '@Laravel' => true,
        '@Laravel:risky' => true,
    ])
    ->setRiskyAllowed(true);

Check if it works.

$ php-cs-fixer fix --dry-run --diff
Loaded config Laravel from "/Volumes/KB/www/coreapp/.php_cs".
Using cache file ".php_cs.cache".

Checked all files in 0.007 seconds, 20.000 MB memory used
$

Obviously for above results, I already executed: php-cs-fixer fix

Hello,

Glad to here it works. I haven't tested it but it should work with most Laravel versions; I don't remember any breaking changes to the service provider or the *_path methods that would break the package. However there is a small risk that old Laravel code broke some of the rules it now uses and it's a lot of work to test old versions so I don't want to official support them 😁

I'm going to go ahead and close this. Thanks for documenting the workaround.

@matt-allan Hi, thanks for your feedback, I have forked this repo and added a /bin/ which I am still planning which of best approach to handle this.

Pseudo CLI:

$ composer global install laravel-code-style

#-- Generates .php_cs
$ laravel-cs-fixer generate --for=<[auto], laravel, basic>

#-- Downloads php-cs-fixer and Installs the package
$ laravel-cs-fixer install

#-- Looks for .php_cs/.php_cs.dist and generate on-demand base on project structure,
#-- if its a laravel project - it adds appropriate folders or your .php_cs default 
#-- else, maybe src, test folders
#-- generate file will be (--config=/tmp/<time>.php_cs.dist)
$ laravel-cs-fixer fix --dry-run --diff

#-- Calls/Alias above
$ laravel-cs-fixer check

#-- Fix all issues?
$ laravel-cs-fixer fix

Thanks Again!