Add PHP linter

This commit is contained in:
TomasVotruba 2020-01-17 16:28:33 +01:00
parent 9d82c9e76a
commit e4f1380c19
4 changed files with 68 additions and 1 deletions

View File

@ -23,6 +23,7 @@ services:
alias: 'Rector\Console\Application'
Symfony\Component\Console\Descriptor\TextDescriptor: null
Symfony\Component\Process\PhpExecutableFinder: null
# PhpParser - Parser
PhpParser\ParserFactory: null

View File

@ -15,6 +15,7 @@ use Rector\Console\Shell;
use Rector\Extension\ReportingExtensionRunner;
use Rector\FileSystem\FilesFinder;
use Rector\Guard\RectorGuard;
use Rector\Linter\Linter;
use Rector\PhpParser\NodeTraverser\RectorNodeTraverser;
use Rector\Stubs\StubLoader;
use Symfony\Component\Console\Input\InputArgument;
@ -85,6 +86,11 @@ final class ProcessCommand extends AbstractCommand
*/
private $paths = [];
/**
* @var Linter
*/
private $linter;
/**
* @param string[] $paths
* @param string[] $fileExtensions
@ -100,6 +106,7 @@ final class ProcessCommand extends AbstractCommand
ReportingExtensionRunner $reportingExtensionRunner,
RectorNodeTraverser $rectorNodeTraverser,
StubLoader $stubLoader,
Linter $linter,
array $paths,
array $fileExtensions
) {
@ -114,9 +121,10 @@ final class ProcessCommand extends AbstractCommand
$this->reportingExtensionRunner = $reportingExtensionRunner;
$this->rectorNodeTraverser = $rectorNodeTraverser;
$this->stubLoader = $stubLoader;
$this->paths = $paths;
$this->linter = $linter;
parent::__construct();
$this->paths = $paths;
}
protected function configure(): void
@ -203,6 +211,10 @@ final class ProcessCommand extends AbstractCommand
$this->configuration->mustMatchGitDiff()
);
foreach ($phpFileInfos as $phpFileInfo) {
$this->linter->lintFile($phpFileInfo);
}
$this->additionalAutoloader->autoloadWithInputAndSource($input, $source);
$this->rectorApplication->runOnFileInfos($phpFileInfos);

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Rector\Exception\Linter;
use Exception;
final class LintingException extends Exception
{
}

43
src/Linter/Linter.php Normal file
View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Rector\Linter;
use Rector\Exception\Linter\LintingException;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
use Symplify\SmartFileSystem\SmartFileInfo;
/**
* Inspired by https://github.com/keradus/PHP-CS-Fixer/blob/a838434c7584e4744e2fca9950687d5326212560/Symfony/CS/Linter/Linter.php
*/
final class Linter
{
/**
* @var string
*/
private $executable;
public function __construct(PhpExecutableFinder $phpExecutableFinder)
{
$executable = $phpExecutableFinder->find();
if ($executable === false) {
throw new LintingException('PHP executable was not found');
}
$this->executable = $executable;
}
public function lintFile(SmartFileInfo $smartFileInfo): void
{
$process = new Process([$this->executable, '-l', $smartFileInfo->getRealPath()]);
$process->run();
if ($process->isSuccessful()) {
return;
}
throw new LintingException($process->getOutput(), (int) $process->getExitCode());
}
}