From e76a9f041fbb359b38d2a519216c7eac525bb07a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Sun, 20 Aug 2017 01:05:31 +0200 Subject: [PATCH 1/7] Remove old sniff Since we have a better option in PHPCS v3 --- Docs/README.md | 38 ------------- Docs/Strings/ConcatenationSpacingSniff.xml | 29 ---------- Sniffs/Strings/ConcatenationSpacingSniff.php | 50 ----------------- .../Strings/ConcatenationSpacingUnitTest.inc | 34 ------------ .../Strings/ConcatenationSpacingUnitTest.php | 53 ------------------- 5 files changed, 204 deletions(-) delete mode 100644 Docs/README.md delete mode 100644 Docs/Strings/ConcatenationSpacingSniff.xml delete mode 100644 Sniffs/Strings/ConcatenationSpacingSniff.php delete mode 100644 Tests/Strings/ConcatenationSpacingUnitTest.inc delete mode 100644 Tests/Strings/ConcatenationSpacingUnitTest.php diff --git a/Docs/README.md b/Docs/README.md deleted file mode 100644 index 5b652002..00000000 --- a/Docs/README.md +++ /dev/null @@ -1,38 +0,0 @@ -Doctrine Coding Standard -======================== - -The Doctrine project follows a coding standard based on -[PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) with custom -additions and modifications. -When contributing to the Doctrine project, the following rules have to be met in order to fulfill this standard. - -Formatting ----------- - -- Corresponding assignment statement tokens MUST be aligned. Assignment tokens are: - `=`, `&=`, `.=`, `/=`, `-=`, `%=`, `*=`, `+=`, `^=`. - Each `=` token MUST be on the same column as the one from the previous corresponding statement. - -```php -$foo = 'Foo'; -$fooBar = 'FooBar'; -$fooBarBaz = 'FooBarBaz'; -$foo .= $bar; - -$foo = 'Foo'; - -$object->foo($foo); - -$fooBar = 'FooBar'; - -$object->fooBar($fooBar); - -$fooBarBaz = 'FoobarBaz'; - -$object->fooBarBaz($fooBarBaz); -``` - -Strings -------- - -- The string concatenation token `.` MUST be surrounded by spaces. diff --git a/Docs/Strings/ConcatenationSpacingSniff.xml b/Docs/Strings/ConcatenationSpacingSniff.xml deleted file mode 100644 index d6065243..00000000 --- a/Docs/Strings/ConcatenationSpacingSniff.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - . 'World!'; -]]> - - - '. 'World!'; -]]> - - - .'World!'; -]]> - - - '.'World!'; -]]> - - - diff --git a/Sniffs/Strings/ConcatenationSpacingSniff.php b/Sniffs/Strings/ConcatenationSpacingSniff.php deleted file mode 100644 index 9aa51808..00000000 --- a/Sniffs/Strings/ConcatenationSpacingSniff.php +++ /dev/null @@ -1,50 +0,0 @@ -. -*/ - -/** - * Sniff that verifies that there are spaces around the string concatenation operator ".". - * - * @author Steve Müller - */ -class Doctrine_Sniffs_Strings_ConcatenationSpacingSniff implements PHP_CodeSniffer_Sniff -{ - /** - * {@inheritdoc} - */ - public function register() - { - return array(T_STRING_CONCAT); - } - - /** - * {@inheritdoc} - */ - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) - { - $tokens = $phpcsFile->getTokens(); - - if (T_WHITESPACE !== $tokens[$stackPtr - 1]['code']) { - $phpcsFile->addError('Missing space before the string concatenation operator ".".', $stackPtr, 'Before'); - } - - if (T_WHITESPACE !== $tokens[$stackPtr + 1]['code']) { - $phpcsFile->addError('Missing space after the string concatenation operator ".".', $stackPtr, 'After'); - } - } -} diff --git a/Tests/Strings/ConcatenationSpacingUnitTest.inc b/Tests/Strings/ConcatenationSpacingUnitTest.inc deleted file mode 100644 index 05f4ea92..00000000 --- a/Tests/Strings/ConcatenationSpacingUnitTest.inc +++ /dev/null @@ -1,34 +0,0 @@ -. -*/ - -class Doctrine_Tests_Strings_ConcatenationSpacingUnitTest extends AbstractSniffUnitTest -{ - /** - * {@inheritdoc} - */ - public function getErrorList() - { - return array( - 4 => 1, - 5 => 1, - 6 => 2, - 11 => 1, - 12 => 1, - 13 => 2, - 16 => 1, - 17 => 1, - 18 => 2, - 21 => 1, - 22 => 1, - 23 => 2, - 25 => 4, - 31 => 1, - 34 => 1, - ); - } - - /** - * {@inheritdoc} - */ - public function getWarningList() - { - return array(); - } -} From da97bc37c71aa40953140718ecb7d041301e07d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Sun, 20 Aug 2017 01:08:44 +0200 Subject: [PATCH 2/7] Remove PHPUnit since it's not really necessary --- .gitattributes | 2 - .gitignore | 1 - composer.json | 4 -- phpunit.xml.dist | 18 -------- tests_bootstrap.php | 101 -------------------------------------------- 5 files changed, 126 deletions(-) delete mode 100644 phpunit.xml.dist delete mode 100644 tests_bootstrap.php diff --git a/.gitattributes b/.gitattributes index bb847623..f13d4d91 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,5 +2,3 @@ .gitattributes export-ignore .gitignore export-ignore .travis.yml export-ignore -phpunit.xml.dist export-ignore -tests_bootstrap.php export-ignore diff --git a/.gitignore b/.gitignore index 2042d62e..d1502b08 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ vendor/ -*.phpunit.xml composer.lock diff --git a/composer.json b/composer.json index e1f3dd5d..28a6f005 100644 --- a/composer.json +++ b/composer.json @@ -13,10 +13,6 @@ "php": ">=5.3", "squizlabs/php_codesniffer": "~1.0" }, - "require-dev": { - "phpunit/phpunit": "~4.7" - }, - "target-dir": "Doctrine", "extra": { "branch-alias": { "dev-master": "0.1-dev" diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index b33adf29..00000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,18 +0,0 @@ - - - - - ./vendor/squizlabs/php_codesniffer/tests/Standards/AllSniffs.php - - - diff --git a/tests_bootstrap.php b/tests_bootstrap.php deleted file mode 100644 index b99fed24..00000000 --- a/tests_bootstrap.php +++ /dev/null @@ -1,101 +0,0 @@ -. -*/ - -/** - * Copies a directory recursively. - * - * @param string $source The source path to copy. - * @param string $target The target path to copy to. - */ -function copyDirectory($source, $target) -{ - /** @var $iterator \RecursiveIteratorIterator|\RecursiveDirectoryIterator */ - $iterator = new RecursiveIteratorIterator( - new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), - RecursiveIteratorIterator::SELF_FIRST - ); - - foreach ($iterator as $file) { - /** @var $file SplFileInfo */ - if ($file->isDir()) { - mkdir($target . '/' . $iterator->getSubPathName()); - } else { - copy($file, $target . '/' . $iterator->getSubPathName()); - } - } -} - -/** - * Removes a directory recursively. - * - * @param string $directory The path to the directory to remove. - */ -function removeDirectory($directory) -{ - /** @var $iterator \RecursiveIteratorIterator|\RecursiveDirectoryIterator */ - $iterator = new RecursiveIteratorIterator( - new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS), - RecursiveIteratorIterator::CHILD_FIRST - ); - - foreach ($iterator as $file) { - /** @var $file SplFileInfo */ - if ($file->isDir()) { - rmdir($file->getRealPath()); - } else { - unlink($file->getRealPath()); - } - } - - rmdir($directory); -} - -$phpCodeSnifferDir = __DIR__ . '/vendor/squizlabs/php_codesniffer'; - -if ( ! file_exists($phpCodeSnifferDir)) { - throw new \RuntimeException( - 'Could not find PHP_CodeSniffer dependency. ' . - 'Did you maybe forget to run "php composer.phar install --prefer-source --dev"?' - ); -} - -$sniffTestSuiteFile = $phpCodeSnifferDir . '/tests/Standards/AllSniffs.php'; - -if ( ! file_exists($sniffTestSuiteFile)) { - throw new \RuntimeException( - 'Could not find PHP_CodeSniffer test suite. ' . - 'Did you maybe forget to run composer installation with option "--prefer-source"?' - ); -} - -require_once __DIR__ . '/vendor/autoload.php'; - -$doctrineStandardDir = $phpCodeSnifferDir . '/CodeSniffer/Standards/Doctrine'; - -if (file_exists($doctrineStandardDir)) { - removeDirectory($doctrineStandardDir); -} - -mkdir($doctrineStandardDir); -mkdir($doctrineStandardDir . '/Sniffs'); -mkdir($doctrineStandardDir . '/Tests'); -copy(__DIR__ . '/ruleset.xml', $doctrineStandardDir . '/ruleset.xml'); - -copyDirectory(__DIR__ . '/Sniffs', $doctrineStandardDir . '/Sniffs'); -copyDirectory(__DIR__ . '/Tests', $doctrineStandardDir . '/Tests'); From aedfc602692353090b9cd26369a98e532d711e2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Sun, 20 Aug 2017 01:16:10 +0200 Subject: [PATCH 3/7] Add new sniffs to validate new rules We were missing the checks for: - Spaces before AND after not operator - Spaces before AND after return type declaration --- .gitattributes | 1 + .gitignore | 1 + composer.json | 9 +- .../Sniffs/Spacing/ControlStructureSniff.php | 134 ++++++++++++++++++ lib/Doctrine/Sniffs/Spacing/EnsureSpaces.php | 74 ++++++++++ .../Sniffs/Spacing/SpaceOnNotSniff.php | 45 ++++++ .../Sniffs/Spacing/SpaceOnReturnTypeSniff.php | 57 ++++++++ lib/Doctrine/ruleset.xml | 33 +++++ phpcs.xml.dist | 15 ++ ruleset.xml | 19 --- 10 files changed, 367 insertions(+), 21 deletions(-) create mode 100644 lib/Doctrine/Sniffs/Spacing/ControlStructureSniff.php create mode 100644 lib/Doctrine/Sniffs/Spacing/EnsureSpaces.php create mode 100644 lib/Doctrine/Sniffs/Spacing/SpaceOnNotSniff.php create mode 100644 lib/Doctrine/Sniffs/Spacing/SpaceOnReturnTypeSniff.php create mode 100644 lib/Doctrine/ruleset.xml create mode 100644 phpcs.xml.dist delete mode 100644 ruleset.xml diff --git a/.gitattributes b/.gitattributes index f13d4d91..b9f311fa 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,3 +2,4 @@ .gitattributes export-ignore .gitignore export-ignore .travis.yml export-ignore +phpcs.xml.dist export-ignore diff --git a/.gitignore b/.gitignore index d1502b08..34a21f2a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor/ +phpcs.xml composer.lock diff --git a/composer.json b/composer.json index 28a6f005..6a1b12e5 100644 --- a/composer.json +++ b/composer.json @@ -9,9 +9,14 @@ {"name": "Benjamin Eberlei", "email": "kontakt@beberlei.de"}, {"name": "Steve Müller", "email": "st.mueller@dzh-online.de"} ], + "autoload": { + "psr-4": { + "Doctrine\\Sniffs\\": "lib/Doctrine/Sniffs" + } + }, "require": { - "php": ">=5.3", - "squizlabs/php_codesniffer": "~1.0" + "php": "^7.1", + "squizlabs/php_codesniffer": "~3.0" }, "extra": { "branch-alias": { diff --git a/lib/Doctrine/Sniffs/Spacing/ControlStructureSniff.php b/lib/Doctrine/Sniffs/Spacing/ControlStructureSniff.php new file mode 100644 index 00000000..6ecb2014 --- /dev/null +++ b/lib/Doctrine/Sniffs/Spacing/ControlStructureSniff.php @@ -0,0 +1,134 @@ +. +*/ + +declare(strict_types=1); + +namespace Doctrine\Sniffs\Spacing; + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; + +/** + * Small modification on PSR2 sniff to allow a space before the NOT operator + * + * @see \PHP_CodeSniffer\Standards\PSR2\Sniffs\ControlStructures\ControlStructureSpacingSniff + */ +final class ControlStructureSniff implements Sniff +{ + private const OPENER_NAME = 'parenthesis_opener'; + private const OPENER_MESSAGE = 'Expected no spaces after opening bracket; %s found'; + private const CLOSER_NAME = 'parenthesis_closer'; + private const CLOSER_MESSAGE = 'Expected no spaces before closing bracket; %s found'; + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() + { + return [ + \T_IF, + \T_WHILE, + \T_FOREACH, + \T_FOR, + \T_SWITCH, + \T_DO, + \T_ELSE, + \T_ELSEIF, + \T_TRY, + \T_CATCH, + ]; + } + + public function process(File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + + if ( ! isset($tokens[$stackPtr][self::OPENER_NAME], $tokens[$stackPtr][self::CLOSER_NAME])) { + return; + } + + $openerPosition = $tokens[$stackPtr][self::OPENER_NAME]; + $closerPosition = $tokens[$stackPtr][self::CLOSER_NAME]; + + $this->validateParenthesisOpener($phpcsFile, $tokens, $stackPtr, $openerPosition); + $this->validateParenthesisCloser($phpcsFile, $tokens, $stackPtr, $openerPosition, $closerPosition); + } + + private function validateParenthesisOpener(File $file, array $tokens, int $position, int $openerPosition) : void + { + $nextTokenPosition = $openerPosition + 1; + $nextToken = $tokens[$nextTokenPosition]; + + if ($nextToken['code'] !== T_WHITESPACE || $tokens[$nextTokenPosition + 1]['code'] === T_BOOLEAN_NOT) { + return; + } + + $spaces = $nextToken['length']; + + if (\strpos($nextToken['content'], $file->eolChar) !== false) { + $spaces = 'newline'; + } + + $file->recordMetric($position, 'Spaces after control structure open parenthesis', $spaces); + + if ($spaces === 0) { + return; + } + + if ( ! $file->addFixableError(self::OPENER_MESSAGE, $nextTokenPosition, 'AfterOpenBrace', [$spaces])) { + return; + } + + $file->fixer->replaceToken($nextTokenPosition, ''); + } + + private function validateParenthesisCloser( + File $file, + array $tokens, + int $position, + int $openerPosition, + int $closerPosition + ) : void { + if ($tokens[$openerPosition]['line'] !== $tokens[$closerPosition]['line']) { + return; + } + + $previousTokenPosition = $closerPosition - 1; + $previousToken = $tokens[$previousTokenPosition]; + $spaces = 0; + + if ($previousToken['code'] === T_WHITESPACE) { + $spaces = $previousToken['length']; + } + + $file->recordMetric($position, 'Spaces before control structure close parenthesis', $spaces); + + if ($spaces === 0) { + return; + } + + if ( ! $file->addFixableError(self::CLOSER_MESSAGE, $previousTokenPosition, 'BeforeCloseBrace', [$spaces])) { + return; + } + + $file->fixer->replaceToken($previousTokenPosition, ''); + } +} diff --git a/lib/Doctrine/Sniffs/Spacing/EnsureSpaces.php b/lib/Doctrine/Sniffs/Spacing/EnsureSpaces.php new file mode 100644 index 00000000..2804121a --- /dev/null +++ b/lib/Doctrine/Sniffs/Spacing/EnsureSpaces.php @@ -0,0 +1,74 @@ +. +*/ + +declare(strict_types=1); + +namespace Doctrine\Sniffs\Spacing; + +use PHP_CodeSniffer\Files\File; + +trait EnsureSpaces +{ + protected function ensureSpaceBefore(File $file, array $tokens, int $position, string $message) : void + { + $spacing = $this->numberOfSpaces($tokens, $position - 1); + + if ($spacing === 1) { + return; + } + + if ( ! $file->addFixableError($message, $position, 'before', ['before', $spacing])) { + return; + } + + if ($spacing === 0) { + $file->fixer->addContentBefore($position, ' '); + return; + } + + $file->fixer->replaceToken($position - 1, ' '); + } + + protected function ensureSpaceAfter(File $file, array $tokens, int $position, string $message) : void + { + $spacing = $this->numberOfSpaces($tokens, $position + 1); + + if ($spacing === 1) { + return; + } + + if ( ! $file->addFixableError($message, $position, 'after', ['after', $spacing])) { + return; + } + + if ($spacing === 0) { + $file->fixer->addContent($position, ' '); + return; + } + + $file->fixer->replaceToken($position + 1, ' '); + } + + private function numberOfSpaces(array $tokens, int $position) : int + { + $token = $tokens[$position]; + + return $token['code'] === T_WHITESPACE ? $token['length'] : 0; + } +} diff --git a/lib/Doctrine/Sniffs/Spacing/SpaceOnNotSniff.php b/lib/Doctrine/Sniffs/Spacing/SpaceOnNotSniff.php new file mode 100644 index 00000000..d3a33b7d --- /dev/null +++ b/lib/Doctrine/Sniffs/Spacing/SpaceOnNotSniff.php @@ -0,0 +1,45 @@ +. +*/ + +declare(strict_types=1); + +namespace Doctrine\Sniffs\Spacing; + +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; + +final class SpaceOnNotSniff implements Sniff +{ + use EnsureSpaces; + + private const MESSAGE = 'There must be a single space %s a NOT operator; %d found'; + + public function register() + { + return [\T_BOOLEAN_NOT]; + } + + public function process(File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + + $this->ensureSpaceBefore($phpcsFile, $tokens, $stackPtr, self::MESSAGE); + $this->ensureSpaceAfter($phpcsFile, $tokens, $stackPtr, self::MESSAGE); + } +} diff --git a/lib/Doctrine/Sniffs/Spacing/SpaceOnReturnTypeSniff.php b/lib/Doctrine/Sniffs/Spacing/SpaceOnReturnTypeSniff.php new file mode 100644 index 00000000..ede41e25 --- /dev/null +++ b/lib/Doctrine/Sniffs/Spacing/SpaceOnReturnTypeSniff.php @@ -0,0 +1,57 @@ +. +*/ + +declare(strict_types=1); + +namespace Doctrine\Sniffs\Spacing; + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; + +final class SpaceOnReturnTypeSniff implements Sniff +{ + use EnsureSpaces; + + private const MESSAGE = 'There must be a single space %s the colon on return types; %d found'; + + public function register() + { + return [\T_RETURN_TYPE]; + } + + public function process(File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + $colonPosition = $this->findColonPosition($tokens, $stackPtr); + + $this->ensureSpaceBefore($phpcsFile, $tokens, $colonPosition, self::MESSAGE); + $this->ensureSpaceAfter($phpcsFile, $tokens, $colonPosition, self::MESSAGE); + } + + private function findColonPosition(array $tokens, int $position) : int + { + $colonPosition = $position; + + do { + --$colonPosition; + } while ($tokens[$colonPosition]['code'] !== T_COLON); + + return $colonPosition; + } +} diff --git a/lib/Doctrine/ruleset.xml b/lib/Doctrine/ruleset.xml new file mode 100644 index 00000000..9c17cde6 --- /dev/null +++ b/lib/Doctrine/ruleset.xml @@ -0,0 +1,33 @@ + + + The Doctrine coding standard. + + + + + + + + + + error + + + + + + + + + + + + + + + + + + + + diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 00000000..9c9331f3 --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,15 @@ + + + Check the code of the sniffs in doctrine/coding-standards. + + + + + + + + + + + lib + diff --git a/ruleset.xml b/ruleset.xml deleted file mode 100644 index 7287d86f..00000000 --- a/ruleset.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - The Doctrine coding standard. - - - - - - - error - - - - - - - - - From 4bd6c9b76ac983dbbf610608c33b30d33350e5a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Sun, 20 Aug 2017 01:18:03 +0200 Subject: [PATCH 4/7] Use dealerdirect/phpcodesniffer-composer-installer So that our standard gets automatically registered to PHPCS. --- composer.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 6a1b12e5..8ff1eeaa 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "doctrine/coding-standard", - "type": "library", + "type": "phpcodesniffer-standard", "description": "Doctrine Coding Standard", "keywords": ["doctrine", "coding", "standard", "cs", "code", "style", "sniffer"], "homepage": "http://www.doctrine-project.org", @@ -16,7 +16,8 @@ }, "require": { "php": "^7.1", - "squizlabs/php_codesniffer": "~3.0" + "squizlabs/php_codesniffer": "~3.0", + "dealerdirect/phpcodesniffer-composer-installer": "^0.4.2" }, "extra": { "branch-alias": { From cdce50da5a62b60640a43aacfa7abe8f5268ada9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Sun, 20 Aug 2017 01:19:27 +0200 Subject: [PATCH 5/7] Use PHPCS itself to validate our sniffs That's easier than doing some workarounds on PHPUnit. --- .gitattributes | 2 +- .gitignore | 2 + tests/expected_report.txt | 16 ++++++ tests/fixed/concatenation_spacing.php | 35 +++++++++++++ tests/fixed/not_spacing.php | 19 +++++++ tests/fixed/return_type_on_closures.php | 61 +++++++++++++++++++++ tests/fixed/return_type_on_methods.php | 70 +++++++++++++++++++++++++ tests/input/concatenation_spacing.php | 35 +++++++++++++ tests/input/not_spacing.php | 19 +++++++ tests/input/return_type_on_closures.php | 66 +++++++++++++++++++++++ tests/input/return_type_on_methods.php | 70 +++++++++++++++++++++++++ 11 files changed, 394 insertions(+), 1 deletion(-) create mode 100644 tests/expected_report.txt create mode 100644 tests/fixed/concatenation_spacing.php create mode 100644 tests/fixed/not_spacing.php create mode 100644 tests/fixed/return_type_on_closures.php create mode 100644 tests/fixed/return_type_on_methods.php create mode 100644 tests/input/concatenation_spacing.php create mode 100644 tests/input/not_spacing.php create mode 100644 tests/input/return_type_on_closures.php create mode 100644 tests/input/return_type_on_methods.php diff --git a/.gitattributes b/.gitattributes index b9f311fa..30182964 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,4 @@ -/Tests export-ignore +/tests export-ignore .gitattributes export-ignore .gitignore export-ignore .travis.yml export-ignore diff --git a/.gitignore b/.gitignore index 34a21f2a..2a76e0ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ vendor/ phpcs.xml composer.lock +tests/input2 +phpcs.log diff --git a/tests/expected_report.txt b/tests/expected_report.txt new file mode 100644 index 00000000..0b74045c --- /dev/null +++ b/tests/expected_report.txt @@ -0,0 +1,16 @@ + +PHP CODE SNIFFER REPORT SUMMARY +---------------------------------------------------------------------- +FILE ERRORS WARNINGS +---------------------------------------------------------------------- +tests/input/concatenation_spacing.php 19 0 +tests/input/not_spacing.php 7 0 +tests/input/return_type_on_closures.php 21 0 +tests/input/return_type_on_methods.php 16 0 +---------------------------------------------------------------------- +A TOTAL OF 63 ERRORS AND 0 WARNINGS WERE FOUND IN 4 FILES +---------------------------------------------------------------------- +PHPCBF CAN FIX 63 OF THESE SNIFF VIOLATIONS AUTOMATICALLY +---------------------------------------------------------------------- + + diff --git a/tests/fixed/concatenation_spacing.php b/tests/fixed/concatenation_spacing.php new file mode 100644 index 00000000..0c71ed80 --- /dev/null +++ b/tests/fixed/concatenation_spacing.php @@ -0,0 +1,35 @@ + 0) { + echo 1; +} elseif ( ! $test === 0) { + echo 0; +} else { + echo -1; +} + +while ( ! true) { + echo 1; +} + +do { + echo 1; +} while ( ! true); diff --git a/tests/fixed/return_type_on_closures.php b/tests/fixed/return_type_on_closures.php new file mode 100644 index 00000000..9e531d39 --- /dev/null +++ b/tests/fixed/return_type_on_closures.php @@ -0,0 +1,61 @@ + 0) { + echo 1; +} elseif ( !$test === 0) { + echo 0; +} else { + echo -1; +} + +while ( ! true) { + echo 1; +} + +do { + echo 1; +} while ( ! true); diff --git a/tests/input/return_type_on_closures.php b/tests/input/return_type_on_closures.php new file mode 100644 index 00000000..2848baf2 --- /dev/null +++ b/tests/input/return_type_on_closures.php @@ -0,0 +1,66 @@ + Date: Sun, 20 Aug 2017 01:20:45 +0200 Subject: [PATCH 6/7] Apply necessary modifications to build --- .travis.yml | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index d3b92e36..100afc59 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,23 +1,32 @@ +dist: trusty +sudo: false language: php -sudo: false +php: + - 7.1 + - 7.2 + - nightly + +cache: + directories: + - $HOME/.composer/cache before_install: - - composer self-update + - mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available" + - composer self-update + +install: travis_retry composer update --prefer-dist -install: - - composer install --prefer-source +script: + - vendor/bin/phpcs + - vendor/bin/phpcs tests/input --report=summary --report-file=phpcs.log; diff tests/expected_report.txt phpcs.log -matrix: - include: - - php: 5.3 - - php: 5.4 - - php: 5.5 - - php: 5.6 - - php: nightly - - php: hhvm - allow_failures: - - php: nightly - - php: hhvm +jobs: + allow_failures: + - php: nightly -script: ./vendor/bin/phpunit + include: + - stage: Apply fixes + before_script: + - cp -R tests/input/ tests/input2/ + script: vendor/bin/phpcbf tests/input2; diff tests/input2 tests/fixed From 20decd8afc42bbea4f61d7f2e37f80308c3234e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Sun, 20 Aug 2017 02:47:21 +0200 Subject: [PATCH 7/7] Update documentation --- README.md | 87 +++++++++++-------------------------------------------- 1 file changed, 17 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index 72841d50..801117cf 100644 --- a/README.md +++ b/README.md @@ -9,108 +9,55 @@ The [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) Coding Stand Installation ------------ -You have three possibilities to use the Doctrine Coding Standard with PHP_CodeSniffer in a particular project. +You have two possibilities to use the Doctrine Coding Standard with PHP_CodeSniffer in a particular project. -### 1. Standalone installation +### 1. As a composer dependency of your project -You can install the Doctrine Coding Standard as a plugin into your global system PHP_CodeSniffer installation: - -```bash -$ cd /path/to/phpcs/CodeSniffer/Standards -$ php composer create-project doctrine/coding-standard Doctrine ~0.1@dev -``` - -Then you can use it like -(assuming that you have the `phpcs` binary in your search path): - -```bash -$ phpcs --standard=Doctrine /path/to/some/file/to/sniff.php -``` - -Or even set it as default standard -(assuming that you have the `phpcs` binary in your search path): - -```bash -$ phpcs --config-set default_standard Doctrine -``` - -And just sniff a particular file with -(assuming that you have the `phpcs` binary in your search path): - -```bash -$ phpcs /path/to/some/file/to/sniff.php -``` - -### 2. Global installation - -You also can install the Doctrine Coding Standard globally: +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 global require doctrine/coding-standard:~0.1@dev +$ php composer require doctrine/coding-standard:~0.1@dev ``` Then you can use it like: ```bash -$ phpcs --standard=~/.composer/vendor/doctrine/coding-standard/Doctrine /path/to/some/file/to/sniff.php -``` - -Or even set it as default standard: - -```bash -$ phpcs --config-set default_standard ~/.composer/vendor/doctrine/coding-standard/Doctrine +$ ./vendor/bin/phpcs --standard=Doctrine /path/to/some/file/to/sniff.php ``` -And just sniff a particular file with: +You might also do automatic fixes using `phpcbf`: ```bash -$ phpcs /path/to/some/file/to/sniff.php +$ ./vendor/bin/phpcbf --standard=Doctrine /path/to/some/file/to/sniff.php ``` -### 3. Custom installation +### 2. Global installation -You can install the Doctrine Coding Standard anywhere you want: +You can also install the Doctrine Coding Standard globally: ```bash -$ php composer create-project doctrine/coding-standard /path/to/coding-standard/Doctrine ~0.1@dev +$ composer global require doctrine/coding-standard:~0.1@dev ``` Then you can use it like: ```bash -$ ./vendor/bin/phpcs --standard=. /path/to/some/file/to/sniff.php -``` - -### 4. 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:~0.1@dev +$ phpcs --standard=Doctrine /path/to/some/file/to/sniff.php ``` -Then you can use it like: +You might also do automatic fixes using `phpcbf`: ```bash -$ ./vendor/bin/phpcs --standard=vendor/doctrine/coding-standard/Doctrine /path/to/some/file/to/sniff.php +$ phpcbf --standard=Doctrine /path/to/some/file/to/sniff.php ``` Testing ------- -If you are contributing to the Doctrine Coding Standard and want to test your contribution, you have to -make sure all dependencies are correctly installed: - -```bash -$ php composer install --prefer-source -``` - -The option `--prefer-source` is particularly necessary to ensure the test suite from PHP_CodeSniffer is -installed. Otherwise the test suite for the Doctrine Coding Standard won't work! - -Run the test suite with: +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: ```bash -$ ./vendor/bin/phpunit +$ ./vendor/bin/phpcs tests/input --report=summary --report-file=phpcs.log; diff tests/expected_report.txt phpcs.log ```