Skip to content

Commit

Permalink
Merge pull request #3 from butschster/feature/custom-config
Browse files Browse the repository at this point in the history
[WIP] Refactor: Separated spiral-cs  into command classes and Helper class.
  • Loading branch information
SerafimArts committed Aug 24, 2021
2 parents c22a1f3 + 0a9f929 commit d6bb1f7
Show file tree
Hide file tree
Showing 17 changed files with 501 additions and 244 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/vendor/
.idea
.php_cs.cache
.php-cs-fixer.cache
composer.lock
.phpunit.result.cache
221 changes: 34 additions & 187 deletions bin/spiral-cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,206 +3,53 @@

declare(strict_types=1);

use PHP_CodeSniffer\Runner;
use PhpCsFixer\Console\Application as PhpCsFixApplication;
use Spiral\CodeStyle\CodeStyleHelper;
use Spiral\CodeStyle\Commands\CheckCommand;
use Spiral\CodeStyle\Commands\FixCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class CodeStyleHelper
{
public const PACKAGE_NAME = 'code-style';
public const PHP_CS_CONFIG_FILE = 'ruleset.xml';
public const PHP_CS_FIXER_CONFIG_FILE = '.php_cs';

public static function init(): self
{
self::defineComposerAutoloadLocation();
self::defineConfigFolderLocation();
return new self();
}

private function __construct()
{
}

private static function defineComposerAutoloadLocation(): void
{
foreach (
[
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/vendor/autoload.php'
] as $file
) {
if (file_exists($file)) {
define('COMPOSER_AUTOLOAD', realpath($file));
define('VENDOR_DIR', dirname(realpath($file)));
define('PROJECT_ROOT', dirname(VENDOR_DIR));
break;
}
}
if (!defined('COMPOSER_AUTOLOAD')) {
fwrite(
STDERR,
'You need to set up the project dependencies using Composer:' . PHP_EOL . PHP_EOL .
' composer install' . PHP_EOL . PHP_EOL .
'You can learn all about Composer on https://getcomposer.org/.' . PHP_EOL
);
die(1);
}
}

private static function defineConfigFolderLocation(): void
{
foreach (
[
VENDOR_DIR . DIRECTORY_SEPARATOR . 'spiral' . DIRECTORY_SEPARATOR . self::PACKAGE_NAME,
__DIR__,
__DIR__ . DIRECTORY_SEPARATOR . '..'
] as $directory
) {
if (
is_dir($directory) &&
is_dir($configDir = $directory . DIRECTORY_SEPARATOR . 'config') &&
file_exists($configDir . DIRECTORY_SEPARATOR . self::PHP_CS_FIXER_CONFIG_FILE) &&
file_exists($configDir . DIRECTORY_SEPARATOR . self::PHP_CS_CONFIG_FILE)
) {
define('CONFIG_DIR', $configDir);
break;
}
}

if (!defined('CONFIG_DIR')) {
fwrite(
STDERR,
'Unable to find code style config locations' . PHP_EOL
);
die(1);
function defineComposerAutoloadLocation(): void
{
foreach (
[
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/vendor/autoload.php'
] as $file
) {
if (file_exists($file)) {
define('COMPOSER_AUTOLOAD', realpath($file));
define('VENDOR_DIR', dirname(realpath($file)));
define('PROJECT_ROOT', dirname(VENDOR_DIR));
break;
}
}

public function getPhpCsConfigPath(): string
{
return CONFIG_DIR . DIRECTORY_SEPARATOR . self::PHP_CS_CONFIG_FILE;
}

public function getPhpCsFixerConfigPath(): string
{
return CONFIG_DIR . DIRECTORY_SEPARATOR . self::PHP_CS_FIXER_CONFIG_FILE;
}

public function wrapPaths(array $paths): array
{
array_walk($paths, function (&$path) {
$path = PROJECT_ROOT . DIRECTORY_SEPARATOR . $path;
});
return $paths;
}

public function normalizeEndings(array $paths)
{
$finder = new Symfony\Component\Finder\Finder();
foreach ($paths as $path) {
if (is_file($path)) {
$finder->append([$path]);
} else {
$finder->in($path);
}
}

foreach ($finder->files() as $path) {
$this->normalizeContent((string)$path);
}
if (!defined('COMPOSER_AUTOLOAD')) {
fwrite(
STDERR,
'You need to set up the project dependencies using Composer:' . PHP_EOL . PHP_EOL .
' composer install' . PHP_EOL . PHP_EOL .
'You can learn all about Composer on https://getcomposer.org/.' . PHP_EOL
);
die(1);
}

private function normalizeContent(string $filename)
{
$lines = file($filename);
foreach ($lines as &$line) {
$line = rtrim($line, "\n\r ");
unset($line);
}

file_put_contents($filename, join("\n", $lines));
}
}

$codeStyleHelper = CodeStyleHelper::init();

defineComposerAutoloadLocation();
require COMPOSER_AUTOLOAD;
require VENDOR_DIR . '/squizlabs/php_codesniffer/autoload.php';

$codeStyleApp = new Application('Spiral code style application');
$codeStyleApp->addCommands([
(new Symfony\Component\Console\Command\Command('fix'))
->setDescription('Code style fix command')
->addArgument(
'paths',
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
'Enumerate directories or files to check'
)
->addOption(
'preserve-endings',
'l',
InputOption::VALUE_NONE,
'Preserve original line-endings, otherwise forced into LF'
)
->setCode(function (InputInterface $input, OutputInterface $output) use ($codeStyleHelper) {
$paths = $codeStyleHelper->wrapPaths($input->getArgument('paths'));

if (!$input->getOption('preserve-endings')) {
$codeStyleHelper->normalizeEndings($paths);
}
$codeStyleHelper = CodeStyleHelper::init(__DIR__);

// PHPCBF call
$_SERVER['argv'] = array_merge([
'placeholder',
'--report=code',
'--standard=' . $codeStyleHelper->getPhpCsConfigPath(),
], $paths);

$runner = new Runner();
$runner->runPHPCBF();
require VENDOR_DIR . '/squizlabs/php_codesniffer/autoload.php';

// PHPCS-Fixer call
$application = new PhpCsFixApplication();
$application->setAutoExit(false);
foreach ($paths as $path) {
$_SERVER['argv'] = [
'placeholder',
'fix',
'--config',
$codeStyleHelper->getPhpCsFixerConfigPath(),
$path,
];
$application->run();
}
}),
(new Symfony\Component\Console\Command\Command('check'))
->setDescription('Code style correctness check command')
->addArgument(
'paths',
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
'Enumerate directories or files to check'
)
->setCode(function (InputInterface $input, OutputInterface $output) use ($codeStyleHelper) {
$_SERVER['argv'] = array_merge([
'placeholder',
'--report=code',
'--standard=' . $codeStyleHelper->getPhpCsConfigPath(),
], $codeStyleHelper->wrapPaths($input->getArgument('paths')));
$runner = new Runner();
$exitCode = $runner->runPHPCS();
$codeStyleApp = new Application('Spiral code style application');

if ($exitCode === 0) {
$output->writeln('<info>No codestyle issues</info>');
}
return $exitCode;
}),
$codeStyleApp->addCommands([
new FixCommand($codeStyleHelper),
new CheckCommand($codeStyleHelper),
]);

$codeStyleApp->run();
$codeStyleApp->run();
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"bin": [
"bin/spiral-cs"
],
"autoload": {
"psr-4": {
"Spiral\\CodeStyle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Spiral\\Tests\\": "tests/"
Expand Down
49 changes: 49 additions & 0 deletions config/.php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

$config = new PhpCsFixer\Config();

return $config
->setCacheFile(__DIR__ . '/.php-cs-fixer.cache')
->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'binary_operator_spaces' => [
'default' => null,
'operators' => [
'|' => 'single_space',
'!==' => 'single_space',
'!=' => 'single_space',
'==' => 'single_space',
'===' => 'single_space',
]
],
'ordered_class_elements' => true,
'trailing_comma_in_multiline_array' => false,
'declare_strict_types' => true,
'linebreak_after_opening_tag' => true,
'blank_line_after_opening_tag' => true,
'single_quote' => true,
'lowercase_cast' => true,
'short_scalar_cast' => true,
'no_leading_import_slash' => true,
'declare_equal_normalize' => [
'space' => 'none'
],
'new_with_braces' => true,
'no_blank_lines_after_phpdoc' => true,
'single_blank_line_before_namespace' => true,
'visibility_required' => ['property', 'method', 'const'],
'ternary_operator_spaces' => true,
'unary_operator_spaces' => true,
'return_type_declaration' => true,
'concat_space' => [
'spacing' => 'one'
],
'no_useless_else' => true,
'no_useless_return' => true,
'phpdoc_separation' => false,
'yoda_style' => false,
'void_return' => true,
]);
47 changes: 0 additions & 47 deletions config/.php_cs

This file was deleted.

Loading

0 comments on commit d6bb1f7

Please sign in to comment.