Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -85,6 +86,11 @@ final class ProcessCommand extends AbstractCommand
*/
private $paths = [];

/**
* @var Linter
*/
private $linter;

/**
* @param string[] $paths
* @param string[] $fileExtensions
Expand All @@ -100,6 +106,7 @@ public function __construct(
ReportingExtensionRunner $reportingExtensionRunner,
RectorNodeTraverser $rectorNodeTraverser,
StubLoader $stubLoader,
Linter $linter,
array $paths,
array $fileExtensions
) {
Expand All @@ -114,9 +121,10 @@ public function __construct(
$this->reportingExtensionRunner = $reportingExtensionRunner;
$this->rectorNodeTraverser = $rectorNodeTraverser;
$this->stubLoader = $stubLoader;
$this->paths = $paths;
$this->linter = $linter;

parent::__construct();
$this->paths = $paths;
}

protected function configure(): void
Expand Down Expand Up @@ -203,6 +211,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->configuration->mustMatchGitDiff()
);

foreach ($phpFileInfos as $phpFileInfo) {
$this->linter->lintFile($phpFileInfo);
}

$this->additionalAutoloader->autoloadWithInputAndSource($input, $source);

$this->rectorApplication->runOnFileInfos($phpFileInfos);
Expand Down
11 changes: 11 additions & 0 deletions src/Exception/Linter/LintingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Rector\Exception\Linter;

use Exception;

final class LintingException extends Exception
{
}
43 changes: 43 additions & 0 deletions src/Linter/Linter.php
Original file line number Diff line number Diff line change
@@ -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());
}
}