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
95 changes: 81 additions & 14 deletions bin/ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
declare(strict_types=1);

// decoupled in own "*.php" file, so ECS, Rector and PHPStan works out of the box here
use Composer\InstalledVersions;
use Composer\XdebugHandler\XdebugHandler;
use Entropy\Console\ConsoleApplication;
use Entropy\Console\Output\OutputColorizer;
use Entropy\Console\Output\OutputPrinter;
use PHP_CodeSniffer\Util\Tokens;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symplify\EasyCodingStandard\Console\EasyCodingStandardConsoleApplication;
use Symplify\EasyCodingStandard\Console\Style\SymfonyStyleFactory;
use Symplify\EasyCodingStandard\Application\Version\StaticVersionResolver;
use Symplify\EasyCodingStandard\Console\ExitCode;
use Symplify\EasyCodingStandard\DependencyInjection\EasyCodingStandardContainerFactory;
use Symplify\EasyCodingStandard\DependencyInjection\ServiceContainerFactory;

Expand Down Expand Up @@ -142,21 +145,85 @@ public function loadIfNotLoadedYet(string $file): void
}
}

$rawArgv = $_SERVER['argv'] ?? [];

// @fixes https://github.com/rectorphp/rector/issues/2205
$isXdebugAllowed = in_array('--xdebug', $rawArgv, true);
if (! $isXdebugAllowed && ! defined('PHPUNIT_COMPOSER_INSTALL')) {
$xdebugHandler = new XdebugHandler('ecs');
$xdebugHandler->check();
unset($xdebugHandler);
}

try {
$input = new ArgvInput();
$ecsContainerFactory = new EasyCodingStandardContainerFactory();
$container = $ecsContainerFactory->createFromFromInput($input);
$container = $ecsContainerFactory->createFromArgv($rawArgv);
} catch (Throwable $throwable) {
$symfonyStyleFactory = new SymfonyStyleFactory();
$symfonyStyle = $symfonyStyleFactory->create();
$outputPrinter = new OutputPrinter(new OutputColorizer());
$outputPrinter->error($throwable->getMessage());
$outputPrinter->writeln($throwable->getTraceAsString());
exit(ExitCode::FAILURE);
}

$symfonyStyle->error($throwable->getMessage());
$symfonyStyle->writeln($throwable->getTraceAsString());
exit(Command::FAILURE);
// print version and exit
if (in_array('--version', $rawArgv, true) || in_array('-V', $rawArgv, true)) {
echo sprintf('EasyCodingStandard %s', StaticVersionResolver::PACKAGE_VERSION) . PHP_EOL;
echo sprintf('+ PHP_CodeSniffer %s', InstalledVersions::getPrettyVersion('squizlabs/php_codesniffer')) . PHP_EOL;
echo sprintf('+ PHP-CS-Fixer %s', InstalledVersions::getPrettyVersion('friendsofphp/php-cs-fixer')) . PHP_EOL;
exit(ExitCode::SUCCESS);
}

/** @var EasyCodingStandardConsoleApplication $application */
$application = $container->make(EasyCodingStandardConsoleApplication::class);
/** @var ConsoleApplication $application */
$application = $container->make(ConsoleApplication::class);

$statusCode = $application->run();
$statusCode = $application->run(ecs_normalize_argv($rawArgv));
exit($statusCode);

/**
* Strip global/decoration flags Symfony Console handled implicitly, and normalize
* the "-c" config shortcut to "--config", so the Entropy input parser does not
* treat them as unknown command options.
*
* @param string[] $argv
* @return string[]
*/
function ecs_normalize_argv(array $argv): array
{
$decorationFlags = [
'--ansi',
'--no-ansi',
'--quiet',
'-q',
'--no-interaction',
'-n',
'-v',
'-vv',
'-vvv',
'--xdebug',
];

if (in_array('--no-ansi', $argv, true)) {
putenv('NO_COLOR=1');
}

$normalized = [];
foreach ($argv as $arg) {
if (in_array($arg, $decorationFlags, true)) {
continue;
}

if ($arg === '-c') {
$normalized[] = '--config';
continue;
}

if (str_starts_with($arg, '-c=')) {
$normalized[] = '--config=' . substr($arg, 3);
continue;
}

$normalized[] = $arg;
}

return $normalized;
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
"nette/utils": "^4.1",
"sebastian/diff": "^9.0",
"squizlabs/php_codesniffer": "^4.0.1",
"symfony/console": "^6.4.24",
"symfony/finder": "^7.4",
"symplify/easy-parallel": "^12.0",
"symplify/easy-parallel": "dev-main",
"webmozart/assert": "^2.4"
},
"require-dev": {
Expand Down Expand Up @@ -82,6 +81,7 @@
"fix-cs": "bin/ecs check --fix --ansi"
},
"replace": {
"symfony/console": "*",
"symfony/event-dispatcher": "7.*",
"symfony/process": "7.*",
"symfony/stopwatch": "7.*"
Expand Down
11 changes: 4 additions & 7 deletions src/Application/EasyCodingStandardApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Symplify\EasyCodingStandard\Application;

use ParseError;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\EasyCodingStandard\Caching\ChangedFilesDetector;
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle;
use Symplify\EasyCodingStandard\DependencyInjection\SimpleParameterProvider;
Expand Down Expand Up @@ -38,15 +36,14 @@ public function __construct(
private ScheduleFactory $scheduleFactory,
private ParallelFileProcessor $parallelFileProcessor,
private CpuCoreCountProvider $cpuCoreCountProvider,
private SymfonyStyle $symfonyStyle,
private ParametersMerger $parametersMerger
) {
}

/**
* @return array{coding_standard_errors?: CodingStandardError[], file_diffs?: FileDiff[], system_errors?: SystemError[]|string[], system_errors_count?: int}
*/
public function run(Configuration $configuration, InputInterface $input): array
public function run(Configuration $configuration): array
{
// 1. find files in sources
$filePaths = $this->sourceFinder->find($configuration->getSources());
Expand Down Expand Up @@ -87,11 +84,11 @@ public function run(Configuration $configuration, InputInterface $input): array

if (! $isProgressBarStarted) {
$fileCount = count($filePaths);
$this->symfonyStyle->progressStart($fileCount);
$this->easyCodingStandardStyle->progressStart($fileCount);
$isProgressBarStarted = true;
}

$this->symfonyStyle->progressAdvance($stepCount);
$this->easyCodingStandardStyle->progressAdvance($stepCount);
// running in parallel here → nothing else to do
};

Expand All @@ -106,7 +103,7 @@ public function run(Configuration $configuration, InputInterface $input): array
$mainScript,
$postFileCallback,
$configuration->getConfig(),
$input
$configuration
);
}

Expand Down
15 changes: 10 additions & 5 deletions src/Configuration/ConfigInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
namespace Symplify\EasyCodingStandard\Configuration;

use Nette\Utils\FileSystem;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\EasyCodingStandard\Application\FileProcessorCollector;
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle;

final readonly class ConfigInitializer
{
public function __construct(
private FileProcessorCollector $fileProcessorCollector,
private SymfonyStyle $symfonyStyle,
private EasyCodingStandardStyle $easyCodingStandardStyle,
private InitPathsResolver $initPathsResolver,
private \Symfony\Component\Filesystem\Filesystem $filesystem,
) {
Expand All @@ -30,13 +30,16 @@ public function createConfig(string $projectDirectory): void

// config already exists, nothing to add
if ($doesConfigExist) {
$this->symfonyStyle->warning(
$this->easyCodingStandardStyle->warning(
'We found ecs.php config, but no rules in it. Register some rules or sets there first'
);
return;
}

$response = $this->symfonyStyle->ask('No "ecs.php" config found. Should we generate it for you?', 'yes');
$response = $this->easyCodingStandardStyle->ask(
'No "ecs.php" config found. Should we generate it for you?',
'yes'
);

// be tolerant about input
if (! in_array($response, ['yes', 'YES', 'y', 'Y'], true)) {
Expand All @@ -52,7 +55,9 @@ public function createConfig(string $projectDirectory): void
// create the ecs.php file
FileSystem::write(getcwd() . '/ecs.php', $templateFileContents, null);

$this->symfonyStyle->success('The ecs.php config was generated! Re-run the command to tidy your code');
$this->easyCodingStandardStyle->success(
'The ecs.php config was generated! Re-run the command to tidy your code'
);
}

private function fillPaths(string $projectDirectory, string $templateFileContents): string
Expand Down
56 changes: 25 additions & 31 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Symplify\EasyCodingStandard\Configuration;

use Symfony\Component\Console\Input\InputInterface;
use Symplify\EasyCodingStandard\Console\Output\OutputFormatterCollector;
use Symplify\EasyCodingStandard\DependencyInjection\SimpleParameterProvider;
use Symplify\EasyCodingStandard\Exception\Configuration\SourceNotFoundException;
Expand All @@ -20,33 +19,32 @@ public function __construct(

/**
* Needs to run in the start of the life cycle, since the rest of workflow uses it.
*
* @param string[] $paths
*/
public function createFromInput(InputInterface $input): Configuration
{
$paths = $this->resolvePaths($input);

$isFixer = (bool) $input->getOption(Option::FIX);
$shouldClearCache = (bool) $input->getOption(Option::CLEAR_CACHE);
$showProgressBar = $this->canShowProgressBar($input);
$showErrorTable = ! (bool) $input->getOption(Option::NO_ERROR_TABLE);
$showDiffs = ! (bool) $input->getOption(Option::NO_DIFFS);
$parallelPort = (string) $input->getOption(Option::PARALLEL_PORT);
$parallelIdentifier = (string) $input->getOption(Option::PARALLEL_IDENTIFIER);

$outputFormat = (string) $input->getOption(Option::OUTPUT_FORMAT);

/** @var string|null $memoryLimit */
$memoryLimit = $input->getOption(Option::MEMORY_LIMIT);
public function create(
array $paths,
bool $isFixer,
bool $shouldClearCache,
bool $noProgressBar,
bool $noErrorTable,
bool $noDiffs,
string $outputFormat,
?string $config,
string $parallelPort,
string $parallelIdentifier,
?string $memoryLimit,
bool $debug,
): Configuration {
$paths = $this->resolvePaths($paths);

$showProgressBar = $this->canShowProgressBar($debug, $outputFormat, $noProgressBar);
$showErrorTable = ! $noErrorTable;
$showDiffs = ! $noDiffs;

$isParallel = SimpleParameterProvider::getBoolParameter(Option::PARALLEL);

$isReportingWithRealPath = SimpleParameterProvider::getBoolParameter(Option::REPORTING_REALPATH);

$config = $input->getOption(Option::CONFIG);
if ($config !== null) {
$config = (string) $config;
}

return new Configuration(
$isFixer,
$shouldClearCache,
Expand All @@ -64,22 +62,19 @@ public function createFromInput(InputInterface $input): Configuration
);
}

private function canShowProgressBar(InputInterface $input): bool
private function canShowProgressBar(bool $debug, string $outputFormat, bool $noProgressBar): bool
{
// --debug option shows more
$debug = (bool) $input->getOption(Option::DEBUG);
if ($debug) {
return false;
}

$outputFormat = $input->getOption(Option::OUTPUT_FORMAT);
$outputFormatter = $this->outputFormatterCollector->getByName($outputFormat);

if (! $outputFormatter->hasSupportForProgressBars()) {
return false;
}

return ! (bool) $input->getOption(Option::NO_PROGRESS_BAR);
return ! $noProgressBar;
}

/**
Expand All @@ -97,12 +92,11 @@ private function ensurePathsExists(array $paths): void
}

/**
* @param string[] $paths
* @return string[]
*/
private function resolvePaths(InputInterface $input): array
private function resolvePaths(array $paths): array
{
/** @var string[] $paths */
$paths = (array) $input->getArgument(Option::PATHS);
if ($paths === []) {
// if not paths are provided from CLI, use the config ones
$paths = SimpleParameterProvider::getArrayParameter(Option::PATHS);
Expand Down
64 changes: 0 additions & 64 deletions src/Console/Command/AbstractCheckCommand.php

This file was deleted.

Loading
Loading