From 7caa54eec66c9a1d365fe3c276ce99532d578759 Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Wed, 12 Apr 2017 17:06:38 +0200 Subject: [PATCH 01/25] Add license sniff and fixes The test are executed independent of the standard AbstractSniffUnitTest class. That class is only available if PHP_CodeSniffer is downloaded by composer with prefer-source. Copyright year detection: - Detect current copyright year from LICENSE.md and/or COPYING.md - Detect current copyright year from all source files Violation detection (when running phpcs): - Check file-level docblock - Check missing/invalid @see tag - Check missing/invalid @copyright tag - Check missing/invalid @license tag - Check order of @see, @copyright and @license tags Fixes (when running phpcbf): - Create missing LICENSE.md and/or COPYING.md - Use the detected copyright year in LICENSE.md and COPYING.md - Fix invalid @see tag - Fix invalid @copyright tag - Fix invalid @license tag --- .gitignore | 5 + .travis.yml | 30 ++ COPYING.md | 3 + LICENSE.md | 2 +- composer.json | 32 +- phpcs.xml | 17 + phpunit.xml.dist | 18 ++ ruleset.xml | 20 +- .../Commenting/FileLevelDocBlockSniff.php | 295 ++++++++++++++++++ .../Sniffs/Files/COPYING.md | 10 + .../Sniffs/Files/CopyingSniff.php | 65 ++++ .../Sniffs/Files/LICENSE.md | 10 + .../Sniffs/Files/LicenseSniff.php | 65 ++++ src/ZendCodingStandard/Utils/LicenseUtils.php | 129 ++++++++ src/ZendCodingStandard/ruleset.xml | 30 ++ test/SniffTestCase.php | 150 +++++++++ .../Assets/FileLevelDocBlockEmptyTags.php | 12 + ...elDocBlockIncorrectCopyrightLink.fixed.php | 12 + ...ileLevelDocBlockIncorrectCopyrightLink.php | 12 + ...evelDocBlockIncorrectLicenseLink.fixed.php | 12 + .../FileLevelDocBlockIncorrectLicenseLink.php | 12 + ...LevelDocBlockIncorrectSourceLink.fixed.php | 12 + .../FileLevelDocBlockIncorrectSourceLink.php | 12 + .../Assets/FileLevelDocBlockMissing.php | 7 + .../Assets/FileLevelDocBlockPass.php | 12 + .../FileLevelDocBlockSpacingAfterOpen.php | 13 + .../Commenting/FileLevelDocBlockSniffTest.php | 110 +++++++ test/Util/LicenseUtilTest.php | 53 ++++ 28 files changed, 1131 insertions(+), 29 deletions(-) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 COPYING.md create mode 100644 phpcs.xml create mode 100644 phpunit.xml.dist create mode 100644 src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php create mode 100644 src/ZendCodingStandard/Sniffs/Files/COPYING.md create mode 100644 src/ZendCodingStandard/Sniffs/Files/CopyingSniff.php create mode 100644 src/ZendCodingStandard/Sniffs/Files/LICENSE.md create mode 100644 src/ZendCodingStandard/Sniffs/Files/LicenseSniff.php create mode 100644 src/ZendCodingStandard/Utils/LicenseUtils.php create mode 100644 src/ZendCodingStandard/ruleset.xml create mode 100644 test/SniffTestCase.php create mode 100644 test/Sniffs/Commenting/Assets/FileLevelDocBlockEmptyTags.php create mode 100644 test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectCopyrightLink.fixed.php create mode 100644 test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectCopyrightLink.php create mode 100644 test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectLicenseLink.fixed.php create mode 100644 test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectLicenseLink.php create mode 100644 test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectSourceLink.fixed.php create mode 100644 test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectSourceLink.php create mode 100644 test/Sniffs/Commenting/Assets/FileLevelDocBlockMissing.php create mode 100644 test/Sniffs/Commenting/Assets/FileLevelDocBlockPass.php create mode 100644 test/Sniffs/Commenting/Assets/FileLevelDocBlockSpacingAfterOpen.php create mode 100644 test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php create mode 100644 test/Util/LicenseUtilTest.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..d5e33e78 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/clover.xml +/composer.lock +/coveralls-upload.json +/phpunit.xml +/vendor/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..e7dd135e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,30 @@ +sudo: false + +language: php + +cache: + directories: + - $HOME/.composer/cache + - vendor + +env: + global: + - COMPOSER_ARGS="--no-interaction" + +matrix: + include: + - php: 7 + +before_install: + - if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi + +install: + - travis_retry composer install $COMPOSER_ARGS --ignore-platform-reqs + - composer show + +script: + - vendor/bin/phpcs -s + - vendor/bin/phpunit + +notifications: + email: false diff --git a/COPYING.md b/COPYING.md new file mode 100644 index 00000000..21ff4cdb --- /dev/null +++ b/COPYING.md @@ -0,0 +1,3 @@ +Copyright (c) 2016-2017, Zend Technologies USA, Inc. + +All rights reserved. diff --git a/LICENSE.md b/LICENSE.md index badc26ea..4095e570 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,4 +1,4 @@ -Copyright (c) 2016, Zend Technologies USA, Inc. +Copyright (c) 2016-2017, Zend Technologies USA, Inc. All rights reserved. diff --git a/composer.json b/composer.json index 89f82020..b9aee0a9 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1,26 @@ { - "name": "zendframework/zend-coding-standard", - "description": "Zend Framework coding standard", - "license": "BSD-3-Clause", - "keywords": [ - "zf", - "coding standard" - ], - "require": { - "squizlabs/php_codesniffer": "^2.7" + "name": "zendframework/zend-coding-standard", + "description": "Zend Framework coding standard", + "license": "BSD-3-Clause", + "keywords": [ + "zf", + "coding standard" + ], + "require": { + "squizlabs/php_codesniffer": "^2.7" + }, + "require-dev": { + "mikey179/vfsStream": "^1.6", + "phpunit/phpunit": "^6.1" + }, + "autoload": { + "psr-4": { + "Zend\\CodingStandard\\": "src/ZendCodingStandard/" } + }, + "autoload-dev": { + "psr-4": { + "ZendTest\\CodingStandard\\": "test/" + } + } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 00000000..5cc286e1 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,17 @@ + + + + + + src + test + + + */Assets/* + + + + + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 00000000..dc917472 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,18 @@ + + + + + test/ + + + + + + src/ + test/Util/ + + + diff --git a/ruleset.xml b/ruleset.xml index 2e2536e0..11bdacda 100644 --- a/ruleset.xml +++ b/ruleset.xml @@ -1,23 +1,5 @@ Zend Framework Coding Standard - - - - - - - - - - - - - - - - - - - + diff --git a/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php new file mode 100644 index 00000000..92a3ed5d --- /dev/null +++ b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php @@ -0,0 +1,295 @@ +licenseFirstYear = gmdate('Y'); + $this->licenseLastYear = gmdate('Y'); + + $year = LicenseUtils::getCopyrightFirstYear(LicenseUtils::getCopyrightFile()); + if ($year !== null && $year < $this->licenseFirstYear) { + $this->licenseFirstYear = $year; + } + + $year = LicenseUtils::getCopyrightFirstYear(LicenseUtils::getLicenseFile()); + if ($year !== null && $year < $this->licenseFirstYear) { + $this->licenseFirstYear = $year; + } + + $content = file_get_contents('composer.json'); + $content = json_decode($content, true); + + $this->repo = $content['name']; + } + + /** + * Registers the tokens that this sniff wants to listen for. + * + * @return int[] + * @see Tokens.php + */ + public function register() + { + return [T_OPEN_TAG]; + } + + /** + * Called when one of the token types that this sniff is listening for is + * found. + * + * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the + * token was found. + * @param int $stackPtr The position in the PHP_CodeSniffer + * file's token stack where the token + * was found. + * + * @return int Optionally returns a stack pointer. The sniff will not be + * called again on the current file until the returned stack + * pointer is reached. Return (count($tokens) + 1) to skip + * the rest of the file. + */ + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + { + // Skip license file + if (in_array(substr($phpcsFile->getFilename(), -10), ['LICENSE.md', 'COPYING.md'])) { + return ($phpcsFile->numTokens + 1); + } + + $tokens = $phpcsFile->getTokens(); + $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); + + if ($tokens[$commentStart]['code'] === T_COMMENT) { + $phpcsFile->addError( + 'You must use "/**" style comments for a file-level DocBlock', + $commentStart, + 'WrongStyle' + ); + $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'yes'); + + return ($phpcsFile->numTokens + 1); + } + + if ($commentStart === false || $tokens[$commentStart]['code'] !== T_DOC_COMMENT_OPEN_TAG) { + $phpcsFile->addError('Missing file-level DocBlock', $stackPtr, 'Missing'); + $phpcsFile->recordMetric($stackPtr, 'File has file-level DocBlock', 'no'); + + return ($phpcsFile->numTokens + 1); + } + + $commentEnd = $tokens[$commentStart]['comment_closer']; + + $nextToken = $phpcsFile->findNext( + T_WHITESPACE, + $commentEnd + 1, + null, + true + ); + + $ignore = [ + T_CLASS, + T_INTERFACE, + T_TRAIT, + T_FUNCTION, + T_CLOSURE, + T_PUBLIC, + T_PRIVATE, + T_PROTECTED, + T_FINAL, + T_STATIC, + T_ABSTRACT, + T_CONST, + T_PROPERTY, + T_INCLUDE, + T_INCLUDE_ONCE, + T_REQUIRE, + T_REQUIRE_ONCE, + ]; + + if (in_array($tokens[$nextToken]['code'], $ignore) === true) { + $phpcsFile->addError('Missing file-level DocBlock', $stackPtr, 'Missing'); + $phpcsFile->recordMetric($stackPtr, 'File has file-level DocBlock', 'no'); + + return ($phpcsFile->numTokens + 1); + } + + $phpcsFile->recordMetric($stackPtr, 'File has file-level DocBlock', 'yes'); + + // No blank line between the open tag and the file comment. + if ($tokens[$commentStart]['line'] > ($tokens[$stackPtr]['line'] + 1)) { + $error = 'There must be no blank lines before the file-level DocBlock'; + $phpcsFile->addError($error, $stackPtr, 'SpacingAfterOpen'); + } + + // Exactly one blank line after the file comment. + $next = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), null, true); + if ($tokens[$next]['line'] !== ($tokens[$commentEnd]['line'] + 2)) { + $error = 'There must be exactly one blank line after the file-level DocBlock'; + $phpcsFile->addError($error, $commentEnd, 'SpacingAfterComment'); + } + + // Required tags in correct order. + $required = [ + '@see' => true, + '@copyright' => true, + '@license' => true, + ]; + + $foundTags = []; + foreach ($tokens[$commentStart]['comment_tags'] as $tag) { + $name = $tokens[$tag]['content']; + $isRequired = isset($required[$name]); + + if ($isRequired === true && in_array($name, $foundTags) === true) { + $error = 'Only one %s tag is allowed in a file-level DocBlock'; + $data = [$name]; + $phpcsFile->addError($error, $tag, 'Duplicate' . ucfirst(substr($name, 1)) . 'Tag', $data); + } + + $foundTags[] = $name; + + if ($isRequired === false) { + continue; + } + + $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd); + if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) { + $error = 'Content missing for %s tag in file-level DocBlock'; + $data = [$name]; + $phpcsFile->addError($error, $tag, 'Empty' . ucfirst(substr($name, 1)) . 'Tag', $data); + continue; + } + + if ($name === '@see') { + $expected = sprintf('https://github.com/%s for the canonical source repository', $this->repo); + if (preg_match('|^' . $expected . '$|', $tokens[$string]['content']) === 0) { + $error = 'Expected "%s" for @see tag'; + $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectSourceLink', [$expected]); + if ($fix === true) { + $this->fix = true; + $phpcsFile->fixer->replaceToken($string, $expected); + } + } + continue; + } + + if ($name === '@copyright') { + // Grab license date range + $this->parseCopyrightDate($tokens[$string]['content']); + + $expected = sprintf('https://github.com/%s/blob/master/COPYING.md Copyright', $this->repo); + if (preg_match('|^' . $expected . '$|', $tokens[$string]['content']) === 0) { + $error = 'Expected "%s" for @copyright tag'; + $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectCopyrightLink', [$expected]); + if ($fix === true) { + $this->fix = true; + $phpcsFile->fixer->replaceToken($string, $expected); + } + } + continue; + } + + if ($name === '@license') { + $expected = sprintf('https://github.com/%s/blob/master/LICENSE.md New BSD License', $this->repo); + if (preg_match('|^' . $expected . '$|', $tokens[$string]['content']) === 0) { + $error = 'Expected "%s" for @license tag'; + $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectLicenseLink', [$expected]); + if ($fix === true) { + $this->fix = true; + $phpcsFile->fixer->replaceToken($string, $expected); + } + } + continue; + } + } + + // Check if the tags are in the correct position. + $pos = 0; + foreach ($required as $tag => $true) { + if (in_array($tag, $foundTags) === false) { + $error = 'Missing %s tag in file-level DocBlock'; + $data = [$tag]; + $phpcsFile->addError($error, $commentEnd, 'Missing' . ucfirst(substr($tag, 1)) . 'Tag', $data); + } + + if (isset($foundTags[$pos]) === false) { + break; + } + + if ($foundTags[$pos] !== $tag) { + $error = 'The file-level DocBlock tag in position %s should be the %s tag'; + $data = [ + ($pos + 1), + $tag, + ]; + $phpcsFile->addWarning( + $error, + $tokens[$commentStart]['comment_tags'][$pos], + ucfirst(substr($tag, 1)) . 'TagOrder', + $data + ); + } + + $pos++; + } + + if ($this->copyrightChanged === true && $this->fix === true) { + // Make sure the files exist + LicenseUtils::createCopyrightFile(); + LicenseUtils::createLicenseFile(); + + // Update copyright file + LicenseUtils::updateCopyright( + LicenseUtils::getCopyrightFile(), + $this->licenseFirstYear, + $this->licenseLastYear + ); + + // Update license file + LicenseUtils::updateCopyright( + LicenseUtils::getLicenseFile(), + $this->licenseFirstYear, + $this->licenseLastYear + ); + + $this->copyrightChanged = false; + } + + // Ignore the rest of the file. + return ($phpcsFile->numTokens + 1); + } + + private function parseCopyrightDate($string) + { + $matches = []; + preg_match('|(?[\d]{4})(-(?[\d]{4}))?|', $string, $matches); + + $licenseFirstYear = isset($matches['start']) ? $matches['start'] : null; + $licenseLastYear = isset($matches['end']) ? $matches['end'] : null; + if ($licenseFirstYear !== null && $this->licenseFirstYear > $licenseFirstYear) { + $this->licenseFirstYear = $licenseFirstYear; + $this->copyrightChanged = true; + } + if ($licenseLastYear !== null && $this->licenseLastYear < $licenseLastYear) { + $this->licenseLastYear = $licenseLastYear; + $this->copyrightChanged = true; + } + } +} diff --git a/src/ZendCodingStandard/Sniffs/Files/COPYING.md b/src/ZendCodingStandard/Sniffs/Files/COPYING.md new file mode 100644 index 00000000..785a8ad1 --- /dev/null +++ b/src/ZendCodingStandard/Sniffs/Files/COPYING.md @@ -0,0 +1,10 @@ +copyrightFile = LicenseUtils::getCopyrightFile(); + } + + /** + * Registers the tokens that this sniff wants to listen for. + * + * @return int[] + * @see Tokens.php + */ + public function register() + { + return [T_OPEN_TAG]; + } + + /** + * Called when one of the token types that this sniff is listening for is + * found. + * + * @param \PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the + * token was found. + * @param int $stackPtr The position in the PHP_CodeSniffer + * file's token stack where the token + * was found. + * + * @return int Optionally returns a stack pointer. The sniff will not be + * called again on the current file until the returned stack + * pointer is reached. Return (count($tokens) + 1) to skip + * the rest of the file. + */ + public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) + { + // Skip all files except the copying file + if (substr($phpcsFile->getFilename(), -10) !== $this->copyrightFile->getFilename()) { + return ($phpcsFile->numTokens + 1); + } + + if (! $this->copyrightFile->getRealPath()) { + $error = 'Missing COPYING.md file in the component root dir'; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingLicense'); + if ($fix === true) { + LicenseUtils::createCopyrightFile(); + } + } + + // TODO: Check copyright year + + // Ignore the rest of the file. + return ($phpcsFile->numTokens + 1); + } +} diff --git a/src/ZendCodingStandard/Sniffs/Files/LICENSE.md b/src/ZendCodingStandard/Sniffs/Files/LICENSE.md new file mode 100644 index 00000000..34498305 --- /dev/null +++ b/src/ZendCodingStandard/Sniffs/Files/LICENSE.md @@ -0,0 +1,10 @@ +licenseFile = LicenseUtils::getLicenseFile(); + } + + /** + * Registers the tokens that this sniff wants to listen for. + * + * @return int[] + * @see Tokens.php + */ + public function register() + { + return [T_OPEN_TAG]; + } + + /** + * Called when one of the token types that this sniff is listening for is + * found. + * + * @param \PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the + * token was found. + * @param int $stackPtr The position in the PHP_CodeSniffer + * file's token stack where the token + * was found. + * + * @return int Optionally returns a stack pointer. The sniff will not be + * called again on the current file until the returned stack + * pointer is reached. Return (count($tokens) + 1) to skip + * the rest of the file. + */ + public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) + { + // Skip all files except the license file + if (substr($phpcsFile->getFilename(), -10) !== $this->licenseFile->getFilename()) { + return ($phpcsFile->numTokens + 1); + } + + if (! $this->licenseFile->getRealPath()) { + $error = 'Missing LICENSE.md file in the component root dir'; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingLicense'); + if ($fix === true) { + LicenseUtils::createLicenseFile(); + } + } + + // TODO: Check copyright year + + // Ignore the rest of the file. + return ($phpcsFile->numTokens + 1); + } +} diff --git a/src/ZendCodingStandard/Utils/LicenseUtils.php b/src/ZendCodingStandard/Utils/LicenseUtils.php new file mode 100644 index 00000000..d85e3d48 --- /dev/null +++ b/src/ZendCodingStandard/Utils/LicenseUtils.php @@ -0,0 +1,129 @@ +getRealPath()) { + // File already exists + return; + } + + // Create new file + $file = $file->openFile('x'); + $file->fwrite(sprintf(self::$copyright, gmdate('Y'))); + } + + public static function getLicenseFile() + { + return new SplFileInfo('LICENSE.md'); + } + + public static function createLicenseFile(SplFileInfo $file = null) + { + if ($file === null) { + $file = self::getLicenseFile(); + } + + if ($file->getRealPath()) { + // File already exists + return; + } + + // Create new file + $file = $file->openFile('x'); + $file->fwrite(sprintf(self::$license, gmdate('Y'))); + } + + public static function updateCopyright(SplFileInfo $file, $firstYear, $lastYear = null) + { + if ($lastYear === null) { + $lastYear = gmdate('Y'); + } + + $copyrightDateRange = $lastYear; + if ($firstYear !== $lastYear) { + $copyrightDateRange = sprintf('%s-%s', $firstYear, $lastYear); + } + + // Update copyright line; first line in the file + $content = file($file->getRealPath()); + $content[0] = sprintf(self::$copyrightLine . PHP_EOL, $copyrightDateRange); + file_put_contents($file->getRealPath(), $content); + } + + public static function getCopyrightFirstYear(SplFileInfo $file) + { + if (! $file->getRealPath()) { + return null; + } + + $content = file($file->getRealPath()); + $matches = []; + preg_match('|(?[\d]{4})(-(?[\d]{4}))?|', $content[0], $matches); + + $licenseFirstYear = isset($matches['start']) ? $matches['start'] : null; + $licenseLastYear = isset($matches['end']) ? $matches['end'] : null; + + return $licenseFirstYear; + } +} diff --git a/src/ZendCodingStandard/ruleset.xml b/src/ZendCodingStandard/ruleset.xml new file mode 100644 index 00000000..a3ecb1e0 --- /dev/null +++ b/src/ZendCodingStandard/ruleset.xml @@ -0,0 +1,30 @@ + + + Zend Framework Coding Standard + + + + + Sniffs/Files/LICENSE.md + Sniffs/Files/COPYING.md + + + + + + + + + + + + + + + + + + + + + diff --git a/test/SniffTestCase.php b/test/SniffTestCase.php new file mode 100644 index 00000000..ce85c18f --- /dev/null +++ b/test/SniffTestCase.php @@ -0,0 +1,150 @@ +cli->getDefaults(), [ + 'encoding' => 'utf-8', + 'files' => [$asset], + 'standard' => $standard, + 'showSources' => true, + ]); + + $reflection = new \ReflectionProperty($phpcs->cli, 'values'); + $reflection->setAccessible(true); + $reflection->setValue($phpcs->cli, $options); + + $phpcs->initStandard($standard, ['ZendCodingStandard.Commenting.FileLevelDocBlock']); + $phpcs->setIgnorePatterns([]); + + return $phpcs->processFile($asset); + } + + public function assertErrorCount($expectedCount, \PHP_CodeSniffer_File $file) + { + if (! \is_int($expectedCount)) { + throw InvalidArgumentHelper::factory(1, 'integer'); + } + + $message = sprintf( + 'Failed asserting that "%s" has %d violations.', + str_replace(__DIR__, 'test', $file->getFilename()), + $expectedCount + ); + + static::assertThat( + $file->getErrorCount(), + new IsEqual($expectedCount), + $message + ); + } + + public function assertHasError($expectedError, \PHP_CodeSniffer_File $file) + { + foreach ($file->getErrors() as $line => $lineErrors) { + foreach ($lineErrors as $column => $errors) { + foreach ($errors as $error) { + if (isset($error['source']) && $error['source'] === $expectedError) { + static::assertThat(true, static::isTrue()); + + return; + } + } + } + } + + $message = sprintf( + 'Failed asserting that "%s" has "%s" error.', + str_replace(__DIR__, 'test', $file->getFilename()), + $expectedError + ); + + static::assertThat(false, static::isTrue(), $message); + } + + public function assertWarningCount($expectedCount, \PHP_CodeSniffer_File $file) + { + if (! \is_int($expectedCount)) { + throw InvalidArgumentHelper::factory(1, 'integer'); + } + + $message = sprintf( + 'Failed asserting that "%s" has %d warnings.', + str_replace(__DIR__, 'test', $file->getFilename()), + $expectedCount + ); + + static::assertThat( + $file->getWarningCount(), + new IsEqual($expectedCount), + $message + ); + } + + public function assertHasWarning($expectedError, \PHP_CodeSniffer_File $file) + { + foreach ($file->getWarnings() as $line => $lineErrors) { + foreach ($lineErrors as $column => $errors) { + foreach ($errors as $error) { + if (isset($error['source']) && $error['source'] === $expectedError) { + static::assertThat(true, static::isTrue()); + + return; + } + } + } + } + + $message = sprintf( + 'Failed asserting that "%s" has "%s" warning.', + str_replace(__DIR__, 'test', $file->getFilename()), + $expectedError + ); + + static::assertThat(false, static::isTrue(), $message); + } + + public function assertAssetCanBeFixed($fixed, \PHP_CodeSniffer_File $file) + { + if ($fixed === null) { + $message = sprintf( + 'Failed asserting that "%s" has no fixable violations.', + str_replace(__DIR__, 'test', $file->getFilename()) + ); + $this->assertEquals(0, $file->getFixableCount(), $message); + + return; + } + + // Try to fix the file + $file->fixer->fixFile(); + $message = sprintf( + 'Failed to fix %d fixable violations in "%s".', + $file->getFixableCount(), + str_replace(__DIR__, 'test', $file->getFilename()) + ); + $this->assertEquals(0, $file->getFixableCount(), $message); + + // Validate fixes + $message = sprintf( + 'Failed asserting that "%s" has all fixable violations fixed.', + str_replace(__DIR__, 'test', $file->getFilename()) + ); + $this->assertEquals('', trim($file->fixer->generateDiff($fixed)), $message); + } +} diff --git a/test/Sniffs/Commenting/Assets/FileLevelDocBlockEmptyTags.php b/test/Sniffs/Commenting/Assets/FileLevelDocBlockEmptyTags.php new file mode 100644 index 00000000..d937bef1 --- /dev/null +++ b/test/Sniffs/Commenting/Assets/FileLevelDocBlockEmptyTags.php @@ -0,0 +1,12 @@ +processAsset($asset); + + $this->assertErrorCount($errorCount, $result); + $this->assertWarningCount($warningCount, $result); + + foreach ($errors as $error) { + $this->assertHasError($error, $result); + } + + foreach ($warnings as $warning) { + $this->assertHasWarning($warning, $result); + } + + $this->assertAssetCanBeFixed($fixed, $result); + } + + public function assetsProvider() + { + return [ + 'FileLevelDocBlockPass' => [ + 'asset' => __DIR__ . '/Assets/FileLevelDocBlockPass.php', + 'fixed' => null, + 'errorCount' => 0, + 'errors' => [], + 'warningCount' => 0, + 'warnings' => [], + ], + 'FileLevelDocBlockMissing' => [ + 'asset' => __DIR__ . '/Assets/FileLevelDocBlockMissing.php', + 'fixed' => null, + 'errorCount' => 1, + 'errors' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.Missing', + ], + 'warningCount' => 0, + 'warnings' => [], + ], + 'FileLevelDocBlockSpacingAfterOpen' => [ + 'asset' => __DIR__ . '/Assets/FileLevelDocBlockSpacingAfterOpen.php', + 'fixed' => null, + 'errorCount' => 1, + 'errors' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.SpacingAfterOpen', + ], + 'warningCount' => 0, + 'warnings' => [], + ], + 'FileLevelDocBlockIncorrectSourceLink' => [ + 'asset' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectSourceLink.php', + 'fixed' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectSourceLink.fixed.php', + 'errorCount' => 1, + 'errors' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.IncorrectSourceLink', + ], + 'warningCount' => 0, + 'warnings' => [], + ], + 'FileLevelDocBlockIncorrectCopyrightLink' => [ + 'asset' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectCopyrightLink.php', + 'fixed' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectCopyrightLink.fixed.php', + 'errorCount' => 1, + 'errors' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.IncorrectCopyrightLink', + ], + 'warningCount' => 0, + 'warnings' => [], + ], + 'FileLevelDocBlockIncorrectLicenseLink' => [ + 'asset' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectLicenseLink.php', + 'fixed' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectLicenseLink.fixed.php', + 'errorCount' => 1, + 'errors' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.IncorrectLicenseLink', + ], + 'warningCount' => 0, + 'warnings' => [], + ], + 'FileLevelDocBlockEmptyTags' => [ + 'asset' => __DIR__ . '/Assets/FileLevelDocBlockEmptyTags.php', + 'fixed' => null, + 'errorCount' => 3, + 'errors' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.EmptySeeTag', + 'ZendCodingStandard.Commenting.FileLevelDocBlock.EmptyCopyrightTag', + 'ZendCodingStandard.Commenting.FileLevelDocBlock.EmptyLicenseTag', + ], + 'warningCount' => 0, + 'warnings' => [], + ], + ]; + } +} diff --git a/test/Util/LicenseUtilTest.php b/test/Util/LicenseUtilTest.php new file mode 100644 index 00000000..a5efafba --- /dev/null +++ b/test/Util/LicenseUtilTest.php @@ -0,0 +1,53 @@ +root = vfsStream::setup('tmp'); + } + + public function testCopyrightFileIsCreated() + { + LicenseUtils::createCopyrightFile( + new SplFileInfo($this->root->url() . '/COPYING.tmp.md') + ); + + $this->assertTrue($this->root->hasChild('COPYING.tmp.md')); + $this->assertEquals( + sprintf(LicenseUtils::$copyright, gmdate('Y')), + $this->root->getChild('COPYING.tmp.md')->getContent() + ); + } + + public function testLicenseFileIsCreated() + { + LicenseUtils::createLicenseFile( + new SplFileInfo($this->root->url() . '/LICENSE.tmp.md') + ); + + $this->assertTrue($this->root->hasChild('LICENSE.tmp.md')); + $this->assertEquals( + sprintf(LicenseUtils::$license, gmdate('Y')), + $this->root->getChild('LICENSE.tmp.md')->getContent() + ); + } +} From 841e6bf941b5f8d8c8d0476263e8383f6d293b26 Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Wed, 12 Apr 2017 18:07:49 +0200 Subject: [PATCH 02/25] Add sniff + fix for invalid date in COPYING.md and LICENSE.md --- .../Sniffs/Files/CopyingSniff.php | 26 +++++++++++++++---- .../Sniffs/Files/LicenseSniff.php | 18 ++++++++++++- src/ZendCodingStandard/Utils/LicenseUtils.php | 16 ++++++++++++ 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/ZendCodingStandard/Sniffs/Files/CopyingSniff.php b/src/ZendCodingStandard/Sniffs/Files/CopyingSniff.php index 00a605a3..9fd0c01d 100644 --- a/src/ZendCodingStandard/Sniffs/Files/CopyingSniff.php +++ b/src/ZendCodingStandard/Sniffs/Files/CopyingSniff.php @@ -32,10 +32,10 @@ public function register() * found. * * @param \PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the - * token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer - * file's token stack where the token - * was found. + * token was found. + * @param int $stackPtr The position in the PHP_CodeSniffer + * file's token stack where the token + * was found. * * @return int Optionally returns a stack pointer. The sniff will not be * called again on the current file until the returned stack @@ -55,9 +55,25 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($fix === true) { LicenseUtils::createCopyrightFile(); } + + // Ignore the rest of the file. + return ($phpcsFile->numTokens + 1); + } + + // Get copyright year + list($firstYear, $lastYear) = LicenseUtils::getCopyrightDate($this->copyrightFile); + if (! $lastYear) { + $lastYear = $firstYear; } - // TODO: Check copyright year + // Check copyright year + if ($lastYear !== gmdate('Y')) { + $error = 'Invalid copyright date in COPYING.md'; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'InvalidCopyrightDate'); + if ($fix === true) { + LicenseUtils::updateCopyright($this->copyrightFile, $firstYear); + } + } // Ignore the rest of the file. return ($phpcsFile->numTokens + 1); diff --git a/src/ZendCodingStandard/Sniffs/Files/LicenseSniff.php b/src/ZendCodingStandard/Sniffs/Files/LicenseSniff.php index 4bbbddf3..fcb382e1 100644 --- a/src/ZendCodingStandard/Sniffs/Files/LicenseSniff.php +++ b/src/ZendCodingStandard/Sniffs/Files/LicenseSniff.php @@ -55,9 +55,25 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) if ($fix === true) { LicenseUtils::createLicenseFile(); } + + // Ignore the rest of the file. + return ($phpcsFile->numTokens + 1); + } + + // Get copyright year + list($firstYear, $lastYear) = LicenseUtils::getCopyrightDate($this->licenseFile); + if (! $lastYear) { + $lastYear = $firstYear; } - // TODO: Check copyright year + // Check copyright year + if ($lastYear !== gmdate('Y')) { + $error = 'Invalid copyright date in COPYING.md'; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'InvalidCopyrightDate'); + if ($fix === true) { + LicenseUtils::updateCopyright($this->licenseFile, $firstYear); + } + } // Ignore the rest of the file. return ($phpcsFile->numTokens + 1); diff --git a/src/ZendCodingStandard/Utils/LicenseUtils.php b/src/ZendCodingStandard/Utils/LicenseUtils.php index d85e3d48..f237d1a4 100644 --- a/src/ZendCodingStandard/Utils/LicenseUtils.php +++ b/src/ZendCodingStandard/Utils/LicenseUtils.php @@ -126,4 +126,20 @@ public static function getCopyrightFirstYear(SplFileInfo $file) return $licenseFirstYear; } + + public static function getCopyrightDate(SplFileInfo $file) + { + if (! $file->getRealPath()) { + return [null, null]; + } + + $content = file($file->getRealPath()); + $matches = []; + preg_match('|(?[\d]{4})(-(?[\d]{4}))?|', $content[0], $matches); + + $licenseFirstYear = isset($matches['start']) ? $matches['start'] : null; + $licenseLastYear = isset($matches['end']) ? $matches['end'] : null; + + return [$licenseFirstYear, $licenseLastYear]; + } } From 297d314bc3bf457ad0432aff9cc6422df81416e7 Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Wed, 12 Apr 2017 22:33:01 +0200 Subject: [PATCH 03/25] Update Travis-CI config - Add php 5.6 and 7.1 to the matrix - Use lowest/locked/latest strategy - Enable notifications for IRC and Slack - Generate and upload code coverage per @weierophinney --- .travis.yml | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index e7dd135e..8add18e6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,24 +7,66 @@ cache: - $HOME/.composer/cache - vendor -env: - global: - - COMPOSER_ARGS="--no-interaction" - matrix: include: + - php: 5.6 + env: + - DEPS=lowest + - php: 5.6 + env: + - DEPS=locked + - TEST_COVERAGE=true + - php: 5.6 + env: + - DEPS=latest + - php: 7 + env: + - DEPS=lowest - php: 7 + env: + - DEPS=locked + - php: 7 + env: + - DEPS=latest + - php: 7.1 + env: + - DEPS=lowest + - php: 7.1 + env: + - DEPS=locked + - php: 7.1 + env: + - DEPS=latest before_install: - if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi + - travis_retry composer self-update install: - travis_retry composer install $COMPOSER_ARGS --ignore-platform-reqs + - if [[ $TRAVIS_PHP_VERSION =~ ^5.6 ]]; then travis_retry composer update $COMPOSER_ARGS --with-dependencies $LEGACY_DEPS ; fi + - if [[ $DEPS == 'latest' ]]; then travis_retry composer update $COMPOSER_ARGS ; fi + - if [[ $DEPS == 'lowest' ]]; then travis_retry composer update --prefer-lowest --prefer-stable $COMPOSER_ARGS ; fi + - if [[ $TEST_COVERAGE == 'true' ]]; then travis_retry composer require --dev $COMPOSER_ARGS $COVERAGE_DEPS ; fi - composer show script: + - if [[ $TEST_COVERAGE == 'true' ]]; then composer test-coverage ; else composer test ; fi - vendor/bin/phpcs -s - vendor/bin/phpunit +after_script: + - if [[ $TEST_COVERAGE == 'true' ]]; then composer upload-coverage ; fi + notifications: email: false + irc: + channels: + - "irc.freenode.org#zftalk.dev" + on_success: change + on_failure: always + slack: + rooms: + - secure: "fh+J7c7A9f7Sje2h9M00mw9UyeKQ2l1FyGvOwQ50CPHvf0u7bb1OV3sEqTukiwSNEQNmJ5C3QuJpaxkIeDjI8LJpeNderWeu6NH2O5OedSEElHmc7RsBygfiHM05hWnv10ddDxJ+YtmuNjpkXIoXcBdHby+eRBJ09YStVhnIwQakBbKBH7Idlitn23QYl4VZeA3jTcGsHhCtGjpjDt4sohs/RJWgGfAYTSKcjSLdFWWdg2G8PRPKTyQkR+nFd92lvVeRteg0VzxGJqKXoeJP3B0WYB7emQJho+ly4DZFkL+wJZPtcEHCi/ne9l/OaVy6XGhbiDVXxxpyexD4cmGySdjpsYirXqxjS6V8kfWn3JVbCxipI518zJq5Rb3JOCVcdoo7P/xXqYj+fihMbGfxBorUqwm8uBlCblRGXJ1QcVRsi52u0zMJN+QQ/gYHf0gBoF9IbYvQDeshZT80TnYUy2/om/j9xTUfZMdEKTRQWrj9LbsNKY619gZt2u/b2tbcWdjFiPA/Uxa2VjphHz8LFuHaU43/km4swLhthkPowdZLozuyJjksbJcjH7izh3Hxd8oRxBFrqdfuPgh3owQyW75wvG/TGWnNXMqO66OuXXaJKAomBnVBjGB6DBo3TnPzEyKUkLi54AVQIfaDPqQH8yec5ugogOYrrQDLIT1kdVY=" + on_success: change + on_failure: always From e63eef63cb120e97dd2dea99ca9697426a0e9f49 Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Wed, 12 Apr 2017 23:08:05 +0200 Subject: [PATCH 04/25] Add missing composer scripts --- .travis.yml | 3 +-- composer.json | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8add18e6..d53bba5f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,8 +52,7 @@ install: script: - if [[ $TEST_COVERAGE == 'true' ]]; then composer test-coverage ; else composer test ; fi - - vendor/bin/phpcs -s - - vendor/bin/phpunit + - composer cs-check after_script: - if [[ $TEST_COVERAGE == 'true' ]]; then composer upload-coverage ; fi diff --git a/composer.json b/composer.json index b9aee0a9..d459b515 100644 --- a/composer.json +++ b/composer.json @@ -7,11 +7,11 @@ "coding standard" ], "require": { - "squizlabs/php_codesniffer": "^2.7" + "squizlabs/php_codesniffer": "^2.8" }, "require-dev": { "mikey179/vfsStream": "^1.6", - "phpunit/phpunit": "^6.1" + "phpunit/phpunit": "^6.1 || ^5.7.15" }, "autoload": { "psr-4": { @@ -22,5 +22,16 @@ "psr-4": { "ZendTest\\CodingStandard\\": "test/" } + }, + "scripts": { + "check": [ + "@cs-check", + "@test" + ], + "cs-check": "phpcs -s", + "cs-fix": "phpcbf", + "test": "phpunit --colors=always", + "test-coverage": "phpunit --coverage-clover clover.xml", + "upload-coverage": "coveralls -v" } } From eab40db514005a50f2acf8ff18903601b72e1394 Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Wed, 12 Apr 2017 23:56:11 +0200 Subject: [PATCH 05/25] Improve consistency, formatting and docblocks per @weierophinney --- composer.json | 1 + .../Commenting/FileLevelDocBlockSniff.php | 155 +++++++++++------- .../Sniffs/Files/COPYING.md | 5 +- .../Sniffs/Files/CopyingSniff.php | 27 +-- .../Sniffs/Files/LicenseSniff.php | 30 ++-- src/ZendCodingStandard/Utils/LicenseUtils.php | 51 ++++-- .../Commenting/FileLevelDocBlockSniffTest.php | 18 +- 7 files changed, 184 insertions(+), 103 deletions(-) diff --git a/composer.json b/composer.json index d459b515..4c6bad50 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,7 @@ "coding standard" ], "require": { + "php": "^5.6 || ^7.0", "squizlabs/php_codesniffer": "^2.8" }, "require-dev": { diff --git a/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php index 92a3ed5d..a6bc7275 100644 --- a/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php +++ b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php @@ -5,38 +5,86 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ +use PHP_CodeSniffer_File as File; +use PHP_CodeSniffer_Sniff as Sniff; use Zend\CodingStandard\Utils\LicenseUtils; -class ZendCodingStandard_Sniffs_Commenting_FileLevelDocBlockSniff implements \PHP_CodeSniffer_Sniff +/** + * FileLevelDocBlock Sniff + * + * - Checks if a file has a valid file-level docblock + * - Checks for missing/invalid see tag + * - Checks for missing/invalid copyright tag + * - Checks for missing/invalid license tag + * - Checks order of see, copyright and license tags + */ +class ZendCodingStandard_Sniffs_Commenting_FileLevelDocBlockSniff implements Sniff { + /** + * @var string + */ private $repo; + /** + * @var string + */ private $licenseFirstYear; + /** + * @var string + */ private $licenseLastYear; - private $copyrightChanged; + /** + * @var boolean + */ + private $copyrightChanged = false; - private $fix; + /** + * @var boolean + */ + private $fix = false; + + const IGNORE = [ + T_CLASS, + T_INTERFACE, + T_TRAIT, + T_FUNCTION, + T_CLOSURE, + T_PUBLIC, + T_PRIVATE, + T_PROTECTED, + T_FINAL, + T_STATIC, + T_ABSTRACT, + T_CONST, + T_PROPERTY, + T_INCLUDE, + T_INCLUDE_ONCE, + T_REQUIRE, + T_REQUIRE_ONCE, + ]; public function __construct() { $this->licenseFirstYear = gmdate('Y'); $this->licenseLastYear = gmdate('Y'); - $year = LicenseUtils::getCopyrightFirstYear(LicenseUtils::getCopyrightFile()); - if ($year !== null && $year < $this->licenseFirstYear) { - $this->licenseFirstYear = $year; + // Detect copyright year in copying file + list($firstYear, $lastYear) = LicenseUtils::getCopyrightDate(LicenseUtils::getCopyrightFile()); + if ($firstYear !== null && $firstYear < $this->licenseFirstYear) { + $this->licenseFirstYear = $firstYear; } - $year = LicenseUtils::getCopyrightFirstYear(LicenseUtils::getLicenseFile()); - if ($year !== null && $year < $this->licenseFirstYear) { - $this->licenseFirstYear = $year; + // Detect copyright year in license file + list($firstYear, $lastYear) = LicenseUtils::getCopyrightDate(LicenseUtils::getLicenseFile()); + if ($firstYear !== null && $firstYear < $this->licenseFirstYear) { + $this->licenseFirstYear = $firstYear; } + // Get current repo name from composer.json $content = file_get_contents('composer.json'); $content = json_decode($content, true); - $this->repo = $content['name']; } @@ -44,7 +92,6 @@ public function __construct() * Registers the tokens that this sniff wants to listen for. * * @return int[] - * @see Tokens.php */ public function register() { @@ -55,18 +102,15 @@ public function register() * Called when one of the token types that this sniff is listening for is * found. * - * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where the - * token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer - * file's token stack where the token - * was found. + * @param File $phpcsFile The PHP_CodeSniffer file where the token was found. + * @param int $stackPtr The position in the PHP_CodeSniffer file's token + * stack where the token was found. * * @return int Optionally returns a stack pointer. The sniff will not be - * called again on the current file until the returned stack - * pointer is reached. Return (count($tokens) + 1) to skip - * the rest of the file. + * called again on the current file until the returned stack + * pointer is reached. */ - public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + public function process(File $phpcsFile, $stackPtr) { // Skip license file if (in_array(substr($phpcsFile->getFilename(), -10), ['LICENSE.md', 'COPYING.md'])) { @@ -103,27 +147,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) true ); - $ignore = [ - T_CLASS, - T_INTERFACE, - T_TRAIT, - T_FUNCTION, - T_CLOSURE, - T_PUBLIC, - T_PRIVATE, - T_PROTECTED, - T_FINAL, - T_STATIC, - T_ABSTRACT, - T_CONST, - T_PROPERTY, - T_INCLUDE, - T_INCLUDE_ONCE, - T_REQUIRE, - T_REQUIRE_ONCE, - ]; - - if (in_array($tokens[$nextToken]['code'], $ignore) === true) { + if (in_array($tokens[$nextToken]['code'], self::IGNORE) === true) { $phpcsFile->addError('Missing file-level DocBlock', $stackPtr, 'Missing'); $phpcsFile->recordMetric($stackPtr, 'File has file-level DocBlock', 'no'); @@ -251,31 +275,23 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) } if ($this->copyrightChanged === true && $this->fix === true) { - // Make sure the files exist - LicenseUtils::createCopyrightFile(); - LicenseUtils::createLicenseFile(); - - // Update copyright file - LicenseUtils::updateCopyright( + $this->updateCopyrightLines( LicenseUtils::getCopyrightFile(), - $this->licenseFirstYear, - $this->licenseLastYear - ); - - // Update license file - LicenseUtils::updateCopyright( - LicenseUtils::getLicenseFile(), - $this->licenseFirstYear, - $this->licenseLastYear + LicenseUtils::getLicenseFile() ); - - $this->copyrightChanged = false; } // Ignore the rest of the file. return ($phpcsFile->numTokens + 1); } + /** + * Parse copyright line + * + * Detect year range and update the current first and last years if needed. + * + * @param $string + */ private function parseCopyrightDate($string) { $matches = []; @@ -292,4 +308,27 @@ private function parseCopyrightDate($string) $this->copyrightChanged = true; } } + + private function updateCopyrightLines(SplFileInfo $copyrightFile, SplFileInfo $licenseFile) + { + // Make sure the files exist + LicenseUtils::createCopyrightFile($copyrightFile); + LicenseUtils::createLicenseFile($licenseFile); + + // Update copyright file + LicenseUtils::updateCopyright( + $copyrightFile, + $this->licenseFirstYear, + $this->licenseLastYear + ); + + // Update license file + LicenseUtils::updateCopyright( + $licenseFile, + $this->licenseFirstYear, + $this->licenseLastYear + ); + + $this->copyrightChanged = false; + } } diff --git a/src/ZendCodingStandard/Sniffs/Files/COPYING.md b/src/ZendCodingStandard/Sniffs/Files/COPYING.md index 785a8ad1..2ba48f48 100644 --- a/src/ZendCodingStandard/Sniffs/Files/COPYING.md +++ b/src/ZendCodingStandard/Sniffs/Files/COPYING.md @@ -1,6 +1,9 @@ getFilename(), -10) !== $this->licenseFile->getFilename()) { diff --git a/src/ZendCodingStandard/Utils/LicenseUtils.php b/src/ZendCodingStandard/Utils/LicenseUtils.php index f237d1a4..569c0294 100644 --- a/src/ZendCodingStandard/Utils/LicenseUtils.php +++ b/src/ZendCodingStandard/Utils/LicenseUtils.php @@ -9,6 +9,9 @@ use SplFileInfo; +/** + * License Utility class + */ class LicenseUtils { public static $copyrightLine = 'Copyright (c) %s, Zend Technologies USA, Inc.'; @@ -52,11 +55,21 @@ class LicenseUtils EOF; + /** + * Get COPYING.md as an object + * + * @return SplFileInfo + */ public static function getCopyrightFile() { return new SplFileInfo('COPYING.md'); } + /** + * Create a new copyright file if it does not exist + * + * @param SplFileInfo|null $file + */ public static function createCopyrightFile(SplFileInfo $file = null) { if ($file === null) { @@ -73,11 +86,21 @@ public static function createCopyrightFile(SplFileInfo $file = null) $file->fwrite(sprintf(self::$copyright, gmdate('Y'))); } + /** + * Get COPYING.md as an object + * + * @return SplFileInfo + */ public static function getLicenseFile() { return new SplFileInfo('LICENSE.md'); } + /** + * Create a new license file if it does not exist + * + * @param SplFileInfo|null $file + */ public static function createLicenseFile(SplFileInfo $file = null) { if ($file === null) { @@ -94,6 +117,12 @@ public static function createLicenseFile(SplFileInfo $file = null) $file->fwrite(sprintf(self::$license, gmdate('Y'))); } + /** + * Update the copyright line + * The copyright line must be the first one in the file. + * + * @param SplFileInfo|null $file + */ public static function updateCopyright(SplFileInfo $file, $firstYear, $lastYear = null) { if ($lastYear === null) { @@ -111,22 +140,12 @@ public static function updateCopyright(SplFileInfo $file, $firstYear, $lastYear file_put_contents($file->getRealPath(), $content); } - public static function getCopyrightFirstYear(SplFileInfo $file) - { - if (! $file->getRealPath()) { - return null; - } - - $content = file($file->getRealPath()); - $matches = []; - preg_match('|(?[\d]{4})(-(?[\d]{4}))?|', $content[0], $matches); - - $licenseFirstYear = isset($matches['start']) ? $matches['start'] : null; - $licenseLastYear = isset($matches['end']) ? $matches['end'] : null; - - return $licenseFirstYear; - } - + /** + * Detect the copyright date range from the first line in a file + * + * @param SplFileInfo $file + * @return array[string|null,string|null] + */ public static function getCopyrightDate(SplFileInfo $file) { if (! $file->getRealPath()) { diff --git a/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php b/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php index a8e53d52..456c1279 100644 --- a/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php +++ b/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php @@ -35,7 +35,7 @@ public function testAssets($asset, $fixed, $errorCount, array $errors, $warningC public function assetsProvider() { return [ - 'FileLevelDocBlockPass' => [ + 'FileLevelDocBlockPass' => [ 'asset' => __DIR__ . '/Assets/FileLevelDocBlockPass.php', 'fixed' => null, 'errorCount' => 0, @@ -43,7 +43,8 @@ public function assetsProvider() 'warningCount' => 0, 'warnings' => [], ], - 'FileLevelDocBlockMissing' => [ + + 'FileLevelDocBlockMissing' => [ 'asset' => __DIR__ . '/Assets/FileLevelDocBlockMissing.php', 'fixed' => null, 'errorCount' => 1, @@ -53,7 +54,8 @@ public function assetsProvider() 'warningCount' => 0, 'warnings' => [], ], - 'FileLevelDocBlockSpacingAfterOpen' => [ + + 'FileLevelDocBlockSpacingAfterOpen' => [ 'asset' => __DIR__ . '/Assets/FileLevelDocBlockSpacingAfterOpen.php', 'fixed' => null, 'errorCount' => 1, @@ -63,7 +65,8 @@ public function assetsProvider() 'warningCount' => 0, 'warnings' => [], ], - 'FileLevelDocBlockIncorrectSourceLink' => [ + + 'FileLevelDocBlockIncorrectSourceLink' => [ 'asset' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectSourceLink.php', 'fixed' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectSourceLink.fixed.php', 'errorCount' => 1, @@ -73,6 +76,7 @@ public function assetsProvider() 'warningCount' => 0, 'warnings' => [], ], + 'FileLevelDocBlockIncorrectCopyrightLink' => [ 'asset' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectCopyrightLink.php', 'fixed' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectCopyrightLink.fixed.php', @@ -83,7 +87,8 @@ public function assetsProvider() 'warningCount' => 0, 'warnings' => [], ], - 'FileLevelDocBlockIncorrectLicenseLink' => [ + + 'FileLevelDocBlockIncorrectLicenseLink' => [ 'asset' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectLicenseLink.php', 'fixed' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectLicenseLink.fixed.php', 'errorCount' => 1, @@ -93,7 +98,8 @@ public function assetsProvider() 'warningCount' => 0, 'warnings' => [], ], - 'FileLevelDocBlockEmptyTags' => [ + + 'FileLevelDocBlockEmptyTags' => [ 'asset' => __DIR__ . '/Assets/FileLevelDocBlockEmptyTags.php', 'fixed' => null, 'errorCount' => 3, From a2159ca2409a50556abf6ac7dd0004306929e2dc Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Thu, 13 Apr 2017 00:14:48 +0200 Subject: [PATCH 06/25] Use standard phpunit assertions for backwards compatibility --- test/SniffTestCase.php | 46 +++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/test/SniffTestCase.php b/test/SniffTestCase.php index ce85c18f..02b8fad7 100644 --- a/test/SniffTestCase.php +++ b/test/SniffTestCase.php @@ -7,15 +7,17 @@ namespace ZendTest\CodingStandard; -use PHPUnit\Framework\Constraint\IsEqual; +use PHP_CodeSniffer; +use PHP_CodeSniffer_File as File; use PHPUnit\Framework\TestCase; use PHPUnit\Util\InvalidArgumentHelper; +use ReflectionProperty; class SniffTestCase extends TestCase { public function processAsset($asset) { - $phpcs = new \PHP_CodeSniffer(); + $phpcs = new PHP_CodeSniffer(); $standard = 'src/ZendCodingStandard/ruleset.xml'; $options = array_merge($phpcs->cli->getDefaults(), [ @@ -25,7 +27,7 @@ public function processAsset($asset) 'showSources' => true, ]); - $reflection = new \ReflectionProperty($phpcs->cli, 'values'); + $reflection = new ReflectionProperty($phpcs->cli, 'values'); $reflection->setAccessible(true); $reflection->setValue($phpcs->cli, $options); @@ -35,9 +37,9 @@ public function processAsset($asset) return $phpcs->processFile($asset); } - public function assertErrorCount($expectedCount, \PHP_CodeSniffer_File $file) + public function assertErrorCount($expectedCount, File $file) { - if (! \is_int($expectedCount)) { + if (! is_int($expectedCount)) { throw InvalidArgumentHelper::factory(1, 'integer'); } @@ -46,21 +48,16 @@ public function assertErrorCount($expectedCount, \PHP_CodeSniffer_File $file) str_replace(__DIR__, 'test', $file->getFilename()), $expectedCount ); - - static::assertThat( - $file->getErrorCount(), - new IsEqual($expectedCount), - $message - ); + $this->assertEquals($expectedCount, $file->getErrorCount(), $message); } - public function assertHasError($expectedError, \PHP_CodeSniffer_File $file) + public function assertHasError($expectedError, File $file) { foreach ($file->getErrors() as $line => $lineErrors) { foreach ($lineErrors as $column => $errors) { foreach ($errors as $error) { if (isset($error['source']) && $error['source'] === $expectedError) { - static::assertThat(true, static::isTrue()); + $this->assertTrue(true); return; } @@ -73,13 +70,12 @@ public function assertHasError($expectedError, \PHP_CodeSniffer_File $file) str_replace(__DIR__, 'test', $file->getFilename()), $expectedError ); - - static::assertThat(false, static::isTrue(), $message); + $this->assertTrue(false, $message); } - public function assertWarningCount($expectedCount, \PHP_CodeSniffer_File $file) + public function assertWarningCount($expectedCount, File $file) { - if (! \is_int($expectedCount)) { + if (! is_int($expectedCount)) { throw InvalidArgumentHelper::factory(1, 'integer'); } @@ -88,21 +84,16 @@ public function assertWarningCount($expectedCount, \PHP_CodeSniffer_File $file) str_replace(__DIR__, 'test', $file->getFilename()), $expectedCount ); - - static::assertThat( - $file->getWarningCount(), - new IsEqual($expectedCount), - $message - ); + $this->assertEquals($expectedCount, $file->getWarningCount(), $message); } - public function assertHasWarning($expectedError, \PHP_CodeSniffer_File $file) + public function assertHasWarning($expectedError, File $file) { foreach ($file->getWarnings() as $line => $lineErrors) { foreach ($lineErrors as $column => $errors) { foreach ($errors as $error) { if (isset($error['source']) && $error['source'] === $expectedError) { - static::assertThat(true, static::isTrue()); + $this->assertTrue(true); return; } @@ -115,11 +106,10 @@ public function assertHasWarning($expectedError, \PHP_CodeSniffer_File $file) str_replace(__DIR__, 'test', $file->getFilename()), $expectedError ); - - static::assertThat(false, static::isTrue(), $message); + $this->assertTrue(false, $message); } - public function assertAssetCanBeFixed($fixed, \PHP_CodeSniffer_File $file) + public function assertAssetCanBeFixed($fixed, File $file) { if ($fixed === null) { $message = sprintf( From 7575bca67bdff5ad6159f2ab43cd5a77cdc4b382 Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Thu, 13 Apr 2017 00:20:37 +0200 Subject: [PATCH 07/25] Add missing travis-ci global env's --- .travis.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.travis.yml b/.travis.yml index d53bba5f..438fe38a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,12 @@ cache: - $HOME/.composer/cache - vendor +env: + global: + - COMPOSER_ARGS="--no-interaction" + - COVERAGE_DEPS="satooshi/php-coveralls" + - LEGACY_DEPS="phpunit/phpunit" + matrix: include: - php: 5.6 From 40d8951b3fbc3652a60383338e28da0ce62e2af1 Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Thu, 13 Apr 2017 00:30:35 +0200 Subject: [PATCH 08/25] Fix location of ZendCodingStandard ruleset --- ruleset.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruleset.xml b/ruleset.xml index 11bdacda..2ee7b3ea 100644 --- a/ruleset.xml +++ b/ruleset.xml @@ -1,5 +1,5 @@ Zend Framework Coding Standard - + From 189a37b1e7a03cfe061e950b8c5a08b50e6cfeae Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Thu, 13 Apr 2017 09:37:06 +0200 Subject: [PATCH 09/25] Fix code coverage whitelist --- phpunit.xml.dist | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index dc917472..d5ec939c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -10,9 +10,8 @@ - - src/ - test/Util/ + + src From 7b5e15507337f45c50421c95de56a8d067c16750 Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Thu, 13 Apr 2017 09:47:47 +0200 Subject: [PATCH 10/25] Enable coveralls --- .coveralls.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .coveralls.yml diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 00000000..bc71b62f --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1,2 @@ +coverage_clover: clover.xml +json_path: coveralls-upload.json From d993b37e28beb5b4a492eb0472976bb73d9e92cf Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Thu, 13 Apr 2017 20:37:56 +0200 Subject: [PATCH 11/25] Enforce checking and updating license and copying file The copyright dates where not correctly updated and were also out of sync in some cases. If a valid copyright date range is detected in a source code file, both files are now checked and updated if needed. Also before one of the files is updated, the other file is checked as well to calculate the correct date range and make sure they are in sync. --- .../Commenting/FileLevelDocBlockSniff.php | 99 +----------- .../Sniffs/Files/CopyingSniff.php | 22 +-- .../Sniffs/Files/LicenseSniff.php | 24 +-- src/ZendCodingStandard/Utils/LicenseUtils.php | 141 +++++++++++------- src/ZendCodingStandard/ruleset.xml | 2 +- test/Util/LicenseUtilTest.php | 98 ++++++++++-- 6 files changed, 201 insertions(+), 185 deletions(-) diff --git a/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php index a6bc7275..373b6f69 100644 --- a/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php +++ b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php @@ -25,26 +25,6 @@ class ZendCodingStandard_Sniffs_Commenting_FileLevelDocBlockSniff implements Sni */ private $repo; - /** - * @var string - */ - private $licenseFirstYear; - - /** - * @var string - */ - private $licenseLastYear; - - /** - * @var boolean - */ - private $copyrightChanged = false; - - /** - * @var boolean - */ - private $fix = false; - const IGNORE = [ T_CLASS, T_INTERFACE, @@ -67,21 +47,6 @@ class ZendCodingStandard_Sniffs_Commenting_FileLevelDocBlockSniff implements Sni public function __construct() { - $this->licenseFirstYear = gmdate('Y'); - $this->licenseLastYear = gmdate('Y'); - - // Detect copyright year in copying file - list($firstYear, $lastYear) = LicenseUtils::getCopyrightDate(LicenseUtils::getCopyrightFile()); - if ($firstYear !== null && $firstYear < $this->licenseFirstYear) { - $this->licenseFirstYear = $firstYear; - } - - // Detect copyright year in license file - list($firstYear, $lastYear) = LicenseUtils::getCopyrightDate(LicenseUtils::getLicenseFile()); - if ($firstYear !== null && $firstYear < $this->licenseFirstYear) { - $this->licenseFirstYear = $firstYear; - } - // Get current repo name from composer.json $content = file_get_contents('composer.json'); $content = json_decode($content, true); @@ -207,7 +172,6 @@ public function process(File $phpcsFile, $stackPtr) $error = 'Expected "%s" for @see tag'; $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectSourceLink', [$expected]); if ($fix === true) { - $this->fix = true; $phpcsFile->fixer->replaceToken($string, $expected); } } @@ -215,16 +179,18 @@ public function process(File $phpcsFile, $stackPtr) } if ($name === '@copyright') { - // Grab license date range - $this->parseCopyrightDate($tokens[$string]['content']); + // Grab copyright date range + list($firstYear, $lastYear) = LicenseUtils::detectDateRange($tokens[$string]['content']); $expected = sprintf('https://github.com/%s/blob/master/COPYING.md Copyright', $this->repo); if (preg_match('|^' . $expected . '$|', $tokens[$string]['content']) === 0) { $error = 'Expected "%s" for @copyright tag'; $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectCopyrightLink', [$expected]); if ($fix === true) { - $this->fix = true; $phpcsFile->fixer->replaceToken($string, $expected); + if ($firstYear !== null) { + LicenseUtils::buildFiles($firstYear, $lastYear); + } } } continue; @@ -236,7 +202,6 @@ public function process(File $phpcsFile, $stackPtr) $error = 'Expected "%s" for @license tag'; $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectLicenseLink', [$expected]); if ($fix === true) { - $this->fix = true; $phpcsFile->fixer->replaceToken($string, $expected); } } @@ -274,61 +239,7 @@ public function process(File $phpcsFile, $stackPtr) $pos++; } - if ($this->copyrightChanged === true && $this->fix === true) { - $this->updateCopyrightLines( - LicenseUtils::getCopyrightFile(), - LicenseUtils::getLicenseFile() - ); - } - // Ignore the rest of the file. return ($phpcsFile->numTokens + 1); } - - /** - * Parse copyright line - * - * Detect year range and update the current first and last years if needed. - * - * @param $string - */ - private function parseCopyrightDate($string) - { - $matches = []; - preg_match('|(?[\d]{4})(-(?[\d]{4}))?|', $string, $matches); - - $licenseFirstYear = isset($matches['start']) ? $matches['start'] : null; - $licenseLastYear = isset($matches['end']) ? $matches['end'] : null; - if ($licenseFirstYear !== null && $this->licenseFirstYear > $licenseFirstYear) { - $this->licenseFirstYear = $licenseFirstYear; - $this->copyrightChanged = true; - } - if ($licenseLastYear !== null && $this->licenseLastYear < $licenseLastYear) { - $this->licenseLastYear = $licenseLastYear; - $this->copyrightChanged = true; - } - } - - private function updateCopyrightLines(SplFileInfo $copyrightFile, SplFileInfo $licenseFile) - { - // Make sure the files exist - LicenseUtils::createCopyrightFile($copyrightFile); - LicenseUtils::createLicenseFile($licenseFile); - - // Update copyright file - LicenseUtils::updateCopyright( - $copyrightFile, - $this->licenseFirstYear, - $this->licenseLastYear - ); - - // Update license file - LicenseUtils::updateCopyright( - $licenseFile, - $this->licenseFirstYear, - $this->licenseLastYear - ); - - $this->copyrightChanged = false; - } } diff --git a/src/ZendCodingStandard/Sniffs/Files/CopyingSniff.php b/src/ZendCodingStandard/Sniffs/Files/CopyingSniff.php index 420908d7..2d2efe43 100644 --- a/src/ZendCodingStandard/Sniffs/Files/CopyingSniff.php +++ b/src/ZendCodingStandard/Sniffs/Files/CopyingSniff.php @@ -60,25 +60,29 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) $error = 'Missing COPYING.md file in the component root dir'; $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingLicense'); if ($fix === true) { - LicenseUtils::createCopyrightFile(); + LicenseUtils::buildFiles(); } // Ignore the rest of the file. return ($phpcsFile->numTokens + 1); } - // Get copyright year - list($firstYear, $lastYear) = LicenseUtils::getCopyrightDate($this->copyrightFile); - if (! $lastYear) { - $lastYear = $firstYear; - } + // Get copyright dates + list($firstYear, $lastYear) = LicenseUtils::detectDateRange( + file_get_contents($this->copyrightFile->getRealPath()) + ); // Check copyright year - if ($lastYear !== gmdate('Y')) { - $error = 'Invalid copyright date in COPYING.md'; + if (($lastYear === null && $firstYear !== gmdate('Y')) + || ($lastYear !== null && $lastYear !== gmdate('Y')) + ) { + $error = sprintf( + 'Expected "Copyright (c) %s" in COPYING.md', + LicenseUtils::formatDateRange($firstYear, gmdate('Y')) + ); $fix = $phpcsFile->addFixableError($error, $stackPtr, 'InvalidCopyrightDate'); if ($fix === true) { - LicenseUtils::updateCopyright($this->copyrightFile, $firstYear); + LicenseUtils::buildFiles($firstYear, $lastYear); } } diff --git a/src/ZendCodingStandard/Sniffs/Files/LicenseSniff.php b/src/ZendCodingStandard/Sniffs/Files/LicenseSniff.php index 576c9a90..78518992 100644 --- a/src/ZendCodingStandard/Sniffs/Files/LicenseSniff.php +++ b/src/ZendCodingStandard/Sniffs/Files/LicenseSniff.php @@ -59,25 +59,29 @@ public function process(File $phpcsFile, $stackPtr) $error = 'Missing LICENSE.md file in the component root dir'; $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingLicense'); if ($fix === true) { - LicenseUtils::createLicenseFile(); + LicenseUtils::buildFiles(); } // Ignore the rest of the file. return ($phpcsFile->numTokens + 1); } - // Get copyright year - list($firstYear, $lastYear) = LicenseUtils::getCopyrightDate($this->licenseFile); - if (! $lastYear) { - $lastYear = $firstYear; - } + // Get license dates + list($firstYear, $lastYear) = LicenseUtils::detectDateRange( + file_get_contents($this->licenseFile->getRealPath()) + ); - // Check copyright year - if ($lastYear !== gmdate('Y')) { - $error = 'Invalid copyright date in COPYING.md'; + // Check license year + if (($lastYear === null && $firstYear !== gmdate('Y')) + || ($lastYear !== null && $lastYear !== gmdate('Y')) + ) { + $error = sprintf( + 'Expected "Copyright (c) %s" in LICENSE.md', + LicenseUtils::formatDateRange($firstYear, gmdate('Y')) + ); $fix = $phpcsFile->addFixableError($error, $stackPtr, 'InvalidCopyrightDate'); if ($fix === true) { - LicenseUtils::updateCopyright($this->licenseFile, $firstYear); + LicenseUtils::buildFiles($firstYear, $lastYear); } } diff --git a/src/ZendCodingStandard/Utils/LicenseUtils.php b/src/ZendCodingStandard/Utils/LicenseUtils.php index 569c0294..13f87453 100644 --- a/src/ZendCodingStandard/Utils/LicenseUtils.php +++ b/src/ZendCodingStandard/Utils/LicenseUtils.php @@ -65,27 +65,6 @@ public static function getCopyrightFile() return new SplFileInfo('COPYING.md'); } - /** - * Create a new copyright file if it does not exist - * - * @param SplFileInfo|null $file - */ - public static function createCopyrightFile(SplFileInfo $file = null) - { - if ($file === null) { - $file = self::getCopyrightFile(); - } - - if ($file->getRealPath()) { - // File already exists - return; - } - - // Create new file - $file = $file->openFile('x'); - $file->fwrite(sprintf(self::$copyright, gmdate('Y'))); - } - /** * Get COPYING.md as an object * @@ -97,68 +76,114 @@ public static function getLicenseFile() } /** - * Create a new license file if it does not exist + * Detect and match date range * - * @param SplFileInfo|null $file + * It detects the copyright dates in a string and compares those to the + * given values. It returns the lowest detected first year and highest + * detected last year. + * + * @param string $string + * @param string|null $firstYear + * @param string|null $lastYear + * @return array */ - public static function createLicenseFile(SplFileInfo $file = null) + public static function detectDateRange($string, $firstYear = null, $lastYear = null) { - if ($file === null) { - $file = self::getLicenseFile(); + $matches = []; + preg_match('|(?[\d]{4})(-(?[\d]{4}))?|', $string, $matches); + + $detectedFirstYear = isset($matches['start']) ? $matches['start'] : null; + $detectedLastYear = isset($matches['end']) ? $matches['end'] : null; + + if ($firstYear === null || $detectedFirstYear < $firstYear) { + $firstYear = $detectedFirstYear; } - if ($file->getRealPath()) { - // File already exists - return; + if ($lastYear === null || $detectedLastYear > $lastYear) { + $lastYear = $detectedLastYear; } - // Create new file - $file = $file->openFile('x'); - $file->fwrite(sprintf(self::$license, gmdate('Y'))); + return [$firstYear, $lastYear]; } /** - * Update the copyright line - * The copyright line must be the first one in the file. + * Format date range + * + * Returns a formatted date range from a given first and last year. If the + * last year is not set or the same as the first year it returns a single + * year. Otherwise it returns `-`. * - * @param SplFileInfo|null $file + * @param string|null $firstYear + * @param string|null $lastYear + * @return string */ - public static function updateCopyright(SplFileInfo $file, $firstYear, $lastYear = null) + public static function formatDateRange($firstYear = null, $lastYear = null) { - if ($lastYear === null) { - $lastYear = gmdate('Y'); + $currentYear = gmdate('Y'); + if ($lastYear === null || $lastYear > $currentYear) { + $lastYear = $currentYear; } - $copyrightDateRange = $lastYear; - if ($firstYear !== $lastYear) { - $copyrightDateRange = sprintf('%s-%s', $firstYear, $lastYear); + $dateRange = $lastYear; + if ($firstYear !== null && $firstYear < $lastYear) { + $dateRange = sprintf('%s-%s', $firstYear, $lastYear); } - // Update copyright line; first line in the file - $content = file($file->getRealPath()); - $content[0] = sprintf(self::$copyrightLine . PHP_EOL, $copyrightDateRange); - file_put_contents($file->getRealPath(), $content); + return $dateRange; } /** - * Detect the copyright date range from the first line in a file + * Build copying and license files * - * @param SplFileInfo $file - * @return array[string|null,string|null] + * This detects the current date range if any of the two files exist. And + * updates their content in case of any detected changes. + * + * @param null $firstYear + * @param null $lastYear + * @param SplFileInfo|null $copyrightFile + * @param SplFileInfo|null $licenseFile */ - public static function getCopyrightDate(SplFileInfo $file) - { - if (! $file->getRealPath()) { - return [null, null]; + public static function buildFiles( + $firstYear = null, + $lastYear = null, + SplFileInfo $copyrightFile = null, + SplFileInfo $licenseFile = null + ) { + if ($copyrightFile === null) { + $copyrightFile = self::getCopyrightFile(); } - $content = file($file->getRealPath()); - $matches = []; - preg_match('|(?[\d]{4})(-(?[\d]{4}))?|', $content[0], $matches); + if ($licenseFile === null) { + $licenseFile = self::getLicenseFile(); + } - $licenseFirstYear = isset($matches['start']) ? $matches['start'] : null; - $licenseLastYear = isset($matches['end']) ? $matches['end'] : null; + // Get copyright dates + $oldCopyright = null; + if ($copyrightFile->isReadable()) { + $oldCopyright = file_get_contents($copyrightFile->getPathname()); + list($firstYear, $lastYear) = self::detectDateRange($oldCopyright, $firstYear, $lastYear); + } - return [$licenseFirstYear, $licenseLastYear]; + // Get license dates + $oldLicense = null; + if ($licenseFile->isReadable()) { + $oldLicense = file_get_contents($licenseFile->getPathname()); + list($firstYear, $lastYear) = self::detectDateRange($oldLicense, $firstYear, $lastYear); + } + + // Format date range, enforce current year + $copyrightDateRange = self::formatDateRange($firstYear, gmdate('Y')); + + // Save new copyright content if it's updated + $newCopyright = sprintf(self::$copyright, $copyrightDateRange); + if ($oldCopyright === null || $oldCopyright !== $newCopyright) { + file_put_contents($copyrightFile->getPathname(), $newCopyright); + } + + // Save new license if it's updated + $newLicense = sprintf(self::$license, $copyrightDateRange); + if ($oldLicense === null || $oldLicense !== $newLicense) { + file_put_contents($licenseFile->getPathname(), $newLicense); + } } } diff --git a/src/ZendCodingStandard/ruleset.xml b/src/ZendCodingStandard/ruleset.xml index a3ecb1e0..ecdc8d72 100644 --- a/src/ZendCodingStandard/ruleset.xml +++ b/src/ZendCodingStandard/ruleset.xml @@ -5,8 +5,8 @@ - Sniffs/Files/LICENSE.md Sniffs/Files/COPYING.md + Sniffs/Files/LICENSE.md diff --git a/test/Util/LicenseUtilTest.php b/test/Util/LicenseUtilTest.php index a5efafba..fd1b779b 100644 --- a/test/Util/LicenseUtilTest.php +++ b/test/Util/LicenseUtilTest.php @@ -10,8 +10,8 @@ use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\vfsStreamDirectory; use PHPUnit\Framework\TestCase; -use Zend\CodingStandard\Utils\LicenseUtils; use SplFileInfo; +use Zend\CodingStandard\Utils\LicenseUtils; class LicenseUtilTest extends TestCase { @@ -25,29 +25,101 @@ public function setUp() $this->root = vfsStream::setup('tmp'); } - public function testCopyrightFileIsCreated() + /** + * @dataProvider dateRangeDetectionProvider + */ + public function testDateRangeDetection($string, $firstYear, $lastYear, $expectedFirstYear, $expectedLastYear) + { + list($actualFirstYear, $actualLastYear) = LicenseUtils::detectDateRange($string, $firstYear, $lastYear); + + $this->assertEquals($expectedFirstYear, $actualFirstYear); + $this->assertEquals($expectedLastYear, $actualLastYear); + } + + public function dateRangeDetectionProvider() { - LicenseUtils::createCopyrightFile( - new SplFileInfo($this->root->url() . '/COPYING.tmp.md') + return [ + 'empty' => ['Copyright (c) Foo', null, null, null, null], + '2014' => ['(c) 2014 Foo', null, null, 2014, null], + '2015-2016' => ['(c) 2015-2016 Bar', null, null, 2015, 2016], + '2016-current' => [sprintf('(c) 2016-%s', gmdate('Y')), null, null, 2016, gmdate('Y')], + 'current' => [sprintf('(c) %s', gmdate('Y')), null, null, gmdate('Y'), null], + 'o2012' => ['(c) 2014 Foo', 2012, null, 2012, null], + 'o2016' => ['(c) 2014 Foo', 2016, null, 2014, null], + 'o2012-o2015' => ['(c) 2014 Foo', 2012, 2015, 2012, 2015], + 'o2012-o2016' => ['(c) 2014-2015', 2012, 2016, 2012, 2016], + 'o2016-oCurrent' => [sprintf('(c) 2016-%s', gmdate('Y')), 2012, 2016, 2012, gmdate('Y')], + 'oCurrent' => [sprintf('(c) %s', gmdate('Y')), 2012, 2014, 2012, 2014], + ]; + } + + /** + * @dataProvider dateRangeFormatProvider + */ + public function testFormatDateRange($firstYear, $lastYear, $expected) + { + $this->assertEquals($expected, LicenseUtils::formatDateRange($firstYear, $lastYear)); + } + + public function dateRangeFormatProvider() + { + return [ + '2014' => ['2014', null, '2014-' . gmdate('Y')], + '2015-2016' => ['2015', '2016', '2015-2016'], + '2016-current' => ['2016', gmdate('Y'), '2016-' . gmdate('Y')], + 'current' => [gmdate('Y'), null, gmdate('Y')], + ]; + } + + public function testBuildNewFiles() + { + $firstYear = null; + $lastYear = null; + $copyrightFile = new SplFileInfo($this->root->url() . '/COPYING.tmp'); + $licenseFile = new SplFileInfo($this->root->url() . '/LICENSE.tmp'); + + LicenseUtils::buildFiles($firstYear, $lastYear, $copyrightFile, $licenseFile); + + $this->assertTrue($this->root->hasChild('COPYING.tmp')); + $this->assertEquals( + sprintf(LicenseUtils::$copyright, LicenseUtils::formatDateRange(gmdate('Y'))), + $this->root->getChild('COPYING.tmp')->getContent() ); - $this->assertTrue($this->root->hasChild('COPYING.tmp.md')); + $this->assertTrue($this->root->hasChild('LICENSE.tmp')); $this->assertEquals( - sprintf(LicenseUtils::$copyright, gmdate('Y')), - $this->root->getChild('COPYING.tmp.md')->getContent() + sprintf(LicenseUtils::$license, LicenseUtils::formatDateRange(gmdate('Y'))), + $this->root->getChild('LICENSE.tmp')->getContent() ); } - public function testLicenseFileIsCreated() + public function testUpdateBothFilesWithSameDates() { - LicenseUtils::createLicenseFile( - new SplFileInfo($this->root->url() . '/LICENSE.tmp.md') + $copyrightFile = new SplFileInfo($this->root->url() . '/COPYING.tmp'); + $licenseFile = new SplFileInfo($this->root->url() . '/LICENSE.tmp'); + + file_put_contents( + $copyrightFile->getPathname(), + sprintf(LicenseUtils::$copyright, LicenseUtils::formatDateRange('2015')) + ); + + file_put_contents( + $licenseFile->getPathname(), + sprintf(LicenseUtils::$license, LicenseUtils::formatDateRange('2016-2017')) + ); + + LicenseUtils::buildFiles('2016', '2016', $copyrightFile, $licenseFile); + + $this->assertTrue($this->root->hasChild('COPYING.tmp')); + $this->assertEquals( + sprintf(LicenseUtils::$copyright, LicenseUtils::formatDateRange('2015', gmdate('Y'))), + $this->root->getChild('COPYING.tmp')->getContent() ); - $this->assertTrue($this->root->hasChild('LICENSE.tmp.md')); + $this->assertTrue($this->root->hasChild('LICENSE.tmp')); $this->assertEquals( - sprintf(LicenseUtils::$license, gmdate('Y')), - $this->root->getChild('LICENSE.tmp.md')->getContent() + sprintf(LicenseUtils::$license, LicenseUtils::formatDateRange('2015', gmdate('Y'))), + $this->root->getChild('LICENSE.tmp')->getContent() ); } } From 82d08bf0804a663c8e35b6b5aebe9e3bf524329f Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Fri, 14 Apr 2017 10:23:37 +0200 Subject: [PATCH 12/25] Detect and replace deprecated link tag with see tag --- .../Commenting/FileLevelDocBlockSniff.php | 23 +++++++++++++++---- ...leLevelDocBlockDeprecatedLinkTag.fixed.php | 12 ++++++++++ .../FileLevelDocBlockDeprecatedLinkTag.php | 12 ++++++++++ .../Commenting/FileLevelDocBlockSniffTest.php | 12 ++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 test/Sniffs/Commenting/Assets/FileLevelDocBlockDeprecatedLinkTag.fixed.php create mode 100644 test/Sniffs/Commenting/Assets/FileLevelDocBlockDeprecatedLinkTag.php diff --git a/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php index 373b6f69..9ec593d0 100644 --- a/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php +++ b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php @@ -154,7 +154,15 @@ public function process(File $phpcsFile, $stackPtr) $foundTags[] = $name; - if ($isRequired === false) { + if ($name === '@link') { + $error = 'Deprecated @link tag is used, use @see tag instead'; + $fix = $phpcsFile->addFixableError($error, $tag, 'DeprecatedLinkTag'); + if ($fix === true) { + $phpcsFile->fixer->replaceToken($tag, '@see '); + } + } + + if ($isRequired === false && $name !== '@link') { continue; } @@ -166,11 +174,11 @@ public function process(File $phpcsFile, $stackPtr) continue; } - if ($name === '@see') { + if ($name === '@see' || $name === '@link') { $expected = sprintf('https://github.com/%s for the canonical source repository', $this->repo); if (preg_match('|^' . $expected . '$|', $tokens[$string]['content']) === 0) { - $error = 'Expected "%s" for @see tag'; - $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectSourceLink', [$expected]); + $error = 'Expected "%s" for %s tag'; + $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectSourceLink', [$expected, $name]); if ($fix === true) { $phpcsFile->fixer->replaceToken($string, $expected); } @@ -209,6 +217,13 @@ public function process(File $phpcsFile, $stackPtr) } } + // If a @link tag was detected, it already triggered errors at this + // point. Treat @link as @see to suppress even more errors and warnings + // which should have been fixed by renaming the tag. + if ($foundTags[0] === '@link') { + $foundTags[0] = '@see'; + } + // Check if the tags are in the correct position. $pos = 0; foreach ($required as $tag => $true) { diff --git a/test/Sniffs/Commenting/Assets/FileLevelDocBlockDeprecatedLinkTag.fixed.php b/test/Sniffs/Commenting/Assets/FileLevelDocBlockDeprecatedLinkTag.fixed.php new file mode 100644 index 00000000..17883a82 --- /dev/null +++ b/test/Sniffs/Commenting/Assets/FileLevelDocBlockDeprecatedLinkTag.fixed.php @@ -0,0 +1,12 @@ + 0, 'warnings' => [], ], + + 'FileLevelDocBlockDeprecatedLinkTag' => [ + 'asset' => __DIR__ . '/Assets/FileLevelDocBlockDeprecatedLinkTag.php', + 'fixed' => __DIR__ . '/Assets/FileLevelDocBlockDeprecatedLinkTag.fixed.php', + 'errorCount' => 2, + 'errors' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.DeprecatedLinkTag', + 'ZendCodingStandard.Commenting.FileLevelDocBlock.IncorrectSourceLink', + ], + 'warningCount' => 0, + 'warnings' => [], + ], ]; } } From 8f30be0b0f22d1a5c6b7b017c7c149177c023437 Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Fri, 14 Apr 2017 10:31:26 +0200 Subject: [PATCH 13/25] Globally exclude TestAssets --- phpcs.xml | 3 --- src/ZendCodingStandard/ruleset.xml | 3 +++ .../Assets/FileLevelDocBlockMissing.php | 7 ------ .../Commenting/FileLevelDocBlockSniffTest.php | 24 +++++++++---------- ...leLevelDocBlockDeprecatedLinkTag.fixed.php | 2 +- .../FileLevelDocBlockDeprecatedLinkTag.php | 2 +- .../FileLevelDocBlockEmptyTags.php | 2 +- ...elDocBlockIncorrectCopyrightLink.fixed.php | 2 +- ...ileLevelDocBlockIncorrectCopyrightLink.php | 2 +- ...evelDocBlockIncorrectLicenseLink.fixed.php | 2 +- .../FileLevelDocBlockIncorrectLicenseLink.php | 2 +- ...LevelDocBlockIncorrectSourceLink.fixed.php | 2 +- .../FileLevelDocBlockIncorrectSourceLink.php | 2 +- .../TestAsset/FileLevelDocBlockMissing.php | 7 ++++++ .../FileLevelDocBlockPass.php | 2 +- .../FileLevelDocBlockSpacingAfterOpen.php | 2 +- 16 files changed, 33 insertions(+), 33 deletions(-) delete mode 100644 test/Sniffs/Commenting/Assets/FileLevelDocBlockMissing.php rename test/Sniffs/Commenting/{Assets => TestAsset}/FileLevelDocBlockDeprecatedLinkTag.fixed.php (85%) rename test/Sniffs/Commenting/{Assets => TestAsset}/FileLevelDocBlockDeprecatedLinkTag.php (84%) rename test/Sniffs/Commenting/{Assets => TestAsset}/FileLevelDocBlockEmptyTags.php (58%) rename test/Sniffs/Commenting/{Assets => TestAsset}/FileLevelDocBlockIncorrectCopyrightLink.fixed.php (85%) rename test/Sniffs/Commenting/{Assets => TestAsset}/FileLevelDocBlockIncorrectCopyrightLink.php (85%) rename test/Sniffs/Commenting/{Assets => TestAsset}/FileLevelDocBlockIncorrectLicenseLink.fixed.php (85%) rename test/Sniffs/Commenting/{Assets => TestAsset}/FileLevelDocBlockIncorrectLicenseLink.php (82%) rename test/Sniffs/Commenting/{Assets => TestAsset}/FileLevelDocBlockIncorrectSourceLink.fixed.php (85%) rename test/Sniffs/Commenting/{Assets => TestAsset}/FileLevelDocBlockIncorrectSourceLink.php (85%) create mode 100644 test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissing.php rename test/Sniffs/Commenting/{Assets => TestAsset}/FileLevelDocBlockPass.php (85%) rename test/Sniffs/Commenting/{Assets => TestAsset}/FileLevelDocBlockSpacingAfterOpen.php (85%) diff --git a/phpcs.xml b/phpcs.xml index 5cc286e1..63348e5a 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -6,9 +6,6 @@ src test - - */Assets/* - diff --git a/src/ZendCodingStandard/ruleset.xml b/src/ZendCodingStandard/ruleset.xml index ecdc8d72..567c9d09 100644 --- a/src/ZendCodingStandard/ruleset.xml +++ b/src/ZendCodingStandard/ruleset.xml @@ -8,6 +8,9 @@ Sniffs/Files/COPYING.md Sniffs/Files/LICENSE.md + + */TestAsset/* + diff --git a/test/Sniffs/Commenting/Assets/FileLevelDocBlockMissing.php b/test/Sniffs/Commenting/Assets/FileLevelDocBlockMissing.php deleted file mode 100644 index b7f32dd3..00000000 --- a/test/Sniffs/Commenting/Assets/FileLevelDocBlockMissing.php +++ /dev/null @@ -1,7 +0,0 @@ - [ - 'asset' => __DIR__ . '/Assets/FileLevelDocBlockPass.php', + 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockPass.php', 'fixed' => null, 'errorCount' => 0, 'errors' => [], @@ -45,7 +45,7 @@ public function assetsProvider() ], 'FileLevelDocBlockMissing' => [ - 'asset' => __DIR__ . '/Assets/FileLevelDocBlockMissing.php', + 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockMissing.php', 'fixed' => null, 'errorCount' => 1, 'errors' => [ @@ -56,7 +56,7 @@ public function assetsProvider() ], 'FileLevelDocBlockSpacingAfterOpen' => [ - 'asset' => __DIR__ . '/Assets/FileLevelDocBlockSpacingAfterOpen.php', + 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockSpacingAfterOpen.php', 'fixed' => null, 'errorCount' => 1, 'errors' => [ @@ -67,8 +67,8 @@ public function assetsProvider() ], 'FileLevelDocBlockIncorrectSourceLink' => [ - 'asset' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectSourceLink.php', - 'fixed' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectSourceLink.fixed.php', + 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockIncorrectSourceLink.php', + 'fixed' => __DIR__ . '/TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php', 'errorCount' => 1, 'errors' => [ 'ZendCodingStandard.Commenting.FileLevelDocBlock.IncorrectSourceLink', @@ -78,8 +78,8 @@ public function assetsProvider() ], 'FileLevelDocBlockIncorrectCopyrightLink' => [ - 'asset' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectCopyrightLink.php', - 'fixed' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectCopyrightLink.fixed.php', + 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.php', + 'fixed' => __DIR__ . '/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.fixed.php', 'errorCount' => 1, 'errors' => [ 'ZendCodingStandard.Commenting.FileLevelDocBlock.IncorrectCopyrightLink', @@ -89,8 +89,8 @@ public function assetsProvider() ], 'FileLevelDocBlockIncorrectLicenseLink' => [ - 'asset' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectLicenseLink.php', - 'fixed' => __DIR__ . '/Assets/FileLevelDocBlockIncorrectLicenseLink.fixed.php', + 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockIncorrectLicenseLink.php', + 'fixed' => __DIR__ . '/TestAsset/FileLevelDocBlockIncorrectLicenseLink.fixed.php', 'errorCount' => 1, 'errors' => [ 'ZendCodingStandard.Commenting.FileLevelDocBlock.IncorrectLicenseLink', @@ -100,7 +100,7 @@ public function assetsProvider() ], 'FileLevelDocBlockEmptyTags' => [ - 'asset' => __DIR__ . '/Assets/FileLevelDocBlockEmptyTags.php', + 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockEmptyTags.php', 'fixed' => null, 'errorCount' => 3, 'errors' => [ @@ -113,8 +113,8 @@ public function assetsProvider() ], 'FileLevelDocBlockDeprecatedLinkTag' => [ - 'asset' => __DIR__ . '/Assets/FileLevelDocBlockDeprecatedLinkTag.php', - 'fixed' => __DIR__ . '/Assets/FileLevelDocBlockDeprecatedLinkTag.fixed.php', + 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockDeprecatedLinkTag.php', + 'fixed' => __DIR__ . '/TestAsset/FileLevelDocBlockDeprecatedLinkTag.fixed.php', 'errorCount' => 2, 'errors' => [ 'ZendCodingStandard.Commenting.FileLevelDocBlock.DeprecatedLinkTag', diff --git a/test/Sniffs/Commenting/Assets/FileLevelDocBlockDeprecatedLinkTag.fixed.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.fixed.php similarity index 85% rename from test/Sniffs/Commenting/Assets/FileLevelDocBlockDeprecatedLinkTag.fixed.php rename to test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.fixed.php index 17883a82..3719e929 100644 --- a/test/Sniffs/Commenting/Assets/FileLevelDocBlockDeprecatedLinkTag.fixed.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.fixed.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\Assets; +namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; class FileLevelDocBlockDeprecatedLinkTag { diff --git a/test/Sniffs/Commenting/Assets/FileLevelDocBlockDeprecatedLinkTag.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.php similarity index 84% rename from test/Sniffs/Commenting/Assets/FileLevelDocBlockDeprecatedLinkTag.php rename to test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.php index 4b86cbb2..b2cf2230 100644 --- a/test/Sniffs/Commenting/Assets/FileLevelDocBlockDeprecatedLinkTag.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\Assets; +namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; class FileLevelDocBlockDeprecatedLinkTag { diff --git a/test/Sniffs/Commenting/Assets/FileLevelDocBlockEmptyTags.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockEmptyTags.php similarity index 58% rename from test/Sniffs/Commenting/Assets/FileLevelDocBlockEmptyTags.php rename to test/Sniffs/Commenting/TestAsset/FileLevelDocBlockEmptyTags.php index d937bef1..794f2987 100644 --- a/test/Sniffs/Commenting/Assets/FileLevelDocBlockEmptyTags.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockEmptyTags.php @@ -5,7 +5,7 @@ * @license */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\Assets; +namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; class FileLevelDocBlockEmptyTags { diff --git a/test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectCopyrightLink.fixed.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.fixed.php similarity index 85% rename from test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectCopyrightLink.fixed.php rename to test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.fixed.php index bbcac09c..b6d5e7a0 100644 --- a/test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectCopyrightLink.fixed.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.fixed.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\Assets; +namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectCopyrightLink { diff --git a/test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectCopyrightLink.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.php similarity index 85% rename from test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectCopyrightLink.php rename to test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.php index 35887f99..ed6df4e0 100644 --- a/test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectCopyrightLink.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\Assets; +namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectCopyrightLink { diff --git a/test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectLicenseLink.fixed.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.fixed.php similarity index 85% rename from test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectLicenseLink.fixed.php rename to test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.fixed.php index e2474c94..b46311e9 100644 --- a/test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectLicenseLink.fixed.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.fixed.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\Assets; +namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectLicenseLink { diff --git a/test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectLicenseLink.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.php similarity index 82% rename from test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectLicenseLink.php rename to test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.php index 47df3260..c36f9597 100644 --- a/test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectLicenseLink.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.php @@ -5,7 +5,7 @@ * @license New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\Assets; +namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectLicenseLink { diff --git a/test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectSourceLink.fixed.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php similarity index 85% rename from test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectSourceLink.fixed.php rename to test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php index 26998fdd..eab97c1c 100644 --- a/test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectSourceLink.fixed.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\Assets; +namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectSourceLink { diff --git a/test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectSourceLink.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.php similarity index 85% rename from test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectSourceLink.php rename to test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.php index 202ff438..daa0bf01 100644 --- a/test/Sniffs/Commenting/Assets/FileLevelDocBlockIncorrectSourceLink.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\Assets; +namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectSourceLink { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissing.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissing.php new file mode 100644 index 00000000..d8ae9fa7 --- /dev/null +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissing.php @@ -0,0 +1,7 @@ + Date: Fri, 14 Apr 2017 14:12:47 +0200 Subject: [PATCH 14/25] Add more tests and a fix for SpacingAfterComment --- .../Commenting/FileLevelDocBlockSniff.php | 23 +++---- src/ZendCodingStandard/ruleset.xml | 2 +- .../Commenting/FileLevelDocBlockSniffTest.php | 61 +++++++++++++++++++ .../FileLevelDocBlockMissingCopyrightTag.php | 12 ++++ .../FileLevelDocBlockMissingLicenseTag.php | 12 ++++ .../FileLevelDocBlockMissingSeeTag.php | 12 ++++ ...LevelDocBlockSpacingAfterComment.fixed.php | 12 ++++ .../FileLevelDocBlockSpacingAfterComment.php | 11 ++++ .../TestAsset/FileLevelDocBlockWrongStyle.php | 12 ++++ 9 files changed, 145 insertions(+), 12 deletions(-) create mode 100644 test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissingCopyrightTag.php create mode 100644 test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissingLicenseTag.php create mode 100644 test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissingSeeTag.php create mode 100644 test/Sniffs/Commenting/TestAsset/FileLevelDocBlockSpacingAfterComment.fixed.php create mode 100644 test/Sniffs/Commenting/TestAsset/FileLevelDocBlockSpacingAfterComment.php create mode 100644 test/Sniffs/Commenting/TestAsset/FileLevelDocBlockWrongStyle.php diff --git a/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php index 9ec593d0..b12fa939 100644 --- a/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php +++ b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php @@ -77,7 +77,7 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - // Skip license file + // Skip license and copying file if (in_array(substr($phpcsFile->getFilename(), -10), ['LICENSE.md', 'COPYING.md'])) { return ($phpcsFile->numTokens + 1); } @@ -85,17 +85,19 @@ public function process(File $phpcsFile, $stackPtr) $tokens = $phpcsFile->getTokens(); $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); + // Valid file-level DocBlock style if ($tokens[$commentStart]['code'] === T_COMMENT) { $phpcsFile->addError( 'You must use "/**" style comments for a file-level DocBlock', $commentStart, 'WrongStyle' ); - $phpcsFile->recordMetric($stackPtr, 'File has doc comment', 'yes'); + $phpcsFile->recordMetric($stackPtr, 'File has file-level DocBlock', 'yes'); return ($phpcsFile->numTokens + 1); } + // File-level DocBlock exists, part 1 if ($commentStart === false || $tokens[$commentStart]['code'] !== T_DOC_COMMENT_OPEN_TAG) { $phpcsFile->addError('Missing file-level DocBlock', $stackPtr, 'Missing'); $phpcsFile->recordMetric($stackPtr, 'File has file-level DocBlock', 'no'); @@ -104,14 +106,9 @@ public function process(File $phpcsFile, $stackPtr) } $commentEnd = $tokens[$commentStart]['comment_closer']; + $nextToken = $phpcsFile->findNext(T_WHITESPACE, $commentEnd + 1, null, true); - $nextToken = $phpcsFile->findNext( - T_WHITESPACE, - $commentEnd + 1, - null, - true - ); - + // File-level DocBlock exists, part 2 if (in_array($tokens[$nextToken]['code'], self::IGNORE) === true) { $phpcsFile->addError('Missing file-level DocBlock', $stackPtr, 'Missing'); $phpcsFile->recordMetric($stackPtr, 'File has file-level DocBlock', 'no'); @@ -119,6 +116,7 @@ public function process(File $phpcsFile, $stackPtr) return ($phpcsFile->numTokens + 1); } + // File-level DocBlock does exist $phpcsFile->recordMetric($stackPtr, 'File has file-level DocBlock', 'yes'); // No blank line between the open tag and the file comment. @@ -127,11 +125,14 @@ public function process(File $phpcsFile, $stackPtr) $phpcsFile->addError($error, $stackPtr, 'SpacingAfterOpen'); } - // Exactly one blank line after the file comment. + // Exactly one blank line after the file-level DocBlock $next = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), null, true); if ($tokens[$next]['line'] !== ($tokens[$commentEnd]['line'] + 2)) { $error = 'There must be exactly one blank line after the file-level DocBlock'; - $phpcsFile->addError($error, $commentEnd, 'SpacingAfterComment'); + $fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfterComment'); + if ($fix === true) { + $phpcsFile->fixer->addNewline($commentEnd); + } } // Required tags in correct order. diff --git a/src/ZendCodingStandard/ruleset.xml b/src/ZendCodingStandard/ruleset.xml index 567c9d09..1db338c4 100644 --- a/src/ZendCodingStandard/ruleset.xml +++ b/src/ZendCodingStandard/ruleset.xml @@ -9,7 +9,7 @@ Sniffs/Files/LICENSE.md - */TestAsset/* + diff --git a/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php b/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php index 515b15e2..946ac246 100644 --- a/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php +++ b/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php @@ -55,6 +55,56 @@ public function assetsProvider() 'warnings' => [], ], + 'FileLevelDocBlockMissingSeeTag' => [ + 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockMissingSeeTag.php', + 'fixed' => null, + 'errorCount' => 1, + 'errors' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.MissingSeeTag', + ], + 'warningCount' => 1, + 'warnings' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.SeeTagOrder', + ], + ], + + 'FileLevelDocBlockMissingCopyrightTag' => [ + 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockMissingCopyrightTag.php', + 'fixed' => null, + 'errorCount' => 1, + 'errors' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.MissingCopyrightTag', + ], + 'warningCount' => 1, + 'warnings' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.CopyrightTagOrder', + ], + ], + + 'FileLevelDocBlockMissingLicenseTag' => [ + 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockMissingLicenseTag.php', + 'fixed' => null, + 'errorCount' => 1, + 'errors' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.MissingLicenseTag', + ], + 'warningCount' => 1, + 'warnings' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.LicenseTagOrder', + ], + ], + + 'FileLevelDocBlockWrongStyle' => [ + 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockWrongStyle.php', + 'fixed' => null, + 'errorCount' => 1, + 'errors' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.WrongStyle', + ], + 'warningCount' => 0, + 'warnings' => [], + ], + 'FileLevelDocBlockSpacingAfterOpen' => [ 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockSpacingAfterOpen.php', 'fixed' => null, @@ -66,6 +116,17 @@ public function assetsProvider() 'warnings' => [], ], + 'FileLevelDocBlockSpacingAfterComment' => [ + 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockSpacingAfterComment.php', + 'fixed' => __DIR__ . '/TestAsset/FileLevelDocBlockSpacingAfterComment.fixed.php', + 'errorCount' => 1, + 'errors' => [ + 'ZendCodingStandard.Commenting.FileLevelDocBlock.SpacingAfterComment', + ], + 'warningCount' => 0, + 'warnings' => [], + ], + 'FileLevelDocBlockIncorrectSourceLink' => [ 'asset' => __DIR__ . '/TestAsset/FileLevelDocBlockIncorrectSourceLink.php', 'fixed' => __DIR__ . '/TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php', diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissingCopyrightTag.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissingCopyrightTag.php new file mode 100644 index 00000000..df107dd2 --- /dev/null +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissingCopyrightTag.php @@ -0,0 +1,12 @@ + Date: Fri, 14 Apr 2017 14:20:49 +0200 Subject: [PATCH 15/25] Enable excluding TestAsset paths again :$ --- phpcs.xml | 3 +++ src/ZendCodingStandard/ruleset.xml | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/phpcs.xml b/phpcs.xml index 63348e5a..a4afb374 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -6,6 +6,9 @@ src test + + */TestAsset/* + diff --git a/src/ZendCodingStandard/ruleset.xml b/src/ZendCodingStandard/ruleset.xml index 1db338c4..567c9d09 100644 --- a/src/ZendCodingStandard/ruleset.xml +++ b/src/ZendCodingStandard/ruleset.xml @@ -9,7 +9,7 @@ Sniffs/Files/LICENSE.md - + */TestAsset/* From 49c98f61f50d8b219f5161cec361287cf88a496c Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Fri, 14 Apr 2017 14:24:25 +0200 Subject: [PATCH 16/25] Bring readme inline with other components --- README.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2a7e94a8..caa8b276 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -Zend Framework Coding Standard -============================== +# Zend Framework Coding Standard -Repository with all coding standard ruleset for Zend Framework repositories. +[![Build Status](https://secure.travis-ci.org/zendframework/zend-coding-standard.svg?branch=master)](https://secure.travis-ci.org/zendframework/zend-coding-standard) +[![Coverage Status](https://coveralls.io/repos/zendframework/zend-coding-standard/badge.svg?branch=master)](https://coveralls.io/r/zendframework/zend-coding-standard?branch=master) +Repository with all coding standard ruleset for Zend Framework repositories. -Installation ------------- +## Installation 1. Install the module via composer by running: @@ -39,9 +39,7 @@ Installation You can add or exclude some locations in that file. For a reference please see: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml - -Usage ------ +## Usage * To run checks only: From 71f10ac5a0baac2d56eef0489717327df4eba49b Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Wed, 17 May 2017 10:18:03 +0200 Subject: [PATCH 17/25] Update to PHP_CodeSniffer 3.x --- composer.json | 6 +-- phpcs.xml | 14 +++--- .../Commenting/FileLevelDocBlockSniff.php | 46 +++++++++---------- .../Sniffs/Files/COPYING.md | 13 ------ .../Sniffs/Files/CopyingSniff.php | 29 ++++++------ .../Sniffs/Files/LICENSE.md | 10 ---- .../Sniffs/Files/LicenseSniff.php | 26 ++++++----- src/ZendCodingStandard/Utils/LicenseUtils.php | 2 +- src/ZendCodingStandard/ruleset.xml | 22 +++++---- test/SniffTestCase.php | 4 +- .../Commenting/FileLevelDocBlockSniffTest.php | 4 +- ...leLevelDocBlockDeprecatedLinkTag.fixed.php | 2 +- .../FileLevelDocBlockDeprecatedLinkTag.php | 2 +- .../TestAsset/FileLevelDocBlockEmptyTags.php | 2 +- ...elDocBlockIncorrectCopyrightLink.fixed.php | 2 +- ...ileLevelDocBlockIncorrectCopyrightLink.php | 2 +- ...evelDocBlockIncorrectLicenseLink.fixed.php | 2 +- .../FileLevelDocBlockIncorrectLicenseLink.php | 2 +- ...LevelDocBlockIncorrectSourceLink.fixed.php | 2 +- .../FileLevelDocBlockIncorrectSourceLink.php | 2 +- .../TestAsset/FileLevelDocBlockMissing.php | 2 +- .../FileLevelDocBlockMissingCopyrightTag.php | 2 +- .../FileLevelDocBlockMissingLicenseTag.php | 2 +- .../FileLevelDocBlockMissingSeeTag.php | 2 +- .../TestAsset/FileLevelDocBlockPass.php | 2 +- ...LevelDocBlockSpacingAfterComment.fixed.php | 2 +- .../FileLevelDocBlockSpacingAfterComment.php | 2 +- .../FileLevelDocBlockSpacingAfterOpen.php | 2 +- .../TestAsset/FileLevelDocBlockWrongStyle.php | 2 +- test/Util/LicenseUtilTest.php | 4 +- 30 files changed, 100 insertions(+), 116 deletions(-) delete mode 100644 src/ZendCodingStandard/Sniffs/Files/COPYING.md delete mode 100644 src/ZendCodingStandard/Sniffs/Files/LICENSE.md diff --git a/composer.json b/composer.json index 4c6bad50..9151c941 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ ], "require": { "php": "^5.6 || ^7.0", - "squizlabs/php_codesniffer": "^2.8" + "squizlabs/php_codesniffer": "^3.0" }, "require-dev": { "mikey179/vfsStream": "^1.6", @@ -16,12 +16,12 @@ }, "autoload": { "psr-4": { - "Zend\\CodingStandard\\": "src/ZendCodingStandard/" + "ZendCodingStandard\\": "src/ZendCodingStandard/" } }, "autoload-dev": { "psr-4": { - "ZendTest\\CodingStandard\\": "test/" + "ZendCodingStandard\\Test\\": "test/" } }, "scripts": { diff --git a/phpcs.xml b/phpcs.xml index a4afb374..56e65a2f 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -2,16 +2,14 @@ + + - src - test + ./COPYING.md + ./LICENSE.md + ./src + ./test */TestAsset/* - - - - - - diff --git a/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php index b12fa939..645f801e 100644 --- a/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php +++ b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php @@ -5,9 +5,11 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -use PHP_CodeSniffer_File as File; -use PHP_CodeSniffer_Sniff as Sniff; -use Zend\CodingStandard\Utils\LicenseUtils; +namespace ZendCodingStandard\Sniffs\Commenting; + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; +use ZendCodingStandard\Utils\LicenseUtils; /** * FileLevelDocBlock Sniff @@ -18,7 +20,7 @@ * - Checks for missing/invalid license tag * - Checks order of see, copyright and license tags */ -class ZendCodingStandard_Sniffs_Commenting_FileLevelDocBlockSniff implements Sniff +class FileLevelDocBlockSniff implements Sniff { /** * @var string @@ -48,8 +50,8 @@ class ZendCodingStandard_Sniffs_Commenting_FileLevelDocBlockSniff implements Sni public function __construct() { // Get current repo name from composer.json - $content = file_get_contents('composer.json'); - $content = json_decode($content, true); + $content = file_get_contents('composer.json'); + $content = json_decode($content, true); $this->repo = $content['name']; } @@ -68,12 +70,10 @@ public function register() * found. * * @param File $phpcsFile The PHP_CodeSniffer file where the token was found. - * @param int $stackPtr The position in the PHP_CodeSniffer file's token - * stack where the token was found. + * @param int $stackPtr The position in the PHP_CodeSniffer file's token stack where the token was found. * - * @return int Optionally returns a stack pointer. The sniff will not be - * called again on the current file until the returned stack - * pointer is reached. + * @return int Optionally returns a stack pointer. The sniff will not be called again on the current file until the + * returned stack pointer is reached. */ public function process(File $phpcsFile, $stackPtr) { @@ -82,7 +82,7 @@ public function process(File $phpcsFile, $stackPtr) return ($phpcsFile->numTokens + 1); } - $tokens = $phpcsFile->getTokens(); + $tokens = $phpcsFile->getTokens(); $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); // Valid file-level DocBlock style @@ -106,7 +106,7 @@ public function process(File $phpcsFile, $stackPtr) } $commentEnd = $tokens[$commentStart]['comment_closer']; - $nextToken = $phpcsFile->findNext(T_WHITESPACE, $commentEnd + 1, null, true); + $nextToken = $phpcsFile->findNext(T_WHITESPACE, $commentEnd + 1, null, true); // File-level DocBlock exists, part 2 if (in_array($tokens[$nextToken]['code'], self::IGNORE) === true) { @@ -129,7 +129,7 @@ public function process(File $phpcsFile, $stackPtr) $next = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), null, true); if ($tokens[$next]['line'] !== ($tokens[$commentEnd]['line'] + 2)) { $error = 'There must be exactly one blank line after the file-level DocBlock'; - $fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfterComment'); + $fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfterComment'); if ($fix === true) { $phpcsFile->fixer->addNewline($commentEnd); } @@ -144,12 +144,12 @@ public function process(File $phpcsFile, $stackPtr) $foundTags = []; foreach ($tokens[$commentStart]['comment_tags'] as $tag) { - $name = $tokens[$tag]['content']; + $name = $tokens[$tag]['content']; $isRequired = isset($required[$name]); if ($isRequired === true && in_array($name, $foundTags) === true) { $error = 'Only one %s tag is allowed in a file-level DocBlock'; - $data = [$name]; + $data = [$name]; $phpcsFile->addError($error, $tag, 'Duplicate' . ucfirst(substr($name, 1)) . 'Tag', $data); } @@ -157,7 +157,7 @@ public function process(File $phpcsFile, $stackPtr) if ($name === '@link') { $error = 'Deprecated @link tag is used, use @see tag instead'; - $fix = $phpcsFile->addFixableError($error, $tag, 'DeprecatedLinkTag'); + $fix = $phpcsFile->addFixableError($error, $tag, 'DeprecatedLinkTag'); if ($fix === true) { $phpcsFile->fixer->replaceToken($tag, '@see '); } @@ -170,7 +170,7 @@ public function process(File $phpcsFile, $stackPtr) $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd); if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) { $error = 'Content missing for %s tag in file-level DocBlock'; - $data = [$name]; + $data = [$name]; $phpcsFile->addError($error, $tag, 'Empty' . ucfirst(substr($name, 1)) . 'Tag', $data); continue; } @@ -179,7 +179,7 @@ public function process(File $phpcsFile, $stackPtr) $expected = sprintf('https://github.com/%s for the canonical source repository', $this->repo); if (preg_match('|^' . $expected . '$|', $tokens[$string]['content']) === 0) { $error = 'Expected "%s" for %s tag'; - $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectSourceLink', [$expected, $name]); + $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectSourceLink', [$expected, $name]); if ($fix === true) { $phpcsFile->fixer->replaceToken($string, $expected); } @@ -194,7 +194,7 @@ public function process(File $phpcsFile, $stackPtr) $expected = sprintf('https://github.com/%s/blob/master/COPYING.md Copyright', $this->repo); if (preg_match('|^' . $expected . '$|', $tokens[$string]['content']) === 0) { $error = 'Expected "%s" for @copyright tag'; - $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectCopyrightLink', [$expected]); + $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectCopyrightLink', [$expected]); if ($fix === true) { $phpcsFile->fixer->replaceToken($string, $expected); if ($firstYear !== null) { @@ -209,7 +209,7 @@ public function process(File $phpcsFile, $stackPtr) $expected = sprintf('https://github.com/%s/blob/master/LICENSE.md New BSD License', $this->repo); if (preg_match('|^' . $expected . '$|', $tokens[$string]['content']) === 0) { $error = 'Expected "%s" for @license tag'; - $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectLicenseLink', [$expected]); + $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectLicenseLink', [$expected]); if ($fix === true) { $phpcsFile->fixer->replaceToken($string, $expected); } @@ -230,7 +230,7 @@ public function process(File $phpcsFile, $stackPtr) foreach ($required as $tag => $true) { if (in_array($tag, $foundTags) === false) { $error = 'Missing %s tag in file-level DocBlock'; - $data = [$tag]; + $data = [$tag]; $phpcsFile->addError($error, $commentEnd, 'Missing' . ucfirst(substr($tag, 1)) . 'Tag', $data); } @@ -240,7 +240,7 @@ public function process(File $phpcsFile, $stackPtr) if ($foundTags[$pos] !== $tag) { $error = 'The file-level DocBlock tag in position %s should be the %s tag'; - $data = [ + $data = [ ($pos + 1), $tag, ]; diff --git a/src/ZendCodingStandard/Sniffs/Files/COPYING.md b/src/ZendCodingStandard/Sniffs/Files/COPYING.md deleted file mode 100644 index 2ba48f48..00000000 --- a/src/ZendCodingStandard/Sniffs/Files/COPYING.md +++ /dev/null @@ -1,13 +0,0 @@ -getFilename(), -10) !== $this->copyrightFile->getFilename()) { + if (substr($phpcsFile->getFilename(), -10) !== 'COPYING.md') { return ($phpcsFile->numTokens + 1); } if (! $this->copyrightFile->getRealPath()) { $error = 'Missing COPYING.md file in the component root dir'; - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingLicense'); + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingLicense'); if ($fix === true) { LicenseUtils::buildFiles(); } @@ -80,7 +81,7 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) 'Expected "Copyright (c) %s" in COPYING.md', LicenseUtils::formatDateRange($firstYear, gmdate('Y')) ); - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'InvalidCopyrightDate'); + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'InvalidCopyrightDate'); if ($fix === true) { LicenseUtils::buildFiles($firstYear, $lastYear); } diff --git a/src/ZendCodingStandard/Sniffs/Files/LICENSE.md b/src/ZendCodingStandard/Sniffs/Files/LICENSE.md deleted file mode 100644 index 34498305..00000000 --- a/src/ZendCodingStandard/Sniffs/Files/LICENSE.md +++ /dev/null @@ -1,10 +0,0 @@ -licenseFile->getRealPath()) { $error = 'Missing LICENSE.md file in the component root dir'; - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingLicense'); + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingLicense'); if ($fix === true) { LicenseUtils::buildFiles(); } @@ -79,7 +81,7 @@ public function process(File $phpcsFile, $stackPtr) 'Expected "Copyright (c) %s" in LICENSE.md', LicenseUtils::formatDateRange($firstYear, gmdate('Y')) ); - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'InvalidCopyrightDate'); + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'InvalidCopyrightDate'); if ($fix === true) { LicenseUtils::buildFiles($firstYear, $lastYear); } diff --git a/src/ZendCodingStandard/Utils/LicenseUtils.php b/src/ZendCodingStandard/Utils/LicenseUtils.php index 13f87453..c9b38273 100644 --- a/src/ZendCodingStandard/Utils/LicenseUtils.php +++ b/src/ZendCodingStandard/Utils/LicenseUtils.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace Zend\CodingStandard\Utils; +namespace ZendCodingStandard\Utils; use SplFileInfo; diff --git a/src/ZendCodingStandard/ruleset.xml b/src/ZendCodingStandard/ruleset.xml index 567c9d09..693bb60d 100644 --- a/src/ZendCodingStandard/ruleset.xml +++ b/src/ZendCodingStandard/ruleset.xml @@ -2,14 +2,11 @@ Zend Framework Coding Standard - + + + - Sniffs/Files/COPYING.md - Sniffs/Files/LICENSE.md - - - */TestAsset/* @@ -26,8 +23,17 @@ + + + *.md + + - - + + *.md + + + *.md + diff --git a/test/SniffTestCase.php b/test/SniffTestCase.php index 02b8fad7..8c3025c2 100644 --- a/test/SniffTestCase.php +++ b/test/SniffTestCase.php @@ -5,10 +5,10 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard; +namespace ZendCodingStandard\Test; use PHP_CodeSniffer; -use PHP_CodeSniffer_File as File; +use PHP_CodeSniffer\Files\File; use PHPUnit\Framework\TestCase; use PHPUnit\Util\InvalidArgumentHelper; use ReflectionProperty; diff --git a/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php b/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php index 946ac246..264d7f68 100644 --- a/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php +++ b/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php @@ -5,9 +5,9 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting; +namespace ZendCodingStandard\Test\Sniffs\Commenting; -use ZendTest\CodingStandard\SniffTestCase; +use ZendCodingStandard\Test\SniffTestCase; class FileLevelDocBlockSniffTest extends SniffTestCase { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.fixed.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.fixed.php index 3719e929..214d9415 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.fixed.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.fixed.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; class FileLevelDocBlockDeprecatedLinkTag { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.php index b2cf2230..6f54ff51 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; class FileLevelDocBlockDeprecatedLinkTag { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockEmptyTags.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockEmptyTags.php index 794f2987..e5fd570d 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockEmptyTags.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockEmptyTags.php @@ -5,7 +5,7 @@ * @license */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; class FileLevelDocBlockEmptyTags { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.fixed.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.fixed.php index b6d5e7a0..d5e1c83f 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.fixed.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.fixed.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectCopyrightLink { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.php index ed6df4e0..6694f52d 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectCopyrightLink { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.fixed.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.fixed.php index b46311e9..e6284f22 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.fixed.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.fixed.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectLicenseLink { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.php index c36f9597..8902fbd4 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.php @@ -5,7 +5,7 @@ * @license New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectLicenseLink { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php index eab97c1c..5c1500ef 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectSourceLink { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.php index daa0bf01..d05cf946 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendTest\CodingStandard\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectSourceLink { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissing.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissing.php index d8ae9fa7..78e1059c 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissing.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissing.php @@ -1,6 +1,6 @@ Date: Wed, 17 May 2017 22:26:21 +0200 Subject: [PATCH 18/25] Truncate copyright text per @weierophinney --- COPYING.md | 2 -- src/ZendCodingStandard/Utils/LicenseUtils.php | 2 -- 2 files changed, 4 deletions(-) diff --git a/COPYING.md b/COPYING.md index 21ff4cdb..5837b4b1 100644 --- a/COPYING.md +++ b/COPYING.md @@ -1,3 +1 @@ Copyright (c) 2016-2017, Zend Technologies USA, Inc. - -All rights reserved. diff --git a/src/ZendCodingStandard/Utils/LicenseUtils.php b/src/ZendCodingStandard/Utils/LicenseUtils.php index c9b38273..d00c5d26 100644 --- a/src/ZendCodingStandard/Utils/LicenseUtils.php +++ b/src/ZendCodingStandard/Utils/LicenseUtils.php @@ -19,8 +19,6 @@ class LicenseUtils public static $copyright = << Date: Wed, 17 May 2017 22:40:31 +0200 Subject: [PATCH 19/25] s/COPYING/COPYRIGHT/ per @weierophinney --- COPYING.md => COPYRIGHT.md | 0 phpcs.xml | 2 +- .../Commenting/FileLevelDocBlockSniff.php | 8 ++++---- .../{CopyingSniff.php => CopyrightSniff.php} | 18 +++++++++--------- .../Sniffs/Files/LicenseSniff.php | 2 +- src/ZendCodingStandard/Utils/LicenseUtils.php | 10 +++++----- src/ZendCodingStandard/ruleset.xml | 2 +- test/SniffTestCase.php | 2 +- .../Commenting/FileLevelDocBlockSniffTest.php | 2 +- ...ileLevelDocBlockDeprecatedLinkTag.fixed.php | 2 +- .../FileLevelDocBlockDeprecatedLinkTag.php | 2 +- ...velDocBlockIncorrectCopyrightLink.fixed.php | 2 +- ...LevelDocBlockIncorrectLicenseLink.fixed.php | 2 +- .../FileLevelDocBlockIncorrectLicenseLink.php | 2 +- ...eLevelDocBlockIncorrectSourceLink.fixed.php | 2 +- .../FileLevelDocBlockIncorrectSourceLink.php | 2 +- .../FileLevelDocBlockMissingCopyrightTag.php | 2 +- .../FileLevelDocBlockMissingLicenseTag.php | 2 +- .../FileLevelDocBlockMissingSeeTag.php | 2 +- .../TestAsset/FileLevelDocBlockPass.php | 2 +- ...eLevelDocBlockSpacingAfterComment.fixed.php | 2 +- .../FileLevelDocBlockSpacingAfterComment.php | 2 +- .../FileLevelDocBlockSpacingAfterOpen.php | 2 +- .../TestAsset/FileLevelDocBlockWrongStyle.php | 2 +- test/Util/LicenseUtilTest.php | 14 +++++++------- 25 files changed, 45 insertions(+), 45 deletions(-) rename COPYING.md => COPYRIGHT.md (100%) rename src/ZendCodingStandard/Sniffs/Files/{CopyingSniff.php => CopyrightSniff.php} (83%) diff --git a/COPYING.md b/COPYRIGHT.md similarity index 100% rename from COPYING.md rename to COPYRIGHT.md diff --git a/phpcs.xml b/phpcs.xml index 56e65a2f..bbb72b7c 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -5,7 +5,7 @@ - ./COPYING.md + ./COPYRIGHT.md ./LICENSE.md ./src ./test diff --git a/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php index 645f801e..3ba49245 100644 --- a/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php +++ b/src/ZendCodingStandard/Sniffs/Commenting/FileLevelDocBlockSniff.php @@ -1,7 +1,7 @@ getFilename(), -10), ['LICENSE.md', 'COPYING.md'])) { + // Skip license and copyright file + if (in_array(substr($phpcsFile->getFilename(), -10), ['LICENSE.md', 'COPYRIGHT.md'])) { return ($phpcsFile->numTokens + 1); } @@ -191,7 +191,7 @@ public function process(File $phpcsFile, $stackPtr) // Grab copyright date range list($firstYear, $lastYear) = LicenseUtils::detectDateRange($tokens[$string]['content']); - $expected = sprintf('https://github.com/%s/blob/master/COPYING.md Copyright', $this->repo); + $expected = sprintf('https://github.com/%s/blob/master/COPYRIGHT.md Copyright', $this->repo); if (preg_match('|^' . $expected . '$|', $tokens[$string]['content']) === 0) { $error = 'Expected "%s" for @copyright tag'; $fix = $phpcsFile->addFixableError($error, $tag, 'IncorrectCopyrightLink', [$expected]); diff --git a/src/ZendCodingStandard/Sniffs/Files/CopyingSniff.php b/src/ZendCodingStandard/Sniffs/Files/CopyrightSniff.php similarity index 83% rename from src/ZendCodingStandard/Sniffs/Files/CopyingSniff.php rename to src/ZendCodingStandard/Sniffs/Files/CopyrightSniff.php index f5dd099f..59c2bf5e 100644 --- a/src/ZendCodingStandard/Sniffs/Files/CopyingSniff.php +++ b/src/ZendCodingStandard/Sniffs/Files/CopyrightSniff.php @@ -1,7 +1,7 @@ getFilename(), -10) !== 'COPYING.md') { + // Skip all files except the copyright file + if (substr($phpcsFile->getFilename(), -10) !== 'COPYRIGHT.md') { return ($phpcsFile->numTokens + 1); } if (! $this->copyrightFile->getRealPath()) { - $error = 'Missing COPYING.md file in the component root dir'; + $error = 'Missing COPYRIGHT.md file in the component root dir'; $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingLicense'); if ($fix === true) { LicenseUtils::buildFiles(); @@ -78,7 +78,7 @@ public function process(File $phpcsFile, $stackPtr) || ($lastYear !== null && $lastYear !== gmdate('Y')) ) { $error = sprintf( - 'Expected "Copyright (c) %s" in COPYING.md', + 'Expected "Copyright (c) %s" in COPYRIGHT.md', LicenseUtils::formatDateRange($firstYear, gmdate('Y')) ); $fix = $phpcsFile->addFixableError($error, $stackPtr, 'InvalidCopyrightDate'); diff --git a/src/ZendCodingStandard/Sniffs/Files/LicenseSniff.php b/src/ZendCodingStandard/Sniffs/Files/LicenseSniff.php index 0f4dd445..7de60570 100644 --- a/src/ZendCodingStandard/Sniffs/Files/LicenseSniff.php +++ b/src/ZendCodingStandard/Sniffs/Files/LicenseSniff.php @@ -1,7 +1,7 @@ - + *.md diff --git a/test/SniffTestCase.php b/test/SniffTestCase.php index 8c3025c2..7c8bb515 100644 --- a/test/SniffTestCase.php +++ b/test/SniffTestCase.php @@ -1,7 +1,7 @@ root->url() . '/COPYING.tmp'); + $copyrightFile = new SplFileInfo($this->root->url() . '/COPYRIGHT.tmp'); $licenseFile = new SplFileInfo($this->root->url() . '/LICENSE.tmp'); LicenseUtils::buildFiles($firstYear, $lastYear, $copyrightFile, $licenseFile); - $this->assertTrue($this->root->hasChild('COPYING.tmp')); + $this->assertTrue($this->root->hasChild('COPYRIGHT.tmp')); $this->assertEquals( sprintf(LicenseUtils::$copyright, LicenseUtils::formatDateRange(gmdate('Y'))), - $this->root->getChild('COPYING.tmp')->getContent() + $this->root->getChild('COPYRIGHT.tmp')->getContent() ); $this->assertTrue($this->root->hasChild('LICENSE.tmp')); @@ -95,7 +95,7 @@ public function testBuildNewFiles() public function testUpdateBothFilesWithSameDates() { - $copyrightFile = new SplFileInfo($this->root->url() . '/COPYING.tmp'); + $copyrightFile = new SplFileInfo($this->root->url() . '/COPYRIGHT.tmp'); $licenseFile = new SplFileInfo($this->root->url() . '/LICENSE.tmp'); file_put_contents( @@ -110,10 +110,10 @@ public function testUpdateBothFilesWithSameDates() LicenseUtils::buildFiles('2016', '2016', $copyrightFile, $licenseFile); - $this->assertTrue($this->root->hasChild('COPYING.tmp')); + $this->assertTrue($this->root->hasChild('COPYRIGHT.tmp')); $this->assertEquals( sprintf(LicenseUtils::$copyright, LicenseUtils::formatDateRange('2015', gmdate('Y'))), - $this->root->getChild('COPYING.tmp')->getContent() + $this->root->getChild('COPYRIGHT.tmp')->getContent() ); $this->assertTrue($this->root->hasChild('LICENSE.tmp')); From fbd5073a0407d81749dfd89f2185f44bed49c360 Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Wed, 17 May 2017 22:54:02 +0200 Subject: [PATCH 20/25] Remove the dummy ruleset and use only the real ruleset --- ruleset.xml | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 ruleset.xml diff --git a/ruleset.xml b/ruleset.xml deleted file mode 100644 index 2ee7b3ea..00000000 --- a/ruleset.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - Zend Framework Coding Standard - - From a075478fefd16db5180c54c8afc5526c828819fd Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Thu, 18 May 2017 10:22:54 +0200 Subject: [PATCH 21/25] Update the TestCase for PHP_CodeSniffer 3 We still can't use the PHP_CodeSniffer test suite to run tests since it doesn't support PHPUnit 6 yet. Also I think it's nicer to check the exact errors by their id instead of a count to ensure the correct errors are triggered. Some autoload-dev hacks are needed since PHP_CodeSniffer includes its own autoloader and doesn't work with composer out of the box. --- composer.json | 74 ++++++++++--------- phpcs.xml | 4 +- test/SniffTestCase.php | 57 +++++++++----- .../Commenting/FileLevelDocBlockSniffTest.php | 12 +-- 4 files changed, 88 insertions(+), 59 deletions(-) diff --git a/composer.json b/composer.json index 9151c941..c3fbd7d5 100644 --- a/composer.json +++ b/composer.json @@ -1,38 +1,42 @@ { - "name": "zendframework/zend-coding-standard", - "description": "Zend Framework coding standard", - "license": "BSD-3-Clause", - "keywords": [ - "zf", - "coding standard" - ], - "require": { - "php": "^5.6 || ^7.0", - "squizlabs/php_codesniffer": "^3.0" - }, - "require-dev": { - "mikey179/vfsStream": "^1.6", - "phpunit/phpunit": "^6.1 || ^5.7.15" - }, - "autoload": { - "psr-4": { - "ZendCodingStandard\\": "src/ZendCodingStandard/" - } - }, - "autoload-dev": { - "psr-4": { - "ZendCodingStandard\\Test\\": "test/" - } - }, - "scripts": { - "check": [ - "@cs-check", - "@test" + "name": "zendframework/zend-coding-standard", + "description": "Zend Framework coding standard", + "license": "BSD-3-Clause", + "keywords": [ + "zf", + "coding standard" ], - "cs-check": "phpcs -s", - "cs-fix": "phpcbf", - "test": "phpunit --colors=always", - "test-coverage": "phpunit --coverage-clover clover.xml", - "upload-coverage": "coveralls -v" - } + "require": { + "php": "^5.6 || ^7.0", + "squizlabs/php_codesniffer": "^3.0" + }, + "require-dev": { + "mikey179/vfsStream": "^1.6", + "phpunit/phpunit": "^6.1 || ^5.7.15" + }, + "autoload": { + "psr-4": { + "ZendCodingStandard\\": "src/ZendCodingStandard/" + } + }, + "autoload-dev": { + "psr-4": { + "ZendCodingStandard\\Test\\": "test/", + "PHP_CodeSniffer\\": "vendor/squizlabs/php_codesniffer/src/" + }, + "files": [ + "vendor/squizlabs/php_codesniffer/autoload.php" + ] + }, + "scripts": { + "check": [ + "@cs-check", + "@test" + ], + "cs-check": "phpcs -s", + "cs-fix": "phpcbf", + "test": "phpunit --colors=always", + "test-coverage": "phpunit --coverage-clover clover.xml", + "upload-coverage": "coveralls -v" + } } diff --git a/phpcs.xml b/phpcs.xml index bbb72b7c..d24dc220 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,12 +1,14 @@ + - + ./COPYRIGHT.md ./LICENSE.md + ./src ./test diff --git a/test/SniffTestCase.php b/test/SniffTestCase.php index 7c8bb515..beccf42f 100644 --- a/test/SniffTestCase.php +++ b/test/SniffTestCase.php @@ -7,34 +7,57 @@ namespace ZendCodingStandard\Test; -use PHP_CodeSniffer; +use PHP_CodeSniffer\Config; use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Files\LocalFile; +use PHP_CodeSniffer\Ruleset; +use PHP_CodeSniffer\Util\Tokens; use PHPUnit\Framework\TestCase; use PHPUnit\Util\InvalidArgumentHelper; -use ReflectionProperty; class SniffTestCase extends TestCase { - public function processAsset($asset) + /** + * @var Config + */ + protected $config; + + protected function setUp() { - $phpcs = new PHP_CodeSniffer(); + parent::setUp(); + + // Init PHP_CodeSniffer + if (defined('PHP_CODESNIFFER_IN_TESTS') === false) { + define('PHP_CODESNIFFER_IN_TESTS', false); + } + if (defined('PHP_CODESNIFFER_CBF') === false) { + define('PHP_CODESNIFFER_CBF', false); + } + if (defined('PHP_CODESNIFFER_VERBOSITY') === false) { + define('PHP_CODESNIFFER_VERBOSITY', 0); + } - $standard = 'src/ZendCodingStandard/ruleset.xml'; - $options = array_merge($phpcs->cli->getDefaults(), [ - 'encoding' => 'utf-8', - 'files' => [$asset], - 'standard' => $standard, - 'showSources' => true, - ]); + // Define tokens + new Tokens(); - $reflection = new ReflectionProperty($phpcs->cli, 'values'); - $reflection->setAccessible(true); - $reflection->setValue($phpcs->cli, $options); + // Setup config + $this->config = new Config(); + + // Don't cache files + $this->config->cache = false; + + // Set ruleset + $this->config->standards = ['src/ZendCodingStandard/ruleset.xml']; + } + + public function processAsset($asset) + { + $ruleset = new Ruleset($this->config); - $phpcs->initStandard($standard, ['ZendCodingStandard.Commenting.FileLevelDocBlock']); - $phpcs->setIgnorePatterns([]); + $file = new LocalFile($asset, $ruleset, $this->config); + $file->process(); - return $phpcs->processFile($asset); + return $file; } public function assertErrorCount($expectedCount, File $file) diff --git a/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php b/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php index 551743b8..db4c3341 100644 --- a/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php +++ b/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php @@ -16,20 +16,20 @@ class FileLevelDocBlockSniffTest extends SniffTestCase */ public function testAssets($asset, $fixed, $errorCount, array $errors, $warningCount, array $warnings) { - $result = $this->processAsset($asset); + $file = $this->processAsset($asset); - $this->assertErrorCount($errorCount, $result); - $this->assertWarningCount($warningCount, $result); + $this->assertErrorCount($errorCount, $file); + $this->assertWarningCount($warningCount, $file); foreach ($errors as $error) { - $this->assertHasError($error, $result); + $this->assertHasError($error, $file); } foreach ($warnings as $warning) { - $this->assertHasWarning($warning, $result); + $this->assertHasWarning($warning, $file); } - $this->assertAssetCanBeFixed($fixed, $result); + $this->assertAssetCanBeFixed($fixed, $file); } public function assetsProvider() From 97b042025a717e812f69d3a419eb479d521cd899 Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Thu, 18 May 2017 10:29:35 +0200 Subject: [PATCH 22/25] Run coverage on PHP 7.1 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 438fe38a..f07267b7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,6 @@ matrix: - php: 5.6 env: - DEPS=locked - - TEST_COVERAGE=true - php: 5.6 env: - DEPS=latest @@ -43,6 +42,7 @@ matrix: - php: 7.1 env: - DEPS=latest + - TEST_COVERAGE=true before_install: - if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi From 542d86b987cefa99fb0e8e3b23f90bee6a7ec59f Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Thu, 18 May 2017 10:47:04 +0200 Subject: [PATCH 23/25] Use proper namespace for tests --- composer.json | 2 +- test/SniffTestCase.php | 2 +- test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php | 4 ++-- .../TestAsset/FileLevelDocBlockDeprecatedLinkTag.fixed.php | 2 +- .../TestAsset/FileLevelDocBlockDeprecatedLinkTag.php | 2 +- .../Commenting/TestAsset/FileLevelDocBlockEmptyTags.php | 2 +- .../FileLevelDocBlockIncorrectCopyrightLink.fixed.php | 2 +- .../TestAsset/FileLevelDocBlockIncorrectCopyrightLink.php | 2 +- .../TestAsset/FileLevelDocBlockIncorrectLicenseLink.fixed.php | 2 +- .../TestAsset/FileLevelDocBlockIncorrectLicenseLink.php | 2 +- .../TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php | 2 +- .../TestAsset/FileLevelDocBlockIncorrectSourceLink.php | 2 +- test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissing.php | 2 +- .../TestAsset/FileLevelDocBlockMissingCopyrightTag.php | 2 +- .../TestAsset/FileLevelDocBlockMissingLicenseTag.php | 2 +- .../Commenting/TestAsset/FileLevelDocBlockMissingSeeTag.php | 2 +- test/Sniffs/Commenting/TestAsset/FileLevelDocBlockPass.php | 2 +- .../TestAsset/FileLevelDocBlockSpacingAfterComment.fixed.php | 2 +- .../TestAsset/FileLevelDocBlockSpacingAfterComment.php | 2 +- .../TestAsset/FileLevelDocBlockSpacingAfterOpen.php | 2 +- .../Commenting/TestAsset/FileLevelDocBlockWrongStyle.php | 2 +- test/Util/LicenseUtilTest.php | 2 +- 22 files changed, 23 insertions(+), 23 deletions(-) diff --git a/composer.json b/composer.json index c3fbd7d5..9a40be87 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ }, "autoload-dev": { "psr-4": { - "ZendCodingStandard\\Test\\": "test/", + "ZendCodingStandardTest\\": "test/", "PHP_CodeSniffer\\": "vendor/squizlabs/php_codesniffer/src/" }, "files": [ diff --git a/test/SniffTestCase.php b/test/SniffTestCase.php index beccf42f..365a4cd5 100644 --- a/test/SniffTestCase.php +++ b/test/SniffTestCase.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendCodingStandard\Test; +namespace ZendCodingStandardTest; use PHP_CodeSniffer\Config; use PHP_CodeSniffer\Files\File; diff --git a/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php b/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php index db4c3341..b55518ca 100644 --- a/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php +++ b/test/Sniffs/Commenting/FileLevelDocBlockSniffTest.php @@ -5,9 +5,9 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendCodingStandard\Test\Sniffs\Commenting; +namespace ZendCodingStandardTest\Sniffs\Commenting; -use ZendCodingStandard\Test\SniffTestCase; +use ZendCodingStandardTest\SniffTestCase; class FileLevelDocBlockSniffTest extends SniffTestCase { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.fixed.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.fixed.php index d8a21c2b..6baac6ec 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.fixed.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.fixed.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandardTest\Sniffs\Commenting\TestAsset; class FileLevelDocBlockDeprecatedLinkTag { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.php index 507ada3b..b276fe91 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockDeprecatedLinkTag.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandardTest\Sniffs\Commenting\TestAsset; class FileLevelDocBlockDeprecatedLinkTag { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockEmptyTags.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockEmptyTags.php index e5fd570d..daa66267 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockEmptyTags.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockEmptyTags.php @@ -5,7 +5,7 @@ * @license */ -namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandardTest\Sniffs\Commenting\TestAsset; class FileLevelDocBlockEmptyTags { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.fixed.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.fixed.php index 87647aad..c391f155 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.fixed.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.fixed.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandardTest\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectCopyrightLink { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.php index 6694f52d..49c11821 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectCopyrightLink.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandardTest\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectCopyrightLink { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.fixed.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.fixed.php index b8460f62..e6de748e 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.fixed.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.fixed.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandardTest\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectLicenseLink { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.php index 63ee295e..6eeb1f61 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectLicenseLink.php @@ -5,7 +5,7 @@ * @license New BSD License */ -namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandardTest\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectLicenseLink { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php index 18d68545..92c3ab2a 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.fixed.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandardTest\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectSourceLink { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.php index ac4e648d..509fb5d9 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockIncorrectSourceLink.php @@ -5,7 +5,7 @@ * @license https://github.com/zendframework/zend-coding-standard/blob/master/LICENSE.md New BSD License */ -namespace ZendCodingStandard\Test\Sniffs\Commenting\TestAsset; +namespace ZendCodingStandardTest\Sniffs\Commenting\TestAsset; class FileLevelDocBlockIncorrectSourceLink { diff --git a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissing.php b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissing.php index 78e1059c..f3892ae7 100644 --- a/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissing.php +++ b/test/Sniffs/Commenting/TestAsset/FileLevelDocBlockMissing.php @@ -1,6 +1,6 @@ Date: Thu, 18 May 2017 11:29:31 +0200 Subject: [PATCH 24/25] Use only 1 autoloader when running tests --- composer.json | 8 ++------ phpunit.xml.dist | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 9a40be87..e7b58df0 100644 --- a/composer.json +++ b/composer.json @@ -21,12 +21,8 @@ }, "autoload-dev": { "psr-4": { - "ZendCodingStandardTest\\": "test/", - "PHP_CodeSniffer\\": "vendor/squizlabs/php_codesniffer/src/" - }, - "files": [ - "vendor/squizlabs/php_codesniffer/autoload.php" - ] + "ZendCodingStandardTest\\": "test/" + } }, "scripts": { "check": [ diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d5ec939c..be783ebf 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,7 @@ From e8e2d85055647b44ea92046ecc5e9a7a9aab5c50 Mon Sep 17 00:00:00 2001 From: Geert Eltink Date: Thu, 18 May 2017 12:19:18 +0200 Subject: [PATCH 25/25] Disable test coverage There is an issue in PHP_CodeSniffer::Autoload which causes classes to be loaded again during code coverage. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f07267b7..c454a821 100644 --- a/.travis.yml +++ b/.travis.yml @@ -42,7 +42,7 @@ matrix: - php: 7.1 env: - DEPS=latest - - TEST_COVERAGE=true + #- TEST_COVERAGE=true before_install: - if [[ $TEST_COVERAGE != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi