diff --git a/bin/spiral-cs b/bin/spiral-cs index 659f391..3d4b57b 100755 --- a/bin/spiral-cs +++ b/bin/spiral-cs @@ -3,11 +3,18 @@ declare(strict_types=1); +use PHP_CodeSniffer\Runner; +use PhpCsFixer\Console\Application as PhpCsFixApplication; +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 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 @@ -53,16 +60,16 @@ class CodeStyleHelper { foreach ( [ - VENDOR_DIR . '/spiral/' . self::PACKAGE_NAME, + VENDOR_DIR . DIRECTORY_SEPARATOR . 'spiral' . DIRECTORY_SEPARATOR . self::PACKAGE_NAME, __DIR__, - __DIR__ . '/..' + __DIR__ . DIRECTORY_SEPARATOR . '..' ] as $directory ) { if ( is_dir($directory) && - is_dir($configDir = $directory . '/config') && - file_exists($configDir . '/' . self::PHP_CS_FIXER_CONFIG_FILE) && - file_exists($configDir . '/' . self::PHP_CS_CONFIG_FILE) + 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; @@ -88,28 +95,36 @@ class CodeStyleHelper return CONFIG_DIR . DIRECTORY_SEPARATOR . self::PHP_CS_FIXER_CONFIG_FILE; } - public function wrapPaths(array $paths) : array + 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(); + $finder->in($paths)->files(); + + foreach ($finder as $file) { + $lines = file((string)$file); + foreach ($lines as &$line) { + $line = rtrim($line, "\n\r "); + unset($line); + } + + file_put_contents((string)$file, join("\n", $lines)); + } + } +} $codeStyleHelper = CodeStyleHelper::init(); require COMPOSER_AUTOLOAD; require VENDOR_DIR . '/squizlabs/php_codesniffer/autoload.php'; -use Symfony\Component\Console\Application; -use PhpCsFixer\Console\Application as PhpCsFixApplication; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; -use PHP_CodeSniffer\Runner; - $codeStyleApp = new Application('Spiral code style application'); $codeStyleApp->addCommands([ (new Symfony\Component\Console\Command\Command('fix')) @@ -119,10 +134,19 @@ $codeStyleApp->addCommands([ 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); + } + // PHPCBF call $_SERVER['argv'] = array_merge([ 'placeholder', diff --git a/config/.php_cs b/config/.php_cs index 99112a2..6011970 100644 --- a/config/.php_cs +++ b/config/.php_cs @@ -6,50 +6,42 @@ return PhpCsFixer\Config::create() ->setCacheFile(__DIR__ . '/.php_cs.cache') ->setRiskyAllowed(true) ->setRules([ - '@PSR2' => true, - 'binary_operator_spaces' => [ - 'default' => null, + '@PSR2' => true, + 'binary_operator_spaces' => [ + 'default' => null, 'operators' => [ - '|' => 'single_space', + '|' => 'single_space', '!==' => 'single_space', - '!=' => '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' => [ + '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, + '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' => [ + '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, - ]) - ->setFinder( - PhpCsFixer\Finder::create() - ->in(__DIR__) - ->exclude([ - 'vendor', - 'bin', - ]) - ); + 'no_useless_else' => true, + 'no_useless_return' => true, + 'phpdoc_separation' => false, + 'yoda_style' => false, + 'void_return' => true, + ]);