-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Open
Labels
Description
I've found that it's hard introduce PHP_CodeSniffer to old projects because it finds a lot of issues that can't be fixed at once.
Currently, as a workaround I use something like this (the code is console command from laravel app)
CodeSnifferCommand.php
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Console\Traits\GitCommandHelper;
use Symfony\Component\Process\Process;
class CodeSnifferCommand extends Command
{
use GitCommandHelper;
/** @var string */
protected $signature = 'common:phpcs
{file? : file to sniff}
{--added : sniff all files that have added to the GIT-index}
{--untracked : sniff all GIT-untracked files}
{--all : sniff all changed files}
{--branch=origin/dev : sniff all filed that different between current branch and \'branch-name\'}
{--changed : sniff all GIT-modified files that have not yet been added to the index}';
/** @var string */
protected $description = 'Code Sniffer';
public function handle(): void
{
$command = sprintf('vendor/bin/phpcs %s', $this->getPathsToChangedFiles());
$process = Process::fromShellCommandline($command, base_path());
$process->run();
$this->warn('put declare(strict_types = 1) by yourself please');
$this->line(
$process->getOutput()
);
$this->warn('put declare(strict_types = 1) by yourself please');
}
}
CodeBeautifierCommand.php
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Console\Traits\GitCommandHelper;
use Symfony\Component\Process\Process;
class CodeBeautifierCommand extends Command
{
use GitCommandHelper;
/** @var string */
protected $signature = 'common:phpcbf
{file? : file to beautify}
{--added : sniff all files that have added to the GIT-index}
{--untracked : sniff all GIT-untracked files}
{--all : sniff all changed files}
{--branch=dev : sniff all filed that different between current branch and \'branch-name\'}
{--changed : sniff all GIT-modified files that have not yet been added to the index}';
/** @var string */
protected $description = 'Code Beautifier';
public function handle(): void
{
$command = sprintf('vendor/bin/phpcbf %s', $this->getPathsToChangedFiles());
$process = Process::fromShellCommandline($command, base_path());
$process->run();
$this->line(
$process->getOutput()
);
}
}
GitCommandHelper.php
<?php
declare(strict_types=1);
namespace App\Console\Traits;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Process\Process;
use LogicException;
/**
* @property InputInterface $input
*/
trait GitCommandHelper
{
protected function getPathsToChangedFiles(): string
{
$files = '';
if ($this->option('all')) {
$this->input->setOption('changed', true);
$this->input->setOption('added', true);
$this->input->setOption('untracked', true);
$this->input->setOption('branch', true);
}
if ($this->argument('file')) {
$files .= $this->argument('file') . ' ';
}
if ($this->option('changed')) {
$changedFilesCommand = 'git diff --name-only';
$changedFilesProcess = Process::fromShellCommandline($changedFilesCommand, base_path());
$changedFilesProcess->run();
$files .= $changedFilesProcess->getOutput();
}
if ($this->option('added')) {
$addedCommand = 'git diff --name-only --cached';
$addedProcess = Process::fromShellCommandline($addedCommand, base_path());
$addedProcess->run();
$files .= $addedProcess->getOutput();
}
if ($this->option('untracked')) {
$untrackedCommand = 'git ls-files --others --exclude-standard';
$untrackedProcess = Process::fromShellCommandline($untrackedCommand, base_path());
$untrackedProcess->run();
$files .= $untrackedProcess->getOutput();
}
if ($this->option('branch')) {
$branchDiffCommand = 'git diff --name-only --diff-filter=d ' . $this->option('branch') . ' | grep \.php$ | grep -v \.blade.php$ ';
$branchDiffProcess = Process::fromShellCommandline($branchDiffCommand, base_path());
$branchDiffProcess->run();
$files .= $branchDiffProcess->getOutput();
}
$filesArray = array_unique(explode(' ', trim(preg_replace("/[\n\r]/", ' ', $files))));
$preparedResult = implode(' ', $filesArray);
if ($preparedResult === '') {
throw new LogicException('Changed not found');
}
return $preparedResult;
}
}
and the doc's says something like this
- run `git fetch && php artisan common:phpcs --branch=origin/dev` to only show changes compared to `dev`, make sure it's `origin/dev` not just `dev`
But the approach that I've shown above is not fully correct, since I can't change all the methods in the file and this way I need to manually find issues that where introduced in current branch/feature.
Also the baseline will be useful for CI
as examples of baselines we can look at psalm and also I've opened similar issue at DaveLiddament/sarb#26
sebb, gersonfs, ArtemGoutsoul, tdtm, dungahk and 4 moreishan-eventrac