Skip to content

Move to PHP-CS-Fixer #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.php_cs export-ignore
.travis.yml export-ignore
phpcs.xml.dist export-ignore
phpunit.xml export-ignore
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
vendor/
phpcs.xml
.php_cs.cache
composer.lock
tests/input2
phpcs.log
9 changes: 9 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$config = new Doctrine\CodingStandard\Config();
$config->getFinder()
->in(__DIR__ . '/lib')
->in(__DIR__ . '/tests')
;

return $config;
14 changes: 2 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
dist: trusty
sudo: false
language: php

Expand All @@ -12,21 +11,12 @@ cache:
- $HOME/.composer/cache

before_install:
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available"
- composer self-update
- phpenv config-rm xdebug.ini || true

install: travis_retry composer update --prefer-dist

script:
- vendor/bin/phpcs
- vendor/bin/phpcs tests/input --report=summary --report-file=phpcs.log; diff tests/expected_report.txt phpcs.log
script: vendor/bin/phpunit

jobs:
allow_failures:
- php: nightly

include:
- stage: Apply fixes
before_script:
- cp -R tests/input/ tests/input2/
script: vendor/bin/phpcbf tests/input2; diff tests/input2 tests/fixed
68 changes: 16 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,86 +6,50 @@ Doctrine Coding Standard
[![Latest Stable Version](https://img.shields.io/packagist/v/doctrine/coding-standard.svg?style=flat-square)](https://packagist.org/packages/doctrine/coding-standard)


The [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) ruleset to check that
The [PHP-CS-Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer) ruleset to check that
repositories are following the standards defined by the Doctrine team.

Standards
---------

Doctrine Coding Standard is based on [PSR-1](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md)
and [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md), with some noticeable
exceptions/differences/extensions (:white_check_mark: are the implemented sniffs):

- Keep the nesting of control structures per method as small as possible
- Prefer early exit over nesting conditions or using else
- :white_check_mark: Align equals (`=`) signs in assignments
- :white_check_mark: Add spaces around a concatenation operator `$foo = 'Hello ' . 'World!';`
- :white_check_mark: Add spaces between assignment, control and return statements
- :white_check_mark: Add spaces after a negation operator `if (! $cond)`
- :white_check_mark: Add spaces around a colon in return type declaration `function () : void {}`
- :white_check_mark: Add spaces after a type cast `$foo = (int) '12345';`
- :white_check_mark: Use apostrophes for enclosing strings
- :white_check_mark: Always use strict comparisons
- :white_check_mark: Always add `declare(strict_types=1)` at the beginning of a file
- :white_check_mark: Always add native types where possible
- :white_check_mark: Omit phpDoc for parameters/returns with native types, unless adding description
- :white_check_mark: Don't use `@author`, `@since` and similar annotations that duplicate Git information
- :white_check_mark: Assignment in condition is not allowed

For full reference of enforcements, go through `lib/Doctrine/ruleset.xml` where each sniff is briefly described.
@TODO

Installation
------------

You have two possibilities to use the Doctrine Coding Standard with PHP_CodeSniffer in a particular project.

### 1. As a composer dependency of your project

You can install the Doctrine Coding Standard as a composer dependency to your particular project.
Just add the following block to your project's `composer.json` file:

```bash
$ php composer require doctrine/coding-standard:~1.0
```

Then you can use it like:

```bash
$ ./vendor/bin/phpcs --standard=Doctrine /path/to/some/file/to/sniff.php
```

You might also do automatic fixes using `phpcbf`:

```bash
$ ./vendor/bin/phpcbf --standard=Doctrine /path/to/some/file/to/sniff.php
$ composer require doctrine/coding-standard
```

### 2. Global installation
Then add the `.php_cs` to your project:

You can also install the Doctrine Coding Standard globally:
```php
<?php

```bash
$ composer global require doctrine/coding-standard:~1.0
```

Then you can use it like:
$config = new Doctrine\CodingStandard\Config();
$config->getFinder()
->in(__DIR__ . '/lib')
->in(__DIR__ . '/tests')
;

```bash
$ phpcs --standard=Doctrine /path/to/some/file/to/sniff.php
return $config;
```

You might also do automatic fixes using `phpcbf`:
And finally run PHP-CS-Fixer:

```bash
$ phpcbf --standard=Doctrine /path/to/some/file/to/sniff.php
$ vendor/bin/php-cs-fixer fix -v
```

Testing
-------

If you are contributing to the Doctrine Coding Standard and want to test your contribution, you just
need to execute PHPCS with the tests folder and ensure it matches the expected report:
need to execute PHP-CS-Fixer:

```bash
$ ./vendor/bin/phpcs tests/input --report=summary --report-file=phpcs.log; diff tests/expected_report.txt phpcs.log
$ vendor/bin/php-cs-fixer fix -v --dry-run --diff
```
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "doctrine/coding-standard",
"type": "phpcodesniffer-standard",
"description": "Doctrine Coding Standard",
"keywords": ["doctrine", "coding", "standard", "cs", "code", "style", "sniffer"],
"homepage": "http://www.doctrine-project.org",
Expand All @@ -11,14 +10,20 @@
],
"autoload": {
"psr-4": {
"Doctrine\\Sniffs\\": "lib/Doctrine/Sniffs"
"Doctrine\\CodingStandard\\": "lib/"
}
},
"autoload-dev": {
"psr-4": {
"Doctrine\\CodingStandard\\Tests\\": "tests/"
}
},
"require": {
"php": "^7.1",
"squizlabs/php_codesniffer": "^3.0.1",
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.2",
"slevomat/coding-standard": "^4.0"
"friendsofphp/php-cs-fixer": "^2.9"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
},
"extra": {
"branch-alias": {
Expand Down
89 changes: 89 additions & 0 deletions lib/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace Doctrine\CodingStandard;

use PhpCsFixer\Config as PhpCsFixerConfig;

final class Config extends PhpCsFixerConfig
{
public function __construct()
{
parent::__construct(__NAMESPACE__);

$this->setRiskyAllowed(true);
$this->setRules([
'@DoctrineAnnotation' => true,
'@PHP71Migration' => true,
'@PHP71Migration:risky' => true,
'@PHPUnit60Migration:risky' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'align_multiline_comment' => ['comment_type' => 'all_multiline'],
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => false,
'blank_line_before_return' => false,
'blank_line_before_statement' => true,
'class_definition' => ['singleItemSingleLine' => true],
'class_keyword_remove' => false,
'combine_consecutive_issets' => false,
'combine_consecutive_unsets' => false,
'compact_nullable_typehint' => true,
'concat_space' => ['spacing' => 'one'],
'doctrine_annotation_array_assignment' => true,
'doctrine_annotation_spaces' => true,
'encoding' => true,
'escape_implicit_backslashes' => true,
'explicit_indirect_variable' => true,
'explicit_string_variable' => true,
'final_internal_class' => false,
'general_phpdoc_annotation_remove' => false,
'hash_to_slash_comment' => false,
'header_comment' => false,
'heredoc_to_nowdoc' => true,
'linebreak_after_opening_tag' => true,
'list_syntax' => true,
'mb_str_functions' => false,
'method_argument_space' => ['keep_multiple_spaces_after_comma' => true],
'method_chaining_indentation' => true,
'method_separation' => false,
'native_function_invocation' => false,
'no_blank_lines_before_namespace' => false,
'no_extra_consecutive_blank_lines' => ['tokens' => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'useTrait', 'curly_brace_block', 'parenthesis_brace_block', 'square_brace_block']],
'no_multiline_whitespace_around_double_arrow' => false,
'no_multiline_whitespace_before_semicolons' => false,
'no_null_property_initialization' => true,
'no_php4_constructor' => true,
'no_short_echo_tag' => true,
'no_superfluous_elseif' => true,
'no_unneeded_control_parentheses' => true,
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'not_operator_with_space' => false,
'not_operator_with_successor_space' => true,
'ordered_class_elements' => ['order' => ['use_trait', 'constant_public', 'constant_protected', 'constant_private', 'property', 'construct', 'destruct', 'magic', 'phpunit', 'method']],
'ordered_imports' => true,
'php_unit_strict' => false,
'php_unit_test_annotation' => true,
'php_unit_test_class_requires_covers' => false,
'phpdoc_add_missing_param_annotation' => false,
'phpdoc_order' => true,
'phpdoc_types_order' => true,
'pow_to_exponentiation' => false,
'pre_increment' => false,
'psr0' => true,
'random_api_migration' => true,
'silenced_deprecation_error' => false,
'simplified_null_return' => true,
'single_line_comment_style' => true,
'static_lambda' => false,
'strict_comparison' => false,
'strict_param' => true,
'unary_operator_spaces' => false,
'void_return' => false,
'yoda_style' => false,
]);
}
}
15 changes: 0 additions & 15 deletions phpcs.xml.dist

This file was deleted.

20 changes: 20 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<phpunit
bootstrap="./vendor/autoload.php"
backupGlobals="false"
colors="true"
verbose="true">

<php>
<env name="PHP_CS_FIXER_FUTURE_MODE" value="1"/>
</php>

<testsuite name="Doctrine\CodingStandard">
<directory>./tests</directory>
</testsuite>

<filter>
<whitelist>
<directory suffix=".php">./lib</directory>
</whitelist>
</filter>
</phpunit>
49 changes: 49 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Doctrine\CodingStandard\Tests;

use Doctrine\CodingStandard\Config;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\FixerFactory;
use PhpCsFixer\RuleSet;
use PHPUnit\Framework\TestCase;

final class ConfigTest extends TestCase
{
public function testAllDefaultRulesAreSpecified()
{
$config = new Config();
$configRules = $config->getRules();
$ruleSet = new RuleSet($configRules);
$rules = $ruleSet->getRules();
// RuleSet strips all disabled rules
foreach ($configRules as $name => $value) {
if ('@' === $name[0]) {
continue;
}
$rules[$name] = $value;
}

$currentRules = array_keys($rules);

$fixerFactory = new FixerFactory();
$fixerFactory->registerBuiltInFixers();
$fixerFactory->registerCustomFixers($config->getCustomFixers());
$fixers = $fixerFactory->getFixers();

$availableRules = array_map(function (FixerInterface $fixer) {
return $fixer->getName();
}, $fixers);
sort($availableRules);

$diff = array_diff($availableRules, $currentRules);
$this->assertEmpty($diff, sprintf("Mancano tra le specifiche i seguenti fixer:\n- %s", implode(PHP_EOL . '- ', $diff)));

$currentRules = array_keys($configRules);
$orderedCurrentRules = $currentRules;
sort($orderedCurrentRules);
$this->assertEquals($orderedCurrentRules, $currentRules, 'Order the rules alphabetically please');
}
}
17 changes: 0 additions & 17 deletions tests/expected_report.txt

This file was deleted.

Loading