Skip to content

Commit

Permalink
[noticket] Optimization for customization (#14)
Browse files Browse the repository at this point in the history
[noticket] Optimization for customization
  • Loading branch information
kleinmann authored Oct 29, 2019
2 parents c7744a4 + 72b32ba commit c2df819
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 265 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/vendor/
.php_cs.cache
vendor
.php_cs.cache
composer.lock
.idea
10 changes: 4 additions & 6 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

require 'vendor/autoload.php';

$finder = PhpCsFixer\Finder::create()
->in(__DIR__);

return K10r\Codestyle\PHP71::create()
->setUsingCache(true)
->setFinder($finder);
return \K10r\Codestyle\PHP72::create(
\PhpCsFixer\Finder::create()
->in(__DIR__)
);
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`
## 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`
44 changes: 25 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
"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": "[email protected]"
}
],
"autoload": {
"psr-4": {
"K10r\\Codestyle\\": "src/"
}
},
"bin": [
"php-cs-fixer.phar",
"php-cs-fixer"
]
}
Binary file modified php-cs-fixer
100644 → 100755
Binary file not shown.
Binary file modified php-cs-fixer.phar
100644 → 100755
Binary file not shown.
18 changes: 18 additions & 0 deletions src/CustomFixer.php
Original file line number Diff line number Diff line change
@@ -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(),
];
}
}
87 changes: 87 additions & 0 deletions src/DefaultRules.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace K10r\Codestyle\Fixers\Comment;
namespace K10r\Codestyle\Fixer;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace K10r\Codestyle\Fixers\Comment;
namespace K10r\Codestyle\Fixer;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
Expand Down
58 changes: 3 additions & 55 deletions src/PHP56.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
65 changes: 10 additions & 55 deletions src/PHP70.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
);
}
}
Loading

0 comments on commit c2df819

Please sign in to comment.