From 18ef20ecf5c45ecbc4116324cc818742e79a81db Mon Sep 17 00:00:00 2001 From: FreakyWizard Date: Wed, 20 Nov 2024 11:06:25 +0000 Subject: [PATCH] (auto): apply php-cs-fixer changes --- config/rules.php | 124 +++++++++---------- src/Config.php | 69 +++++------ src/Interfaces/ConfigInterface.php | 58 ++++----- src/Traits/Helpers.php | 52 ++++---- tests/Feature/ConfigTest.php | 46 +++---- tests/Unit/ConfigRulesMergeTest.php | 178 +++++++++++++-------------- tests/Unit/ConfigRulesRemoveTest.php | 78 ++++++------ 7 files changed, 302 insertions(+), 303 deletions(-) diff --git a/config/rules.php b/config/rules.php index 5c6589a..cf5a5a2 100644 --- a/config/rules.php +++ b/config/rules.php @@ -3,66 +3,66 @@ declare(strict_types=1); return [ - 'line_ending' => "\n", - 'risky_allowed' => true, - 'set_indent' => "\t", - 'rules' => [ - '@PSR2' => true, - 'indentation_type' => true, - 'single_quote' => true, - 'array_syntax' => ['syntax' => 'short'], - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - 'no_unused_imports' => true, - 'no_extra_blank_lines' => [ - 'tokens' => [ - 'extra', - 'throw', - 'continue', - 'curly_brace_block', - 'parenthesis_brace_block', - ], - ], - 'not_operator_with_successor_space' => true, - 'logical_operators' => true, - 'trailing_comma_in_multiline' => true, - 'phpdoc_scalar' => true, - 'phpdoc_var_without_name' => true, - 'phpdoc_single_line_var_spacing' => true, - 'unary_operator_spaces' => true, - 'phpdoc_trim' => true, - 'phpdoc_trim_consecutive_blank_line_separation' => true, - 'align_multiline_comment' => true, - 'array_indentation' => true, - 'no_superfluous_elseif' => true, - 'single_blank_line_before_namespace' => true, - 'blank_line_after_opening_tag' => true, - 'no_blank_lines_after_phpdoc' => true, - 'phpdoc_separation' => true, - 'method_chaining_indentation' => true, - 'binary_operator_spaces' => [ - 'default' => 'single_space', - 'operators' => [ - '=>' => 'single_space', - '|' => 'single_space', - ], - ], - 'return_type_declaration' => [ - 'space_before' => 'none', - ], - 'blank_line_before_statement' => [ - 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], - ], - 'full_opening_tag' => true, - 'method_argument_space' => [ - 'on_multiline' => 'ensure_fully_multiline', - 'keep_multiple_spaces_after_comma' => true, - ], - 'yoda_style' => [ - 'always_move_variable' => true, - 'equal' => true, - 'identical' => true, - 'less_and_greater' => true, - ], - 'declare_strict_types' => true, - ], + 'line_ending' => "\n", + 'risky_allowed' => true, + 'set_indent' => "\t", + 'rules' => [ + '@PSR2' => true, + 'indentation_type' => true, + 'single_quote' => true, + 'array_syntax' => ['syntax' => 'short'], + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + 'no_unused_imports' => true, + 'no_extra_blank_lines' => [ + 'tokens' => [ + 'extra', + 'throw', + 'continue', + 'curly_brace_block', + 'parenthesis_brace_block', + ], + ], + 'not_operator_with_successor_space' => true, + 'logical_operators' => true, + 'trailing_comma_in_multiline' => true, + 'phpdoc_scalar' => true, + 'phpdoc_var_without_name' => true, + 'phpdoc_single_line_var_spacing' => true, + 'unary_operator_spaces' => true, + 'phpdoc_trim' => true, + 'phpdoc_trim_consecutive_blank_line_separation' => true, + 'align_multiline_comment' => true, + 'array_indentation' => true, + 'no_superfluous_elseif' => true, + 'single_blank_line_before_namespace' => true, + 'blank_line_after_opening_tag' => true, + 'no_blank_lines_after_phpdoc' => true, + 'phpdoc_separation' => true, + 'method_chaining_indentation' => true, + 'binary_operator_spaces' => [ + 'default' => 'single_space', + 'operators' => [ + '=>' => 'single_space', + '|' => 'single_space', + ], + ], + 'return_type_declaration' => [ + 'space_before' => 'none', + ], + 'blank_line_before_statement' => [ + 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], + ], + 'full_opening_tag' => true, + 'method_argument_space' => [ + 'on_multiline' => 'ensure_fully_multiline', + 'keep_multiple_spaces_after_comma' => true, + ], + 'yoda_style' => [ + 'always_move_variable' => true, + 'equal' => true, + 'identical' => true, + 'less_and_greater' => true, + ], + 'declare_strict_types' => true, + ], ]; diff --git a/src/Config.php b/src/Config.php index 2910834..205fcac 100644 --- a/src/Config.php +++ b/src/Config.php @@ -10,52 +10,51 @@ class Config extends \PhpCsFixer\Config implements ConfigInterface { - use Helpers; + use Helpers; - private function __construct(string $name) - { - parent::__construct($name); - } + private function __construct(string $name) + { + parent::__construct($name); + } - public static function create(Finder $finder, string $name = 'default'): self - { - $config = new self($name); + public static function create(Finder $finder, string $name = 'default'): self + { + $config = new self($name); - $config->setFinder($finder); - $config->setDefaultRules(); + $config->setFinder($finder); + $config->setDefaultRules(); - return $config; - } + return $config; + } - public function setDefaultRules(): self - { - - $this->setRules($this->configRule('rules', [])) - ->setLineEnding($this->configRule('line_ending', "\n")) - ->setRiskyAllowed($this->configRule('risky_allowed', true)) + public function setDefaultRules(): self + { + $this->setRules($this->configRule('rules', [])) + ->setLineEnding($this->configRule('line_ending', "\n")) + ->setRiskyAllowed($this->configRule('risky_allowed', true)) ->setIndent($this->configRule('set_indent', "\t")); - return $this; - } + return $this; + } - public function mergeRules(array $rules): self - { - $this->setRules(\Ckr\Util\ArrayMerger::doMerge($this->getRules(), $rules)); + public function mergeRules(array $rules): self + { + $this->setRules(\Ckr\Util\ArrayMerger::doMerge($this->getRules(), $rules)); - return $this; - } + return $this; + } - public function removeRules(array $rulesKeys): self - { - $rules = $this->getRules(); + public function removeRules(array $rulesKeys): self + { + $rules = $this->getRules(); - $this->setRules(array_diff_key($rules, array_flip($rulesKeys))); + $this->setRules(array_diff_key($rules, array_flip($rulesKeys))); - return $this; - } + return $this; + } - public function removeRule(string $ruleKey): self - { - return $this->removeRules([$ruleKey]); - } + public function removeRule(string $ruleKey): self + { + return $this->removeRules([$ruleKey]); + } } diff --git a/src/Interfaces/ConfigInterface.php b/src/Interfaces/ConfigInterface.php index 644c9ec..e24a7f2 100644 --- a/src/Interfaces/ConfigInterface.php +++ b/src/Interfaces/ConfigInterface.php @@ -8,33 +8,33 @@ interface ConfigInterface extends \PhpCsFixer\ConfigInterface { - /** - * Creates default PHP CS fixer config. Calls setDefaultSettings() - */ - public static function create(Finder $finder, string $name = 'default'): self; - - /** - * Sets rules, line ending and risky allowed - */ - public function setDefaultRules(): self; - - /** - * Recursively merges provided rules with current rules. - * Allows default rules to be overridden. - * - * @param array|bool> $rules - */ - public function mergeRules(array $rules): self; - - /** - * Unset matching rules - * - * @param array $rulesKeys - */ - public function removeRules(array $rulesKeys): self; - - /** - * Removes rule name - */ - public function removeRule(string $ruleKey): self; + /** + * Creates default PHP CS fixer config. Calls setDefaultSettings() + */ + public static function create(Finder $finder, string $name = 'default'): self; + + /** + * Sets rules, line ending and risky allowed + */ + public function setDefaultRules(): self; + + /** + * Recursively merges provided rules with current rules. + * Allows default rules to be overridden. + * + * @param array|bool> $rules + */ + public function mergeRules(array $rules): self; + + /** + * Unset matching rules + * + * @param array $rulesKeys + */ + public function removeRules(array $rulesKeys): self; + + /** + * Removes rule name + */ + public function removeRule(string $ruleKey): self; } diff --git a/src/Traits/Helpers.php b/src/Traits/Helpers.php index fc4fe68..d5813d8 100644 --- a/src/Traits/Helpers.php +++ b/src/Traits/Helpers.php @@ -8,30 +8,30 @@ trait Helpers { - /** - * Get specified configuration value. - * - * @param string $configFile filename of config file - * @param string $key key of config array - */ - public function config(string $configFile, string $key, mixed $default = null): mixed - { - $path = __DIR__ . "/../../config/{$configFile}.php"; - - Assert::fileExists($path, 'provided config file does not exist'); - - $config = require $path; - - return $config[$key] ?? $default; - } - - /** - * Get specified rule from configuration. - * - * @param string $key key of config array - */ - public function configRule(string $key, mixed $default = null): mixed - { - return $this->config('rules', $key, $default); - } + /** + * Get specified configuration value. + * + * @param string $configFile filename of config file + * @param string $key key of config array + */ + public function config(string $configFile, string $key, mixed $default = null): mixed + { + $path = __DIR__ . "/../../config/{$configFile}.php"; + + Assert::fileExists($path, 'provided config file does not exist'); + + $config = require $path; + + return $config[$key] ?? $default; + } + + /** + * Get specified rule from configuration. + * + * @param string $key key of config array + */ + public function configRule(string $key, mixed $default = null): mixed + { + return $this->config('rules', $key, $default); + } } diff --git a/tests/Feature/ConfigTest.php b/tests/Feature/ConfigTest.php index 8fcece9..df1869a 100644 --- a/tests/Feature/ConfigTest.php +++ b/tests/Feature/ConfigTest.php @@ -7,23 +7,23 @@ use Yard\PhpCsFixerRules\Interfaces\ConfigInterface; beforeEach(function () { - $this->config = Config::create(Finder::create()); + $this->config = Config::create(Finder::create()); }); it('can create config', function () { - expect($this->config)->toBeInstanceOf(ConfigInterface::class); + expect($this->config)->toBeInstanceOf(ConfigInterface::class); }); it('sets rules on create', function () { - expect($this->config->getRules())->not->toBeEmpty(); + expect($this->config->getRules())->not->toBeEmpty(); }); it('sets line ending on create', function () { - expect($this->config->getLineEnding())->not->toBeEmpty(); + expect($this->config->getLineEnding())->not->toBeEmpty(); }); it('sets risky allowed on create', function () { - expect($this->config->getRiskyAllowed())->not->toBeEmpty(); + expect($this->config->getRiskyAllowed())->not->toBeEmpty(); }); it('sets indent on create', function () { @@ -31,41 +31,41 @@ }); it('can reset to default rules using setDefaultRules()', function () { - $defaultRules = $this->config->getRules(); - $defaultLineEnding = $this->config->getLineEnding(); - $defaultRiskyAllowed = $this->config->getRiskyAllowed(); + $defaultRules = $this->config->getRules(); + $defaultLineEnding = $this->config->getLineEnding(); + $defaultRiskyAllowed = $this->config->getRiskyAllowed(); - $this->config->setRules([]) - ->setLineEnding("\t\n") - ->setRiskyAllowed(false); + $this->config->setRules([]) + ->setLineEnding("\t\n") + ->setRiskyAllowed(false); - $this->config->setDefaultRules(); + $this->config->setDefaultRules(); - expect($this->config->getRules())->toBe($defaultRules); - expect($this->config->getLineEnding())->toBe($defaultLineEnding); - expect($this->config->getRiskyAllowed())->toBe($defaultRiskyAllowed); + expect($this->config->getRules())->toBe($defaultRules); + expect($this->config->getLineEnding())->toBe($defaultLineEnding); + expect($this->config->getRiskyAllowed())->toBe($defaultRiskyAllowed); }); it('can override all rules', function () { - $this->config->setRules([]); + $this->config->setRules([]); - expect($this->config->getRules())->toBeEmpty(); + expect($this->config->getRules())->toBeEmpty(); }); it('can override line ending', function () { - $this->config->setLineEnding("\t\n"); + $this->config->setLineEnding("\t\n"); - expect($this->config->getLineEnding())->toBe("\t\n"); + expect($this->config->getLineEnding())->toBe("\t\n"); }); it('can override risky allowed', function () { - $this->config->setRiskyAllowed(false); + $this->config->setRiskyAllowed(false); - expect($this->config->getRiskyAllowed())->toBe(false); + expect($this->config->getRiskyAllowed())->toBe(false); }); it('can override indent', function () { - $this->config->setIndent(" "); + $this->config->setIndent(' '); - expect($this->config->getIndent())->toBe(" "); + expect($this->config->getIndent())->toBe(' '); }); diff --git a/tests/Unit/ConfigRulesMergeTest.php b/tests/Unit/ConfigRulesMergeTest.php index b4913d2..a94b0a8 100644 --- a/tests/Unit/ConfigRulesMergeTest.php +++ b/tests/Unit/ConfigRulesMergeTest.php @@ -7,116 +7,116 @@ use Yard\PhpCsFixerRules\Interfaces\ConfigInterface; beforeEach(function () { - $this->config = Config::create(Finder::create()); + $this->config = Config::create(Finder::create()); }); it('can create config', function () { - expect($this->config)->toBeInstanceOf(ConfigInterface::class); + expect($this->config)->toBeInstanceOf(ConfigInterface::class); }); it('can merge rules', function () { - $this->config->setRules([ // reset rules list - '@PSR2' => true, - 'indentation_type' => true, - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - ])->mergeRules([ - 'ordered_imports' => ['case_sensitive' => false], - 'array_syntax' => ['syntax' => 'short'], - 'phpdoc_scalar' => true, - ]); + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ])->mergeRules([ + 'ordered_imports' => ['case_sensitive' => false], + 'array_syntax' => ['syntax' => 'short'], + 'phpdoc_scalar' => true, + ]); - expect($this->config->getRules())->toBe([ - '@PSR2' => true, - 'indentation_type' => true, - 'ordered_imports' => [ - 'sort_algorithm' => 'alpha', - 'case_sensitive' => false, - ], - 'array_syntax' => ['syntax' => 'short'], - 'phpdoc_scalar' => true, - ]); + expect($this->config->getRules())->toBe([ + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => [ + 'sort_algorithm' => 'alpha', + 'case_sensitive' => false, + ], + 'array_syntax' => ['syntax' => 'short'], + 'phpdoc_scalar' => true, + ]); }); it('can override rules', function () { - $this->config->setRules([ // reset rules list - '@PSR2' => true, - 'indentation_type' => true, - 'ordered_imports' => ['case_sensitive' => false], - ])->mergeRules([ - 'indentation_type' => false, - 'ordered_imports' => ['case_sensitive' => true], - ]); + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['case_sensitive' => false], + ])->mergeRules([ + 'indentation_type' => false, + 'ordered_imports' => ['case_sensitive' => true], + ]); - expect($this->config->getRules())->toBe([ - '@PSR2' => true, - 'indentation_type' => false, - 'ordered_imports' => ['case_sensitive' => true], - ]); + expect($this->config->getRules())->toBe([ + '@PSR2' => true, + 'indentation_type' => false, + 'ordered_imports' => ['case_sensitive' => true], + ]); }); it('can merge and override', function () { - $this->config->setRules([ // reset rules list - '@PSR2' => true, - 'indentation_type' => true, - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - ])->mergeRules([ - '@PSR2' => false, - 'ordered_imports' => ['case_sensitive' => false], - 'phpdoc_scalar' => true, - ]); + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ])->mergeRules([ + '@PSR2' => false, + 'ordered_imports' => ['case_sensitive' => false], + 'phpdoc_scalar' => true, + ]); - expect($this->config->getRules())->toBe([ - '@PSR2' => false, - 'indentation_type' => true, - 'ordered_imports' => [ - 'sort_algorithm' => 'alpha', - 'case_sensitive' => false, - ], - 'phpdoc_scalar' => true, - ]); + expect($this->config->getRules())->toBe([ + '@PSR2' => false, + 'indentation_type' => true, + 'ordered_imports' => [ + 'sort_algorithm' => 'alpha', + 'case_sensitive' => false, + ], + 'phpdoc_scalar' => true, + ]); }); it('can merge and override deep nested arrays', function () { - $this->config->setRules([ // reset rules list - 'binary_operator_spaces' => [ - 'default' => 'single_space', - 'operators' => [ - '=>' => null, - '|' => 'no_space', - ], - ], - ])->mergeRules([ - 'binary_operator_spaces' => [ - 'default' => 'align', - 'operators' => [ - '|' => 'single_space', - '<>' => null, - ], - ], - ]); + $this->config->setRules([ // reset rules list + 'binary_operator_spaces' => [ + 'default' => 'single_space', + 'operators' => [ + '=>' => null, + '|' => 'no_space', + ], + ], + ])->mergeRules([ + 'binary_operator_spaces' => [ + 'default' => 'align', + 'operators' => [ + '|' => 'single_space', + '<>' => null, + ], + ], + ]); - expect($this->config->getRules())->toBe([ - 'binary_operator_spaces' => [ - 'default' => 'align', - 'operators' => [ - '=>' => null, - '|' => 'single_space', - '<>' => null, - ], - ], - ]); + expect($this->config->getRules())->toBe([ + 'binary_operator_spaces' => [ + 'default' => 'align', + 'operators' => [ + '=>' => null, + '|' => 'single_space', + '<>' => null, + ], + ], + ]); }); it('can merge empty arrays', function () { - $this->config->setRules([ // reset rules list - '@PSR2' => true, - 'indentation_type' => true, - 'ordered_imports' => ['case_sensitive' => false], - ])->mergeRules([]); + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['case_sensitive' => false], + ])->mergeRules([]); - expect($this->config->getRules())->toBe([ - '@PSR2' => true, - 'indentation_type' => true, - 'ordered_imports' => ['case_sensitive' => false], - ]); + expect($this->config->getRules())->toBe([ + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['case_sensitive' => false], + ]); }); diff --git a/tests/Unit/ConfigRulesRemoveTest.php b/tests/Unit/ConfigRulesRemoveTest.php index e37d09f..c5d8f11 100644 --- a/tests/Unit/ConfigRulesRemoveTest.php +++ b/tests/Unit/ConfigRulesRemoveTest.php @@ -6,59 +6,59 @@ use Yard\PhpCsFixerRules\Config; beforeEach(function () { - $this->config = Config::create(Finder::create()); + $this->config = Config::create(Finder::create()); }); it('can remove rules', function () { - $this->config->setRules([ // reset rules list - '@PSR2' => true, - 'indentation_type' => true, - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - ])->removeRules([ - "@PSR2", "ordered_imports", - ]); + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ])->removeRules([ + '@PSR2', 'ordered_imports', + ]); - expect($this->config->getRules())->toBe([ - 'indentation_type' => true, - ]); + expect($this->config->getRules())->toBe([ + 'indentation_type' => true, + ]); }); it('can handle an request to remove nothing', function () { - $this->config->setRules([ // reset rules list - '@PSR2' => true, - 'indentation_type' => true, - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - ])->removeRules([]); + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ])->removeRules([]); - expect($this->config->getRules())->toBe([ - '@PSR2' => true, - 'indentation_type' => true, - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - ]); + expect($this->config->getRules())->toBe([ + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ]); }); it('can remove one rule', function () { - $this->config->setRules([ // reset rules list - '@PSR2' => true, - 'indentation_type' => true, - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - ])->removeRule("@PSR2"); + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ])->removeRule('@PSR2'); - expect($this->config->getRules())->toBe([ - 'indentation_type' => true, - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - ]); + expect($this->config->getRules())->toBe([ + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ]); }); it('can remove a list with one rule', function () { - $this->config->setRules([ // reset rules list - '@PSR2' => true, - 'indentation_type' => true, - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - ])->removeRules(["@PSR2"]); + $this->config->setRules([ // reset rules list + '@PSR2' => true, + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ])->removeRules(['@PSR2']); - expect($this->config->getRules())->toBe([ - 'indentation_type' => true, - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - ]); + expect($this->config->getRules())->toBe([ + 'indentation_type' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + ]); });