diff --git a/.gitignore b/.gitignore index b4b3c59..e009b3f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -/vendor/ -.php_cs.cache \ No newline at end of file +vendor +.php_cs.cache +composer.lock +.idea diff --git a/.php_cs.dist b/.php_cs.dist index e91688b..0cc361d 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -2,9 +2,7 @@ require 'vendor/autoload.php'; -$finder = PhpCsFixer\Finder::create() - ->in(__DIR__); - -return K10r\Codestyle\PHP71::create() - ->setUsingCache(true) - ->setFinder($finder); \ No newline at end of file +return \K10r\Codestyle\PHP72::create( + \PhpCsFixer\Finder::create() + ->in(__DIR__) +); diff --git a/README.md b/README.md index 4b78d4f..af69614 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,21 @@ # Usage -First require the package via composer +## Installation +First require the package via composer: `composer require --dev k10r/codestyle` -After that, decide which configuration you want to use, currenty the following configurations are supported: +## Choose a version +After that, decide which configuration you want to use, currently the following configurations are supported: - PHP 5.6 - PHP 7.0 - PHP 7.1 - PHP 7.2 +- PHP 7.3 -Add a `.php_cs.dist` to the root folder of your project with the required chosen configuration applied. A good example can be found in this project root folder. - -Finally add `.php_cs.cache` to project specific .gitignore file and run php-cs-fixer with the following command: `vendor/bin/php-cs-fixer.phar fix -vvv` \ No newline at end of file +## Implementation +- Create a `.php_cs.dist` or add the one provided by the project to the root folder of your project. +- Change the version to one of the listed above +- Finally, add `.php_cs.cache` to project specific `.gitignore` file +- If you want to apply the changes simply use: `vendor/bin/php-cs-fixer.phar fix` +- Use the following command to take a look at the changes before applying them: `vendor/bin/php-cs-fixer.phar fix --dry-run` diff --git a/composer.json b/composer.json index ea4920d..0cc1301 100644 --- a/composer.json +++ b/composer.json @@ -1,21 +1,27 @@ { - "name": "k10r/codestyle", - "description": "Kellerkinder codestyle definitions for different PHP versions.", - "type": "library", - "license": "MIT", - "authors": [ - { - "name": "Kellerkinder / k10r GmbH", - "email": "anfragen@kellerkinder.de" - } - ], - "autoload": { - "psr-4": { - "K10r\\Codestyle\\": "src" - } - }, - "bin": [ - "php-cs-fixer.phar", - "php-cs-fixer" - ] + "name": "k10r/codestyle", + "description": "Kellerkinder codestyle definitions for different PHP versions.", + "type": "library", + "license": "MIT", + "keywords": [ + "fixer", + "php-cs-fixer", + "custom-fixer", + "k10r" + ], + "authors": [ + { + "name": "Kellerkinder GmbH", + "email": "anfragen@kellerkinder.de" + } + ], + "autoload": { + "psr-4": { + "K10r\\Codestyle\\": "src/" + } + }, + "bin": [ + "php-cs-fixer.phar", + "php-cs-fixer" + ] } diff --git a/php-cs-fixer b/php-cs-fixer old mode 100644 new mode 100755 index 7eb7d16..638b218 Binary files a/php-cs-fixer and b/php-cs-fixer differ diff --git a/php-cs-fixer.phar b/php-cs-fixer.phar old mode 100644 new mode 100755 index 7eb7d16..638b218 Binary files a/php-cs-fixer.phar and b/php-cs-fixer.phar differ diff --git a/src/CustomFixer.php b/src/CustomFixer.php new file mode 100644 index 0000000..81c8f64 --- /dev/null +++ b/src/CustomFixer.php @@ -0,0 +1,18 @@ +<?php + +namespace K10r\Codestyle; + +use K10r\Codestyle\Fixer\AutomaticCommentsFixer; +use K10r\Codestyle\Fixer\MultiToSingleLineAnnotationFixer; + +final class CustomFixer +{ + /** @return array */ + public static function getCustomFixer() + { + return [ + new AutomaticCommentsFixer(), + new MultiToSingleLineAnnotationFixer(), + ]; + } +} diff --git a/src/DefaultRules.php b/src/DefaultRules.php new file mode 100644 index 0000000..798d30e --- /dev/null +++ b/src/DefaultRules.php @@ -0,0 +1,87 @@ +<?php + +namespace K10r\Codestyle; + +use PhpCsFixer\Config; +use PhpCsFixer\Finder; + +abstract class DefaultRules +{ + const RULES = [ + '@PSR2' => true, + '@Symfony' => true, + '@DoctrineAnnotation' => true, + 'no_useless_else' => true, + 'no_useless_return' => true, + 'ordered_class_elements' => true, + 'ordered_imports' => true, + 'phpdoc_order' => true, + 'Kellerkinder/single_line_annotation' => true, + 'Kellerkinder/automatic_comments' => true, + 'phpdoc_summary' => false, + 'phpdoc_types_order' => true, + 'return_assignment' => true, + 'phpdoc_align' => true, + 'phpdoc_to_comment' => false, + 'yoda_style' => false, + 'phpdoc_var_without_name' => false, + 'no_multiline_whitespace_before_semicolons' => true, + 'no_unused_imports' => true, + 'no_superfluous_phpdoc_tags' => true, + 'concat_space' => [ + 'spacing' => 'one', + ], + 'blank_line_before_statement' => [ + 'statements' => [ + 'break', + 'continue', + 'do', + 'die', + 'exit', + 'if', + 'return', + 'switch', + 'try', + 'yield', + ], + ], + 'array_syntax' => [ + 'syntax' => 'short', + ], + 'binary_operator_spaces' => [ + 'operators' => [ + '=>' => 'align_single_space_minimal', + '=' => 'align_single_space_minimal', + ], + ], + ]; + + public static function getRules() + { + return self::RULES; + } + + /** + * @param array $additionalRules + * @param bool $usingCache + * + * @return Config + */ + public static function create(Finder $finder = null, $additionalRules = [], $usingCache = true) + { + $newConfig = Config::create(); + + if ($finder) { + $newConfig->setFinder($finder); + } + + return $newConfig->setUsingCache($usingCache) + ->setRules( + array_merge( + self::getRules(), + $additionalRules + ) + ) + ->registerCustomFixers(CustomFixer::getCustomFixer()); + } +} diff --git a/src/Fixers/Comment/AutomaticCommentsFixer.php b/src/Fixer/AutomaticCommentsFixer.php similarity index 98% rename from src/Fixers/Comment/AutomaticCommentsFixer.php rename to src/Fixer/AutomaticCommentsFixer.php index 9b6eb21..1d333f4 100644 --- a/src/Fixers/Comment/AutomaticCommentsFixer.php +++ b/src/Fixer/AutomaticCommentsFixer.php @@ -1,6 +1,6 @@ <?php -namespace K10r\Codestyle\Fixers\Comment; +namespace K10r\Codestyle\Fixer; use PhpCsFixer\AbstractFixer; use PhpCsFixer\FixerDefinition\CodeSample; diff --git a/src/Fixers/Comment/MultiToSingleLineAnnotationFixer.php b/src/Fixer/MultiToSingleLineAnnotationFixer.php similarity index 97% rename from src/Fixers/Comment/MultiToSingleLineAnnotationFixer.php rename to src/Fixer/MultiToSingleLineAnnotationFixer.php index 6bf9301..b6a9ec0 100644 --- a/src/Fixers/Comment/MultiToSingleLineAnnotationFixer.php +++ b/src/Fixer/MultiToSingleLineAnnotationFixer.php @@ -1,6 +1,6 @@ <?php -namespace K10r\Codestyle\Fixers\Comment; +namespace K10r\Codestyle\Fixer; use PhpCsFixer\AbstractFixer; use PhpCsFixer\FixerDefinition\CodeSample; diff --git a/src/PHP56.php b/src/PHP56.php index aead036..5ea7052 100644 --- a/src/PHP56.php +++ b/src/PHP56.php @@ -2,65 +2,13 @@ namespace K10r\Codestyle; -use K10r\Codestyle\Fixers\Comment\AutomaticCommentsFixer; -use K10r\Codestyle\Fixers\Comment\MultiToSingleLineAnnotationFixer; -use PhpCsFixer\Config; - -final class PHP56 extends Config +final class PHP56 extends DefaultRules { - public function __construct() - { - parent::__construct('Kellerkinder PHP 5.6 config'); - } - - public static function create() - { - $factory = parent::create(); - - $factory->registerCustomFixers([ - new AutomaticCommentsFixer(), - new MultiToSingleLineAnnotationFixer(), - ]); - - return $factory; - } - /** * @return array */ - public function getRules() + public static function getRules() { - return [ - '@PSR2' => true, - '@Symfony' => true, - '@DoctrineAnnotation' => true, - 'no_useless_else' => true, - 'no_useless_return' => true, - 'ordered_class_elements' => true, - 'ordered_imports' => true, - 'phpdoc_order' => true, - 'Kellerkinder/single_line_annotation' => true, - 'Kellerkinder/automatic_comments' => true, - 'phpdoc_summary' => false, - 'phpdoc_types_order' => true, - 'return_assignment' => true, - 'phpdoc_align' => true, - 'phpdoc_to_comment' => false, - 'yoda_style' => false, - 'phpdoc_var_without_name' => false, - 'no_multiline_whitespace_before_semicolons' => true, - 'concat_space' => [ - 'spacing' => 'one', - ], - 'array_syntax' => [ - 'syntax' => 'short', - ], - 'binary_operator_spaces' => [ - 'operators' => [ - '=>' => 'align_single_space_minimal', - '=' => 'align_single_space_minimal', - ], - ], - ]; + return self::RULES; } } diff --git a/src/PHP70.php b/src/PHP70.php index 8b81b7c..53c6112 100644 --- a/src/PHP70.php +++ b/src/PHP70.php @@ -2,65 +2,20 @@ namespace K10r\Codestyle; -use K10r\Codestyle\Fixers\Comment\AutomaticCommentsFixer; -use K10r\Codestyle\Fixers\Comment\MultiToSingleLineAnnotationFixer; -use PhpCsFixer\Config; - -final class PHP70 extends Config +final class PHP70 extends DefaultRules { - public function __construct() - { - parent::__construct('Kellerkinder PHP 7.0 config'); - } - - public static function create() - { - $factory = parent::create(); - - $factory->registerCustomFixers([ - new AutomaticCommentsFixer(), - new MultiToSingleLineAnnotationFixer(), - ]); - - return $factory; - } - /** * @return array */ - public function getRules() + public static function getRules() { - return [ - '@PSR2' => true, - '@Symfony' => true, - '@DoctrineAnnotation' => true, - 'no_useless_else' => true, - 'no_useless_return' => true, - 'ordered_class_elements' => true, - 'ordered_imports' => true, - 'phpdoc_order' => true, - 'Kellerkinder/single_line_annotation' => true, - 'Kellerkinder/automatic_comments' => true, - 'phpdoc_summary' => false, - 'phpdoc_types_order' => true, - 'return_assignment' => true, - 'phpdoc_align' => true, - 'phpdoc_to_comment' => false, - 'yoda_style' => false, - 'phpdoc_var_without_name' => false, - 'no_multiline_whitespace_before_semicolons' => true, - 'concat_space' => [ - 'spacing' => 'one', - ], - 'array_syntax' => [ - 'syntax' => 'short', - ], - 'binary_operator_spaces' => [ - 'operators' => [ - '=>' => 'align_single_space_minimal', - '=' => 'align_single_space_minimal', - ], - ], - ]; + return array_merge( + PHP56::RULES, + [ + 'phpdoc_to_return_type' => true, + 'declare_strict_types' => true, + 'ternary_to_null_coalescing' => true, + ] + ); } } diff --git a/src/PHP71.php b/src/PHP71.php index ce906e9..bb5f86a 100644 --- a/src/PHP71.php +++ b/src/PHP71.php @@ -2,71 +2,24 @@ namespace K10r\Codestyle; -use K10r\Codestyle\Fixers\Comment\AutomaticCommentsFixer; -use K10r\Codestyle\Fixers\Comment\MultiToSingleLineAnnotationFixer; -use PhpCsFixer\Config; - -final class PHP71 extends Config +final class PHP71 extends DefaultRules { - public function __construct() - { - parent::__construct('Kellerkinder PHP 7.1 config'); - } - - public static function create() - { - $factory = parent::create(); - - $factory->registerCustomFixers([ - new AutomaticCommentsFixer(), - new MultiToSingleLineAnnotationFixer(), - ]); - - return $factory; - } - /** * @return array */ - public function getRules() + public static function getRules() { - return [ - '@PSR2' => true, - '@Symfony' => true, - '@DoctrineAnnotation' => true, - 'no_useless_else' => true, - 'no_useless_return' => true, - 'ordered_class_elements' => true, - 'ordered_imports' => true, - 'phpdoc_order' => true, - 'Kellerkinder/single_line_annotation' => true, - 'Kellerkinder/automatic_comments' => true, - 'ternary_to_null_coalescing' => true, - 'phpdoc_summary' => false, - 'phpdoc_to_comment' => false, - 'phpdoc_types_order' => true, - 'return_assignment' => true, - 'phpdoc_align' => true, - 'yoda_style' => false, - 'phpdoc_var_without_name' => false, - 'no_multiline_whitespace_before_semicolons' => true, - 'concat_space' => [ - 'spacing' => 'one', - ], - 'array_syntax' => [ - 'syntax' => 'short', - ], - 'visibility_required' => [ - 'const', - 'property', - 'method', - ], - 'binary_operator_spaces' => [ - 'operators' => [ - '=>' => 'align_single_space_minimal', - '=' => 'align_single_space_minimal', + return array_merge( + PHP70::getRules(), + [ + 'ternary_to_null_coalescing' => true, + 'void_return' => true, + 'visibility_required' => [ + 'const', + 'property', + 'method', ], - ], - ]; + ] + ); } } diff --git a/src/PHP72.php b/src/PHP72.php index 83e217e..03fcf4a 100644 --- a/src/PHP72.php +++ b/src/PHP72.php @@ -2,71 +2,13 @@ namespace K10r\Codestyle; -use K10r\Codestyle\Fixers\Comment\AutomaticCommentsFixer; -use K10r\Codestyle\Fixers\Comment\MultiToSingleLineAnnotationFixer; -use PhpCsFixer\Config; - -final class PHP72 extends Config +final class PHP72 extends DefaultRules { - public function __construct() - { - parent::__construct('Kellerkinder PHP 7.2 config'); - } - - public static function create() - { - $factory = parent::create(); - - $factory->registerCustomFixers([ - new AutomaticCommentsFixer(), - new MultiToSingleLineAnnotationFixer(), - ]); - - return $factory; - } - /** * @return array */ - public function getRules() + public static function getRules() { - return [ - '@PSR2' => true, - '@Symfony' => true, - '@DoctrineAnnotation' => true, - 'no_useless_else' => true, - 'no_useless_return' => true, - 'ordered_class_elements' => true, - 'ordered_imports' => true, - 'phpdoc_order' => true, - 'Kellerkinder/single_line_annotation' => true, - 'Kellerkinder/automatic_comments' => true, - 'ternary_to_null_coalescing' => true, - 'phpdoc_summary' => false, - 'phpdoc_to_comment' => false, - 'phpdoc_types_order' => true, - 'return_assignment' => true, - 'phpdoc_align' => true, - 'yoda_style' => false, - 'phpdoc_var_without_name' => false, - 'no_multiline_whitespace_before_semicolons' => true, - 'concat_space' => [ - 'spacing' => 'one', - ], - 'array_syntax' => [ - 'syntax' => 'short', - ], - 'visibility_required' => [ - 'const', - 'property', - 'method', - ], - 'binary_operator_spaces' => [ - 'operators' => [ - '=>' => 'align_single_space_minimal', - '=' => 'align_single_space_minimal', - ], - ], - ]; + return PHP71::getRules(); } } diff --git a/src/PHP73.php b/src/PHP73.php new file mode 100644 index 0000000..41a2778 --- /dev/null +++ b/src/PHP73.php @@ -0,0 +1,14 @@ +<?php + +namespace K10r\Codestyle; + +final class PHP73 extends DefaultRules +{ + /** + * @return array + */ + public static function getRules() + { + return PHP72::getRules(); + } +}