Skip to content

Commit

Permalink
Update the :fix command
Browse files Browse the repository at this point in the history
Allows to use the Fixer config file available in the target path.
  • Loading branch information
JackieDo committed Mar 14, 2022
1 parent e3541f6 commit 9cef826
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"require": {
"php": ">=7.1.3",
"illuminate/support": "^5.8|^6.0|^7.0",
"friendsofphp/php-cs-fixer": "^2.13"
"friendsofphp/php-cs-fixer": "^2.13",
"jackiedo/path-helper": "^1.0"
},
"require-dev": {
"illuminate/console": "^5.8|^6.0|^7.0"
Expand Down
85 changes: 60 additions & 25 deletions src/Console/Commands/ArtisanPhpCsFixerFix.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jackiedo\ArtisanPhpCsFixer\Console\Commands;

use Jackiedo\PathHelper\Path;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

Expand All @@ -28,93 +29,127 @@ class ArtisanPhpCsFixerFix extends BaseCommand
*/
protected $description = 'Fix PHP Coding Standards for directories or files';

/**
* Config file name.
*
* @var string
*/
protected $configFileName = '.php_cs';

/**
* Alternative config file name.
*
* @var string
*/
protected $distConfigFileName = '.php_cs.dist';

/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$configFile = __DIR__ . '/../../Config/.php_cs';
$configFile = realpath(__DIR__ . '/../../Config/.php_cs');
$useOptionConfig = false;

if (is_file($rootConfig = base_path($this->distConfigFileName))) {
$configFile = realpath($rootConfig);
}

if (file_exists($rootConfig = base_path('.php_cs'))) {
$configFile = $rootConfig;
if (is_file($rootConfig = base_path($this->configFileName))) {
$configFile = realpath($rootConfig);
}

if (!is_null($optionConfig = $this->option('config'))) {
$configFile = $optionConfig;
$configFile = Path::osStyle($optionConfig);
$useOptionConfig = true;
}

$commandParams = [];
$commandParams[] = '--config="' . realpath($configFile) . '"';
$commandParams[] = '--path-mode="' . $this->option('path-mode') . '"';
$commandParams = [
'config' => '--config="' . $configFile . '"',
'path-mode' => '--path-mode="' . $this->option('path-mode') . '"',
];

if (!is_null($this->option('allow-risky'))) {
$commandParams[] = '--allow-risky="' . $this->option('allow-risky') . '"';
$commandParams['allow-risky'] = '--allow-risky="' . $this->option('allow-risky') . '"';
}

if ($this->option('dry-run')) {
$commandParams[] = '--dry-run';
$commandParams['dry-run'] = '--dry-run';
}

if (!is_null($this->option('rules'))) {
$commandParams[] = '--rules="' . $this->option('rules') . '"';
$commandParams['rules'] = '--rules="' . $this->option('rules') . '"';
}

if (!is_null($this->option('using-cache'))) {
$commandParams[] = '--using-cache="' . $this->option('using-cache') . '"';
$commandParams['using-cache'] = '--using-cache="' . $this->option('using-cache') . '"';
}

if (!is_null($this->option('cache-file'))) {
$commandParams[] = '--cache-file="' . $this->option('cache-file') . '"';
$commandParams['cache-file'] = '--cache-file="' . $this->option('cache-file') . '"';
}

if ($this->option('diff')) {
$commandParams[] = '--diff';
$commandParams['diff'] = '--diff';
}

if (!is_null($this->option('format'))) {
$commandParams[] = '--format="' . $this->option('format') . '"';
$commandParams['format'] = '--format="' . $this->option('format') . '"';
}

if ($this->option('stop-on-violation')) {
$commandParams[] = '--stop-on-violation';
$commandParams['stop-on-violation'] = '--stop-on-violation';
}

if (!is_null($this->option('show-progress'))) {
$commandParams[] = '--show-progress="' . $this->option('show-progress') . '"';
}

if ($this->option('quiet')) {
$commandParams[] = '--quiet';
$commandParams['quiet'] = '--quiet';
}

if ($this->option('verbose')) {
$commandParams[] = '--verbose';
$commandParams['verbose'] = '--verbose';
}

if ($this->option('ansi')) {
$commandParams[] = '--ansi';
$commandParams['ansi'] = '--ansi';
}

if ($this->option('no-ansi')) {
$commandParams[] = '--no-ansi';
$commandParams['no-ansi'] = '--no-ansi';
}

if ($this->option('no-interaction')) {
$commandParams[] = '--no-interaction';
$commandParams['no-interaction'] = '--no-interaction';
}

$pathParam = null;

if (!empty($this->argument('path'))) {
$paths = [];
$paths = array_map(function ($path) {
return Path::absolute($path);
}, $this->argument('path'));

if (1 == count($paths) && !$useOptionConfig) {
$targetFolder = is_dir($paths[0]) ? $paths[0] : dirname($paths[0]);

foreach ($this->argument('path') as $path) {
$paths[] = '"' . base_path($path) . '"';
if (is_file($configFile = $targetFolder . DIRECTORY_SEPARATOR . $this->distConfigFileName)) {
$commandParams['config'] = '--config="' . $configFile . '"';
}

if (is_file($configFile = $targetFolder . DIRECTORY_SEPARATOR . $this->configFileName)) {
$commandParams['config'] = '--config="' . $configFile . '"';
}
}
$commandParams[] = implode(' ', $paths);

$pathParam = ' "' . implode('" "', $paths) . '"';
}

$paramString = (empty($commandParams)) ? '' : ' ' . implode(' ', $commandParams);
$paramString = (empty($commandParams) ? '' : ' ' . implode(' ', $commandParams)) . $pathParam;

passthru($this->phpCsFixerBinary . ' fix' . $paramString);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Console/Commands/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jackiedo\ArtisanPhpCsFixer\Console\Commands;

use Illuminate\Console\Command;
use Jackiedo\PathHelper\Path;

/**
* The BaseCommand class.
Expand All @@ -27,7 +28,7 @@ public function __construct()
{
parent::__construct();

$this->phpCsFixerBinary = base_path('vendor/bin/php-cs-fixer');
$this->phpCsFixerBinary = Path::osStyle(base_path('vendor/bin/php-cs-fixer'));
}

/**
Expand Down

0 comments on commit 9cef826

Please sign in to comment.