diff --git a/.gitattributes b/.gitattributes index 446c698e..49f1e1b6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -14,6 +14,7 @@ /phpcs.xml.dist export-ignore /phpunit.xml export-ignore /phpunit.xml.dist export-ignore +/.github export-ignore /Yoast/Tests export-ignore # @@ -27,3 +28,4 @@ # *.md text *.php text +*.inc text diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index 17542c91..c7bde9e4 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -5,6 +5,13 @@ The Coding standard for the YoastCS Coding Standard itself. + + . @@ -14,6 +21,19 @@ + + + + + + + + + @@ -32,11 +52,19 @@ + + + + diff --git a/.travis.yml b/.travis.yml index 0f080913..e3870a0e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ php: - 7.1 - 7.2 - 7.3 - - "7.4snapshot" + - 7.4 env: # Test against the highest/lowest supported PHPCS and WPCS versions. @@ -67,9 +67,9 @@ matrix: # This is a much quicker test which only runs the unit tests and linting against low/high # supported PHP/PHPCS/WPCS combinations. - stage: quicktest - php: 7.3 + php: 7.4 env: PHPCS_BRANCH="dev-master" WPCS="dev-develop" PHPLINT=1 - - php: 7.3 + - php: 7.4 env: PHPCS_BRANCH="3.5.0" WPCS="2.2.0" - php: 5.4 env: PHPCS_BRANCH="dev-master" WPCS="2.2.0" PHPLINT=1 diff --git a/CHANGELOG.md b/CHANGELOG.md index bdb49937..b9298fd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/) and [Keep a CHANGELOG](https://keepachangelog.com/). +### [2.0.1] - 2020-02-06 + +#### Changed +* Composer: Supported version of the [DealerDirect Composer PHPCS plugin] has been changed from `^0.5.0` to `^0.5 || ^0.6`. + Note: this requirement is flexible to prevent conflicts with included standards which may include the plugin as well. +* Various housekeeping. + +#### Fixed +* `Yoast.NamingConventions.NamespaceName`: fixed a potential "undefined index" notice. + + ### [2.0.0] - 2019-12-17 #### Added @@ -358,6 +369,7 @@ Initial public release as a stand-alone package. [DealerDirect Composer PHPCS plugin]: https://github.com/Dealerdirect/phpcodesniffer-composer-installer/releases [Parallel-Lint]: https://packagist.org/packages/jakub-onderka/php-parallel-lint +[2.0.1]: https://github.com/Yoast/yoastcs/compare/2.0.0...2.0.1 [2.0.0]: https://github.com/Yoast/yoastcs/compare/1.3.0...2.0.0 [1.3.0]: https://github.com/Yoast/yoastcs/compare/1.2.2...1.3.0 [1.2.2]: https://github.com/Yoast/yoastcs/compare/1.2.1...1.2.2 diff --git a/README.md b/README.md index b52c57ab..74bc766a 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Composer will automatically install dependencies, register standards paths, and To include standards as part of a project require them as development dependencies: ```bash -composer require --dev yoast/yoastcs:^1.0 +composer require --dev yoast/yoastcs:^2.0 ``` Composer will automatically install dependencies and register the YoastCS and other external standards with PHP_CodeSniffer. diff --git a/Yoast/Sniffs/Commenting/CodeCoverageIgnoreDeprecatedSniff.php b/Yoast/Sniffs/Commenting/CodeCoverageIgnoreDeprecatedSniff.php index 7da47be2..8bd93e9d 100644 --- a/Yoast/Sniffs/Commenting/CodeCoverageIgnoreDeprecatedSniff.php +++ b/Yoast/Sniffs/Commenting/CodeCoverageIgnoreDeprecatedSniff.php @@ -20,11 +20,11 @@ class CodeCoverageIgnoreDeprecatedSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. * - * @return array + * @return (int|string)[] */ public function register() { return [ - T_FUNCTION, + \T_FUNCTION, ]; } @@ -41,14 +41,14 @@ public function process( File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); $find = Tokens::$methodPrefixes; - $find[] = T_WHITESPACE; + $find[] = \T_WHITESPACE; $commentEnd = $stackPtr; do { $commentEnd = $phpcsFile->findPrevious( $find, ( $commentEnd - 1 ), null, true ); } while ( $tokens[ $commentEnd ]['line'] === $tokens[ $stackPtr ]['line'] ); - if ( $tokens[ $commentEnd ]['code'] !== T_DOC_COMMENT_CLOSE_TAG + if ( $tokens[ $commentEnd ]['code'] !== \T_DOC_COMMENT_CLOSE_TAG || $tokens[ $commentEnd ]['line'] !== ( $tokens[ $stackPtr ]['line'] - 1 ) ) { // Function without (proper) docblock. Not our concern. @@ -83,10 +83,10 @@ public function process( File $phpcsFile, $stackPtr ) { return; } - $hasTagAsString = $phpcsFile->findNext( T_DOC_COMMENT_STRING, ( $commentStart + 1 ), $commentEnd, false, 'codeCoverageIgnore' ); + $hasTagAsString = $phpcsFile->findNext( \T_DOC_COMMENT_STRING, ( $commentStart + 1 ), $commentEnd, false, 'codeCoverageIgnore' ); if ( $hasTagAsString !== false ) { - $prev = $phpcsFile->findPrevious( T_DOC_COMMENT_WHITESPACE, ( $hasTagAsString - 1 ), $commentStart, true ); - if ( $prev !== false && $tokens[ $prev ]['code'] === T_DOC_COMMENT_STAR ) { + $prev = $phpcsFile->findPrevious( \T_DOC_COMMENT_WHITESPACE, ( $hasTagAsString - 1 ), $commentStart, true ); + if ( $prev !== false && $tokens[ $prev ]['code'] === \T_DOC_COMMENT_STAR ) { $fix = $phpcsFile->addFixableError( 'The `codeCoverageIgnore` annotation in the function docblock needs to be prefixed with an `@`.', $hasTagAsString, diff --git a/Yoast/Sniffs/Commenting/CoversTagSniff.php b/Yoast/Sniffs/Commenting/CoversTagSniff.php index 173ca4b0..0fa8158d 100644 --- a/Yoast/Sniffs/Commenting/CoversTagSniff.php +++ b/Yoast/Sniffs/Commenting/CoversTagSniff.php @@ -42,11 +42,11 @@ class CoversTagSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. * - * @return array + * @return (int|string)[] */ public function register() { return [ - T_DOC_COMMENT_OPEN_TAG, + \T_DOC_COMMENT_OPEN_TAG, ]; } @@ -80,8 +80,8 @@ public function process( File $phpcsFile, $stackPtr ) { } // Found a @covers tag. - $next = $phpcsFile->findNext( T_DOC_COMMENT_WHITESPACE, ( $tag + 1 ), null, true ); - if ( $tokens[ $next ]['code'] !== T_DOC_COMMENT_STRING + $next = $phpcsFile->findNext( \T_DOC_COMMENT_WHITESPACE, ( $tag + 1 ), null, true ); + if ( $tokens[ $next ]['code'] !== \T_DOC_COMMENT_STRING || $tokens[ $next ]['line'] !== $tokens[ $tag ]['line'] ) { $phpcsFile->addError( @@ -96,7 +96,7 @@ public function process( File $phpcsFile, $stackPtr ) { $annotation = $tokens[ $next ]['content']; $coversTags[ "$tag-$next" ] = $annotation; - if ( preg_match( '`^' . self::VALID_CONTENT_REGEX . '$`', $annotation ) === 1 ) { + if ( \preg_match( '`^' . self::VALID_CONTENT_REGEX . '$`', $annotation ) === 1 ) { continue; } @@ -107,7 +107,7 @@ public function process( File $phpcsFile, $stackPtr ) { $errorThrown = false; // Check for Union/Intersect types. - if ( strpos( $annotation, '&' ) !== false ) { + if ( \strpos( $annotation, '&' ) !== false ) { if ( $this->fixAnnotationToSplit( $phpcsFile, $next, 'IntersectFound', '&' ) === true ) { continue; } @@ -115,7 +115,7 @@ public function process( File $phpcsFile, $stackPtr ) { $errorThrown = true; } - if ( strpos( $annotation, '|' ) !== false ) { + if ( \strpos( $annotation, '|' ) !== false ) { if ( $this->fixAnnotationToSplit( $phpcsFile, $next, 'UnionFound', '|' ) === true ) { continue; } @@ -124,16 +124,15 @@ public function process( File $phpcsFile, $stackPtr ) { } // Parentheses/Braces at the end of the annotation. - $expected = rtrim( $annotation, '(){} ' ); + $expected = \rtrim( $annotation, '(){} ' ); if ( $this->fixSimpleError( $phpcsFile, $next, $expected, 'InvalidBraces' ) === true ) { $errorThrown = true; - } // Incorrect `` annotation. - if ( preg_match( '`::[{(\[]?(!)?(public|protected|private)[})\]]?`', $annotation, $matches ) === 1 ) { + if ( \preg_match( '`::[{(\[]?(!)?(public|protected|private)[})\]]?`', $annotation, $matches ) === 1 ) { $replacement = '::<' . $matches[1] . $matches[2] . '>'; - $expected = str_replace( $matches[0], $replacement, $annotation ); + $expected = \str_replace( $matches[0], $replacement, $annotation ); if ( $this->fixSimpleError( $phpcsFile, $next, $expected, 'InvalidFunctionGroup' ) === true ) { $errorThrown = true; @@ -152,7 +151,7 @@ public function process( File $phpcsFile, $stackPtr ) { $phpcsFile->addError( $error, $next, 'Invalid', $data ); } - $coversNothingCount = count( $coversNothingTags ); + $coversNothingCount = \count( $coversNothingTags ); if ( $firstCoversTag !== false && $coversNothingCount > 0 ) { $error = 'A test can\'t both cover something as well as cover nothing. First @coversNothing tag encountered on line %d; first @covers tag encountered on line %d'; $data = [ @@ -166,11 +165,10 @@ public function process( File $phpcsFile, $stackPtr ) { if ( $coversNothingCount > 1 ) { $error = 'Only one @coversNothing tag allowed per test'; $code = 'DuplicateCoversNothing'; - $fixable = true; $removeTags = []; - foreach ( $coversNothingTags as $position => $ptr ) { + foreach ( $coversNothingTags as $ptr ) { $next = ( $ptr + 1 ); - if ( $tokens[ $next ]['code'] === T_DOC_COMMENT_WHITESPACE + if ( $tokens[ $next ]['code'] === \T_DOC_COMMENT_WHITESPACE && $tokens[ $next ]['content'] === $phpcsFile->eolChar ) { // No comment, ok to remove. @@ -178,7 +176,7 @@ public function process( File $phpcsFile, $stackPtr ) { } } - $removalCount = count( $removeTags ); + $removalCount = \count( $removeTags ); if ( ( $coversNothingCount - $removalCount ) > 1 ) { // More than one tag had a comment. $phpcsFile->addError( $error, $tokens[ $stackPtr ]['comment_closer'], $code ); @@ -212,11 +210,11 @@ public function process( File $phpcsFile, $stackPtr ) { } } - $coversCount = count( $coversTags ); + $coversCount = \count( $coversTags ); if ( $coversCount > 1 ) { - $unique = array_unique( $coversTags ); - if ( count( $unique ) !== $coversCount ) { - $value_count = array_count_values( $coversTags ); + $unique = \array_unique( $coversTags ); + if ( \count( $unique ) !== $coversCount ) { + $value_count = \array_count_values( $coversTags ); $error = 'Duplicate @covers tag found. First tag with the same annotation encountered on line %d'; $code = 'DuplicateCovers'; foreach ( $value_count as $annotation => $count ) { @@ -231,12 +229,12 @@ public function process( File $phpcsFile, $stackPtr ) { } if ( ! isset( $first ) ) { - $first = explode( '-', $ptrs ); + $first = \explode( '-', $ptrs ); $data = [ $tokens[ $first[0] ]['line'] ]; continue; } - $ptrs = explode( '-', $ptrs ); + $ptrs = \explode( '-', $ptrs ); $fix = $phpcsFile->addFixableError( $error, $ptrs[0], $code, $data ); if ( $fix === true ) { @@ -246,7 +244,7 @@ public function process( File $phpcsFile, $stackPtr ) { // Remove the whole line. for ( $i = ( $ptrs[1] ); $i >= 0; $i-- ) { if ( $tokens[ $i ]['line'] !== $tokens[ $ptrs[1] ]['line'] ) { - if ( $tokens[ $i ]['code'] === T_DOC_COMMENT_WHITESPACE + if ( $tokens[ $i ]['code'] === \T_DOC_COMMENT_WHITESPACE && $tokens[ $i ]['content'] === $phpcsFile->eolChar ) { $phpcsFile->fixer->replaceToken( $i, '' ); @@ -282,7 +280,7 @@ protected function fixSimpleError( File $phpcsFile, $stackPtr, $expected, $error $annotation = $tokens[ $stackPtr ]['content']; if ( $expected === $annotation - || preg_match( '`^' . self::VALID_CONTENT_REGEX . '$`', $expected ) !== 1 + || \preg_match( '`^' . self::VALID_CONTENT_REGEX . '$`', $expected ) !== 1 ) { return false; } @@ -323,9 +321,9 @@ protected function fixAnnotationToSplit( File $phpcsFile, $stackPtr, $errorCode, if ( $fix === true ) { $tokens = $phpcsFile->getTokens(); $annotation = $tokens[ $stackPtr ]['content']; - $annotations = explode( $separator, $annotation ); - $annotations = array_map( 'trim', $annotations ); - $annotations = array_filter( $annotations ); // Remove empties. + $annotations = \explode( $separator, $annotation ); + $annotations = \array_map( 'trim', $annotations ); + $annotations = \array_filter( $annotations ); // Remove empties. $phpcsFile->fixer->beginChangeset(); $phpcsFile->fixer->replaceToken( $stackPtr, '' ); diff --git a/Yoast/Sniffs/Commenting/FileCommentSniff.php b/Yoast/Sniffs/Commenting/FileCommentSniff.php index 6a112b57..efd7e763 100644 --- a/Yoast/Sniffs/Commenting/FileCommentSniff.php +++ b/Yoast/Sniffs/Commenting/FileCommentSniff.php @@ -40,7 +40,7 @@ class FileCommentSniff extends Squiz_FileCommentSniff { */ public function process( File $phpcsFile, $stackPtr ) { - $namespace_token = $phpcsFile->findNext( T_NAMESPACE, $stackPtr ); + $namespace_token = $phpcsFile->findNext( \T_NAMESPACE, $stackPtr ); if ( $namespace_token === false ) { // No namespace found, fall through to parent sniff. return parent::process( $phpcsFile, $stackPtr ); @@ -50,18 +50,18 @@ public function process( File $phpcsFile, $stackPtr ) { $next_non_empty = $phpcsFile->findNext( Tokens::$emptyTokens, ( $namespace_token + 1 ), null, true ); if ( $next_non_empty === false - || $tokens[ $next_non_empty ]['code'] === T_SEMICOLON - || $tokens[ $next_non_empty ]['code'] === T_OPEN_CURLY_BRACKET - || $tokens[ $next_non_empty ]['code'] === T_NS_SEPARATOR + || $tokens[ $next_non_empty ]['code'] === \T_SEMICOLON + || $tokens[ $next_non_empty ]['code'] === \T_OPEN_CURLY_BRACKET + || $tokens[ $next_non_empty ]['code'] === \T_NS_SEPARATOR ) { // Either live coding, global namespace (i.e. not really namespaced) or namespace operator. // Fall through to parent sniff. return parent::process( $phpcsFile, $stackPtr ); } - $comment_start = $phpcsFile->findNext( T_WHITESPACE, ( $stackPtr + 1 ), $namespace_token, true ); + $comment_start = $phpcsFile->findNext( \T_WHITESPACE, ( $stackPtr + 1 ), $namespace_token, true ); - if ( $tokens[ $comment_start ]['code'] === T_DOC_COMMENT_OPEN_TAG ) { + if ( $tokens[ $comment_start ]['code'] === \T_DOC_COMMENT_OPEN_TAG ) { $phpcsFile->addWarning( 'A file containing a (named) namespace declaration does not need a file docblock', $comment_start, diff --git a/Yoast/Sniffs/Commenting/TestsHaveCoversTagSniff.php b/Yoast/Sniffs/Commenting/TestsHaveCoversTagSniff.php index c8ec5527..f6279c94 100644 --- a/Yoast/Sniffs/Commenting/TestsHaveCoversTagSniff.php +++ b/Yoast/Sniffs/Commenting/TestsHaveCoversTagSniff.php @@ -19,12 +19,12 @@ class TestsHaveCoversTagSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. * - * @return array + * @return (int|string)[] */ public function register() { return [ - T_CLASS, - T_FUNCTION, + \T_CLASS, + \T_FUNCTION, ]; } @@ -43,11 +43,11 @@ public function process( File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); - if ( $tokens[ $stackPtr ]['code'] === T_CLASS ) { + if ( $tokens[ $stackPtr ]['code'] === \T_CLASS ) { return $this->process_class( $phpcsFile, $stackPtr ); } - if ( $tokens[ $stackPtr ]['code'] === T_FUNCTION ) { + if ( $tokens[ $stackPtr ]['code'] === \T_FUNCTION ) { return $this->process_function( $phpcsFile, $stackPtr ); } } @@ -66,7 +66,7 @@ protected function process_class( File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); $name = $phpcsFile->getDeclarationName( $stackPtr ); - if ( substr( $name, -4 ) !== 'Test' ) { + if ( \substr( $name, -4 ) !== 'Test' ) { // Not a test class. if ( isset( $tokens[ $stackPtr ]['scope_closer'] ) ) { // No need to examine the methods in this class. @@ -78,9 +78,9 @@ protected function process_class( File $phpcsFile, $stackPtr ) { // @todo: Once PHPCS 3.5.0 is out, replace with call to new findCommentAboveOOStructure() method. $find = [ - T_WHITESPACE, - T_ABSTRACT, - T_FINAL, + \T_WHITESPACE, + \T_ABSTRACT, + \T_FINAL, ]; $commentEnd = $stackPtr; @@ -88,7 +88,7 @@ protected function process_class( File $phpcsFile, $stackPtr ) { $commentEnd = $phpcsFile->findPrevious( $find, ( $commentEnd - 1 ), null, true ); } while ( $tokens[ $commentEnd ]['line'] === $tokens[ $stackPtr ]['line'] ); - if ( $tokens[ $commentEnd ]['code'] !== T_DOC_COMMENT_CLOSE_TAG + if ( $tokens[ $commentEnd ]['code'] !== \T_DOC_COMMENT_CLOSE_TAG || $tokens[ $commentEnd ]['line'] !== ( $tokens[ $stackPtr ]['line'] - 1 ) ) { // Class without (proper) docblock. Not our concern. @@ -131,14 +131,14 @@ protected function process_function( File $phpcsFile, $stackPtr ) { // @todo: Once PHPCS 3.5.0 is out, replace with call to new findCommentAboveOOStructure() method. $find = Tokens::$methodPrefixes; - $find[] = T_WHITESPACE; + $find[] = \T_WHITESPACE; $commentEnd = $stackPtr; do { $commentEnd = $phpcsFile->findPrevious( $find, ( $commentEnd - 1 ), null, true ); } while ( $tokens[ $commentEnd ]['line'] === $tokens[ $stackPtr ]['line'] ); - if ( $tokens[ $commentEnd ]['code'] !== T_DOC_COMMENT_CLOSE_TAG + if ( $tokens[ $commentEnd ]['code'] !== \T_DOC_COMMENT_CLOSE_TAG || $tokens[ $commentEnd ]['line'] !== ( $tokens[ $stackPtr ]['line'] - 1 ) ) { // Function without (proper) docblock. Not our concern. @@ -168,7 +168,7 @@ protected function process_function( File $phpcsFile, $stackPtr ) { } $name = $phpcsFile->getDeclarationName( $stackPtr ); - if ( stripos( $name, 'test' ) !== 0 && $foundTest === false ) { + if ( \stripos( $name, 'test' ) !== 0 && $foundTest === false ) { // Not a test method. return; } diff --git a/Yoast/Sniffs/ControlStructures/IfElseDeclarationSniff.php b/Yoast/Sniffs/ControlStructures/IfElseDeclarationSniff.php index d9e68782..5c654da1 100644 --- a/Yoast/Sniffs/ControlStructures/IfElseDeclarationSniff.php +++ b/Yoast/Sniffs/ControlStructures/IfElseDeclarationSniff.php @@ -20,12 +20,12 @@ class IfElseDeclarationSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. * - * @return array + * @return (int|string)[] */ public function register() { return [ - T_ELSE, - T_ELSEIF, + \T_ELSE, + \T_ELSEIF, ]; } @@ -47,24 +47,24 @@ public function process( File $phpcsFile, $stackPtr ) { else { // Deal with "else if". $next = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); - if ( $tokens[ $next ]['code'] === T_IF && isset( $tokens[ $next ]['scope_opener'] ) ) { + if ( $tokens[ $next ]['code'] === \T_IF && isset( $tokens[ $next ]['scope_opener'] ) ) { $scope_open = $tokens[ $next ]['scope_opener']; } } - if ( ! isset( $scope_open ) || $tokens[ $scope_open ]['code'] === T_COLON ) { + if ( ! isset( $scope_open ) || $tokens[ $scope_open ]['code'] === \T_COLON ) { // No scope opener found or alternative syntax (not our concern). return; } - $previous_scope_closer = $phpcsFile->findPrevious( T_CLOSE_CURLY_BRACKET, ( $stackPtr - 1 ) ); + $previous_scope_closer = $phpcsFile->findPrevious( \T_CLOSE_CURLY_BRACKET, ( $stackPtr - 1 ) ); if ( $tokens[ $previous_scope_closer ]['line'] === $tokens[ $stackPtr ]['line'] ) { $phpcsFile->addError( '%s statement must be on a new line', $stackPtr, 'NewLine', - [ ucfirst( $tokens[ $stackPtr ]['content'] ) ] + [ \ucfirst( $tokens[ $stackPtr ]['content'] ) ] ); } elseif ( $tokens[ $previous_scope_closer ]['column'] !== $tokens[ $stackPtr ]['column'] ) { @@ -72,7 +72,7 @@ public function process( File $phpcsFile, $stackPtr ) { '%s statement not aligned with previous part of the control structure', $stackPtr, 'Alignment', - [ ucfirst( $tokens[ $stackPtr ]['content'] ) ] + [ \ucfirst( $tokens[ $stackPtr ]['content'] ) ] ); } @@ -82,7 +82,7 @@ public function process( File $phpcsFile, $stackPtr ) { $error = 'Nothing but whitespace and comments allowed between closing bracket and %s statement, found "%s"'; $data = [ $tokens[ $stackPtr ]['content'], - trim( $phpcsFile->getTokensAsString( ( $previous_scope_closer + 1 ), ( $stackPtr - ( $previous_scope_closer + 1 ) ) ) ), + \trim( $phpcsFile->getTokensAsString( ( $previous_scope_closer + 1 ), ( $stackPtr - ( $previous_scope_closer + 1 ) ) ) ), ]; $phpcsFile->addError( $error, $stackPtr, 'StatementFound', $data ); } diff --git a/Yoast/Sniffs/Files/FileNameSniff.php b/Yoast/Sniffs/Files/FileNameSniff.php index 5e499e2d..19f59d2e 100644 --- a/Yoast/Sniffs/Files/FileNameSniff.php +++ b/Yoast/Sniffs/Files/FileNameSniff.php @@ -62,21 +62,21 @@ class FileNameSniff implements Sniff { /** * Object tokens to search for in a file. * - * @var array + * @var (int|string)[] */ private $oo_tokens = [ - T_CLASS, - T_INTERFACE, - T_TRAIT, + \T_CLASS, + \T_INTERFACE, + \T_TRAIT, ]; /** * Returns an array of tokens this test wants to listen for. * - * @return array + * @return (int|string)[] */ public function register() { - return [ T_OPEN_TAG ]; + return [ \T_OPEN_TAG ]; } /** @@ -91,13 +91,13 @@ public function register() { */ public function process( File $phpcsFile, $stackPtr ) { // Stripping potential quotes to ensure `stdin_path` passed by IDEs does not include quotes. - $file = preg_replace( '`^([\'"])(.*)\1$`Ds', '$2', $phpcsFile->getFileName() ); + $file = \preg_replace( '`^([\'"])(.*)\1$`Ds', '$2', $phpcsFile->getFileName() ); if ( $file === 'STDIN' ) { return; } - $path_info = pathinfo( $file ); + $path_info = \pathinfo( $file ); // Basename = filename + extension. $basename = ''; @@ -117,7 +117,7 @@ public function process( File $phpcsFile, $stackPtr ) { $error = 'Filenames should be all lowercase with hyphens as word separators. Expected %s, but found %s.'; $error_code = 'NotHyphenatedLowercase'; - $expected = strtolower( str_replace( '_', '-', $file_name ) ); + $expected = \strtolower( \str_replace( '_', '-', $file_name ) ); if ( $this->is_file_excluded( $phpcsFile, $file ) === false ) { $oo_structure = $phpcsFile->findNext( $this->oo_tokens, $stackPtr ); @@ -129,52 +129,52 @@ public function process( File $phpcsFile, $stackPtr ) { $prefixes = $this->clean_custom_array_property( $this->oo_prefixes ); if ( ! empty( $prefixes ) ) { // Use reverse natural sorting to get the longest of overlapping prefixes first. - rsort( $prefixes, ( SORT_NATURAL | SORT_FLAG_CASE ) ); + \rsort( $prefixes, ( \SORT_NATURAL | \SORT_FLAG_CASE ) ); foreach ( $prefixes as $prefix ) { - if ( $name !== $prefix && stripos( $name, $prefix ) === 0 ) { - $name = substr( $name, strlen( $prefix ) ); - $name = ltrim( $name, '_-' ); + if ( $name !== $prefix && \stripos( $name, $prefix ) === 0 ) { + $name = \substr( $name, \strlen( $prefix ) ); + $name = \ltrim( $name, '_-' ); break; } } } - $expected = strtolower( str_replace( '_', '-', $name ) ); + $expected = \strtolower( \str_replace( '_', '-', $name ) ); switch ( $tokens[ $oo_structure ]['code'] ) { - case T_CLASS: + case \T_CLASS: $error = 'Class file names should be based on the class name without the plugin prefix. Expected %s, but found %s.'; $error_code = 'InvalidClassFileName'; break; - case T_INTERFACE: + case \T_INTERFACE: $error = 'Interface file names should be based on the interface name without the plugin prefix and should have "-interface" as a suffix. Expected %s, but found %s.'; $error_code = 'InvalidInterfaceFileName'; // Don't duplicate "interface" in the filename. - if ( substr( $expected, -10 ) !== '-interface' ) { + if ( \substr( $expected, -10 ) !== '-interface' ) { $expected .= '-interface'; } break; - case T_TRAIT: + case \T_TRAIT: $error = 'Trait file names should be based on the trait name without the plugin prefix and should have "-trait" as a suffix. Expected %s, but found %s.'; $error_code = 'InvalidTraitFileName'; // Don't duplicate "trait" in the filename. - if ( substr( $expected, -6 ) !== '-trait' ) { + if ( \substr( $expected, -6 ) !== '-trait' ) { $expected .= '-trait'; } break; } } else { - $has_function = $phpcsFile->findNext( T_FUNCTION, $stackPtr ); + $has_function = $phpcsFile->findNext( \T_FUNCTION, $stackPtr ); if ( $has_function !== false ) { $error = 'Files containing function declarations should have "-functions" as a suffix. Expected %s, but found %s.'; $error_code = 'InvalidFunctionsFileName'; - if ( substr( $expected, -10 ) !== '-functions' ) { + if ( \substr( $expected, -10 ) !== '-functions' ) { $expected .= '-functions'; } } @@ -211,7 +211,7 @@ protected function is_file_excluded( File $phpcsFile, $path_to_file ) { $exclude = $this->clean_custom_array_property( $this->excluded_files_strict_check, true, true ); if ( ! empty( $exclude ) ) { - $exclude = array_map( [ $this, 'normalize_directory_separators' ], $exclude ); + $exclude = \array_map( [ $this, 'normalize_directory_separators' ], $exclude ); $path_to_file = $this->normalize_directory_separators( $path_to_file ); if ( ! isset( $phpcsFile->config->basepath ) ) { @@ -227,7 +227,7 @@ protected function is_file_excluded( File $phpcsFile, $path_to_file ) { } // Lowercase the filename to not interfere with the lowercase/dashes rule. - $path_to_file = strtolower( ltrim( $path_to_file, '/' ) ); + $path_to_file = \strtolower( \ltrim( $path_to_file, '/' ) ); if ( isset( $exclude[ $path_to_file ] ) ) { // Filename is on the exclude list. @@ -251,17 +251,17 @@ protected function is_file_excluded( File $phpcsFile, $path_to_file ) { * @param bool $flip Whether to flip the array values to keys. * @param bool $to_lower Whether to lowercase the array values. * - * @return array + * @return (string|bool)[] */ protected function clean_custom_array_property( $property, $flip = false, $to_lower = false ) { - $property = array_filter( array_map( 'trim', $property ) ); + $property = \array_filter( \array_map( 'trim', $property ) ); if ( $to_lower === true ) { - $property = array_map( 'strtolower', $property ); + $property = \array_map( 'strtolower', $property ); } if ( $flip === true ) { - $property = array_fill_keys( $property, false ); + $property = \array_fill_keys( $property, false ); } return $property; @@ -275,6 +275,6 @@ protected function clean_custom_array_property( $property, $flip = false, $to_lo * @return string */ private function normalize_directory_separators( $path ) { - return ltrim( strtr( $path, '\\', '/' ), '/' ); + return \ltrim( \strtr( $path, '\\', '/' ), '/' ); } } diff --git a/Yoast/Sniffs/Files/TestDoublesSniff.php b/Yoast/Sniffs/Files/TestDoublesSniff.php index 33359866..1898a271 100644 --- a/Yoast/Sniffs/Files/TestDoublesSniff.php +++ b/Yoast/Sniffs/Files/TestDoublesSniff.php @@ -31,7 +31,7 @@ class TestDoublesSniff implements Sniff { * @since 1.1.0 The property type has changed from string to array. * Use of this property with a string value has been deprecated. * - * @var array + * @var string[] */ public $doubles_path = [ '/tests/doubles', @@ -41,20 +41,20 @@ class TestDoublesSniff implements Sniff { * Validated absolute target paths for test double/mock classes or an empty array * if the intended target directory/directories don't exist. * - * @var array + * @var string[] */ protected $target_paths; /** * Returns an array of tokens this test wants to listen for. * - * @return array + * @return (int|string)[] */ public function register() { return [ - T_CLASS, - T_INTERFACE, - T_TRAIT, + \T_CLASS, + \T_INTERFACE, + \T_TRAIT, ]; } @@ -70,7 +70,7 @@ public function register() { */ public function process( File $phpcsFile, $stackPtr ) { // Stripping potential quotes to ensure `stdin_path` passed by IDEs does not include quotes. - $file = preg_replace( '`^([\'"])(.*)\1$`Ds', '$2', $phpcsFile->getFileName() ); + $file = \preg_replace( '`^([\'"])(.*)\1$`Ds', '$2', $phpcsFile->getFileName() ); if ( $file === 'STDIN' ) { return; @@ -81,7 +81,7 @@ public function process( File $phpcsFile, $stackPtr ) { return; } - if ( stripos( $object_name, 'mock' ) === false && stripos( $object_name, 'double' ) === false ) { + if ( \stripos( $object_name, 'mock' ) === false && \stripos( $object_name, 'double' ) === false ) { return; } @@ -111,23 +111,23 @@ public function process( File $phpcsFile, $stackPtr ) { * * {@internal This should be removed in YoastCS 2.0.0.}} */ - if ( is_string( $this->doubles_path ) ) { + if ( \is_string( $this->doubles_path ) ) { $this->doubles_path = (array) $this->doubles_path; } $tokens = $phpcsFile->getTokens(); $base_path = $this->normalize_directory_separators( $phpcsFile->config->basepath ); - $base_path = rtrim( $base_path, '/' ) . '/'; // Make sure the base_path ends in a single slash. + $base_path = \rtrim( $base_path, '/' ) . '/'; // Make sure the base_path ends in a single slash. - if ( ! isset( $this->target_paths ) || defined( 'PHP_CODESNIFFER_IN_TESTS' ) ) { + if ( ! isset( $this->target_paths ) || \defined( 'PHP_CODESNIFFER_IN_TESTS' ) ) { $this->target_paths = []; foreach ( $this->doubles_path as $doubles_path ) { $target_path = $base_path; - $target_path .= trim( $this->normalize_directory_separators( $doubles_path ), '/' ) . '/'; + $target_path .= \trim( $this->normalize_directory_separators( $doubles_path ), '/' ) . '/'; - if ( file_exists( $target_path ) && is_dir( $target_path ) ) { - $this->target_paths[] = strtolower( $target_path ); + if ( \file_exists( $target_path ) && \is_dir( $target_path ) ) { + $this->target_paths[] = \strtolower( $target_path ); } } } @@ -138,13 +138,13 @@ public function process( File $phpcsFile, $stackPtr ) { $phpcsFile->config->basepath, ]; - if ( count( $this->doubles_path ) === 1 ) { + if ( \count( $this->doubles_path ) === 1 ) { $data[] = 'directory'; - $data[] = implode( '', $this->doubles_path ); + $data[] = \implode( '', $this->doubles_path ); } else { - $all_paths = implode( '", "', $this->doubles_path ); - $all_paths = substr_replace( $all_paths, ' and', strrpos( $all_paths, ',' ), 1 ); + $all_paths = \implode( '", "', $this->doubles_path ); + $all_paths = \substr_replace( $all_paths, ' and', \strrpos( $all_paths, ',' ), 1 ); $data[] = 'directories'; $data[] = $all_paths; @@ -162,7 +162,7 @@ public function process( File $phpcsFile, $stackPtr ) { $is_error = true; foreach ( $this->target_paths as $target_path ) { - if ( stripos( $path_to_file, $target_path ) !== false ) { + if ( \stripos( $path_to_file, $target_path ) !== false ) { $is_error = false; break; } @@ -213,6 +213,6 @@ public function process( File $phpcsFile, $stackPtr ) { * @return string */ private function normalize_directory_separators( $path ) { - return strtr( $path, '\\', '/' ); + return \strtr( $path, '\\', '/' ); } } diff --git a/Yoast/Sniffs/Namespaces/NamespaceDeclarationSniff.php b/Yoast/Sniffs/Namespaces/NamespaceDeclarationSniff.php index 4fcba833..6d11b1ea 100644 --- a/Yoast/Sniffs/Namespaces/NamespaceDeclarationSniff.php +++ b/Yoast/Sniffs/Namespaces/NamespaceDeclarationSniff.php @@ -27,11 +27,11 @@ class NamespaceDeclarationSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. * - * @return array + * @return (int|string)[] */ public function register() { return [ - T_OPEN_TAG, + \T_OPEN_TAG, ]; } @@ -50,7 +50,7 @@ public function process( File $phpcsFile, $stackPtr ) { $statements = []; - while ( ( $stackPtr = $phpcsFile->findNext( T_NAMESPACE, ( $stackPtr + 1 ) ) ) !== false ) { + while ( ( $stackPtr = $phpcsFile->findNext( \T_NAMESPACE, ( $stackPtr + 1 ) ) ) !== false ) { $next_non_empty = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); if ( $next_non_empty === false ) { @@ -58,7 +58,7 @@ public function process( File $phpcsFile, $stackPtr ) { break; } - if ( $tokens[ $next_non_empty ]['code'] === T_NS_SEPARATOR ) { + if ( $tokens[ $next_non_empty ]['code'] === \T_NS_SEPARATOR ) { // Not a namespace declaration, but the use of the namespace keyword as operator. continue; } @@ -77,8 +77,8 @@ public function process( File $phpcsFile, $stackPtr ) { ); } - if ( $tokens[ $next_non_empty ]['code'] === T_SEMICOLON - || $tokens[ $next_non_empty ]['code'] === T_OPEN_CURLY_BRACKET + if ( $tokens[ $next_non_empty ]['code'] === \T_SEMICOLON + || $tokens[ $next_non_empty ]['code'] === \T_OPEN_CURLY_BRACKET ) { // Namespace declaration without namespace name (= global namespace). $phpcsFile->addError( @@ -89,7 +89,7 @@ public function process( File $phpcsFile, $stackPtr ) { } } - $count = count( $statements ); + $count = \count( $statements ); if ( $count > 1 ) { $data = [ $count, diff --git a/Yoast/Sniffs/NamingConventions/NamespaceNameSniff.php b/Yoast/Sniffs/NamingConventions/NamespaceNameSniff.php index ae400ffd..10830eba 100644 --- a/Yoast/Sniffs/NamingConventions/NamespaceNameSniff.php +++ b/Yoast/Sniffs/NamingConventions/NamespaceNameSniff.php @@ -88,18 +88,18 @@ class NamespaceNameSniff implements Sniff { /** * Returns an array of tokens this test wants to listen for. * - * @return array + * @return (int|string)[] */ public function register() { - return [ T_NAMESPACE ]; + return [ \T_NAMESPACE ]; } /** * Filter out all prefixes which don't have namespace separators. * - * @param array $prefixes The unvalidated prefixes. + * @param string[] $prefixes The unvalidated prefixes. * - * @return array + * @return string[] */ protected function filter_prefixes( $prefixes ) { return $this->filter_allow_only_namespace_prefixes( $prefixes ); @@ -112,8 +112,7 @@ protected function filter_prefixes( $prefixes ) { * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * - * @return int StackPtr to the end of the file, this sniff needs to only - * check each file once. + * @return void */ public function process( File $phpcsFile, $stackPtr ) { @@ -125,19 +124,19 @@ public function process( File $phpcsFile, $stackPtr ) { } $next_non_empty = $phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); - if ( $tokens[ $next_non_empty ]['code'] === T_NS_SEPARATOR ) { + if ( $tokens[ $next_non_empty ]['code'] === \T_NS_SEPARATOR ) { // Not a namespace declaration. return; } // Get the complete namespace name. $namespace_name = $tokens[ $next_non_empty ]['content']; - for ( $i = ( $next_non_empty + 1 ); ; $i++ ) { + for ( $i = ( $next_non_empty + 1 ); $i < $phpcsFile->numTokens; $i++ ) { if ( isset( Tokens::$emptyTokens[ $tokens[ $i ]['code'] ] ) ) { continue; } - if ( $tokens[ $i ]['code'] !== T_STRING && $tokens[ $i ]['code'] !== T_NS_SEPARATOR ) { + if ( $tokens[ $i ]['code'] !== \T_STRING && $tokens[ $i ]['code'] !== \T_NS_SEPARATOR ) { // Reached end of the namespace declaration. break; } @@ -145,6 +144,11 @@ public function process( File $phpcsFile, $stackPtr ) { $namespace_name .= $tokens[ $i ]['content']; } + if ( $i === $phpcsFile->numTokens ) { + // Live coding. + return; + } + $this->validate_prefixes(); // Strip off the plugin prefix. @@ -153,9 +157,9 @@ public function process( File $phpcsFile, $stackPtr ) { if ( ! empty( $this->validated_prefixes ) ) { $name = $namespace_name . '\\'; // Validated prefixes always have a \ at the end. foreach ( $this->validated_prefixes as $prefix ) { - if ( strpos( $name . '\\', $prefix ) === 0 ) { - $namespace_name_no_prefix = rtrim( substr( $name, strlen( $prefix ) ), '\\' ); - $found_prefix = rtrim( $prefix, '\\' ); + if ( \strpos( $name . '\\', $prefix ) === 0 ) { + $namespace_name_no_prefix = \rtrim( \substr( $name, \strlen( $prefix ) ), '\\' ); + $found_prefix = \rtrim( $prefix, '\\' ); break; } } @@ -168,15 +172,15 @@ public function process( File $phpcsFile, $stackPtr ) { if ( $namespace_name_no_prefix !== '' ) { $namespace_for_level_check = $namespace_name_no_prefix; // Allow for `Tests\` and `Tests\Doubles\` after the prefix. - if ( strpos( $namespace_for_level_check, 'Tests\\' ) === 0 ) { - $namespace_for_level_check = substr( $namespace_for_level_check, 6 ); - if ( strpos( $namespace_for_level_check, 'Doubles\\' ) === 0 ) { - $namespace_for_level_check = substr( $namespace_for_level_check, 8 ); + if ( \strpos( $namespace_for_level_check, 'Tests\\' ) === 0 ) { + $namespace_for_level_check = \substr( $namespace_for_level_check, 6 ); + if ( \strpos( $namespace_for_level_check, 'Doubles\\' ) === 0 ) { + $namespace_for_level_check = \substr( $namespace_for_level_check, 8 ); } } - $parts = explode( '\\', $namespace_for_level_check ); - $part_count = count( $parts ); + $parts = \explode( '\\', $namespace_for_level_check ); + $part_count = \count( $parts ); if ( $part_count > $this->max_levels ) { $error = 'A namespace name is not allowed to be more than %d levels deep (excluding the prefix). Level depth found: %d in %s'; @@ -212,13 +216,13 @@ public function process( File $phpcsFile, $stackPtr ) { $base_path = $this->normalize_directory_separators( $phpcsFile->config->basepath ); // Stripping potential quotes to ensure `stdin_path` passed by IDEs does not include quotes. - $file = preg_replace( '`^([\'"])(.*)\1$`Ds', '$2', $phpcsFile->getFileName() ); + $file = \preg_replace( '`^([\'"])(.*)\1$`Ds', '$2', $phpcsFile->getFileName() ); if ( $file === 'STDIN' ) { return; } - $directory = $this->normalize_directory_separators( dirname( $file ) ); + $directory = $this->normalize_directory_separators( \dirname( $file ) ); $relative_directory = Common::stripBasepath( $directory, $base_path ); if ( $relative_directory === '.' ) { $relative_directory = ''; @@ -241,43 +245,40 @@ public function process( File $phpcsFile, $stackPtr ) { if ( empty( $this->validated_src_directory ) === false ) { foreach ( $this->validated_src_directory as $subdirectory ) { - if ( strpos( $relative_directory, $subdirectory ) !== 0 ) { + if ( \strpos( $relative_directory, $subdirectory ) !== 0 ) { continue; } - $relative_directory = substr( $relative_directory, strlen( $subdirectory ) ); + $relative_directory = \substr( $relative_directory, \strlen( $subdirectory ) ); break; } } // Now any potential src directory has been stripped, remove the slashes again. - $relative_directory = trim( $relative_directory, '/' ); + $relative_directory = \trim( $relative_directory, '/' ); - $namespace_name_for_translation = str_replace( + $namespace_name_for_translation = \str_replace( [ '_', '\\' ], // Find. [ '-', '/' ], // Replace with. $namespace_name_no_prefix ); - if ( strcasecmp( $relative_directory, $namespace_name_for_translation ) === 0 ) { + if ( \strcasecmp( $relative_directory, $namespace_name_for_translation ) === 0 ) { return; } - $expected = ''; + $expected = '[Plugin\Prefix]'; if ( $found_prefix !== '' ) { $expected = $found_prefix; } - else { - $expected = '[Plugin\Prefix]'; - } if ( $relative_directory !== '' ) { - $levels = explode( '/', $relative_directory ); - $levels = array_filter( $levels ); // Remove empties. + $levels = \explode( '/', $relative_directory ); + $levels = \array_filter( $levels ); // Remove empties. foreach ( $levels as $level ) { - $words = explode( '-', $level ); - $words = array_map( 'ucfirst', $words ); - $expected .= '\\' . implode( '_', $words ); + $words = \explode( '-', $level ); + $words = \array_map( 'ucfirst', $words ); + $expected .= '\\' . \implode( '_', $words ); } } @@ -306,7 +307,7 @@ protected function validate_src_directory() { $this->previous_src_directory = $this->src_directory; $src_directory = (array) $this->src_directory; - $src_directory = array_filter( array_map( 'trim', $src_directory ) ); + $src_directory = \array_filter( \array_map( 'trim', $src_directory ) ); if ( empty( $src_directory ) ) { $this->validated_src_directory = []; @@ -315,7 +316,7 @@ protected function validate_src_directory() { $validated = []; foreach ( $src_directory as $directory ) { - if ( strpos( $directory, '..' ) !== false ) { + if ( \strpos( $directory, '..' ) !== false ) { // Do not allow walking up the directory hierarchy. continue; } @@ -327,8 +328,8 @@ protected function validate_src_directory() { continue; } - if ( strpos( $directory, './' ) === 0 ) { - $directory = substr( $directory, 2 ); + if ( \strpos( $directory, './' ) === 0 ) { + $directory = \substr( $directory, 2 ); } if ( $directory === '' ) { @@ -339,7 +340,7 @@ protected function validate_src_directory() { } // Use reverse natural sorting to get the longest directory first. - rsort( $validated, ( SORT_NATURAL | SORT_FLAG_CASE ) ); + \rsort( $validated, ( \SORT_NATURAL | \SORT_FLAG_CASE ) ); // Set the validated prefixes cache. $this->validated_src_directory = $validated; @@ -353,6 +354,6 @@ protected function validate_src_directory() { * @return string */ private function normalize_directory_separators( $path ) { - return trim( strtr( $path, '\\', '/' ), '/' ); + return \trim( \strtr( $path, '\\', '/' ), '/' ); } } diff --git a/Yoast/Sniffs/NamingConventions/ObjectNameDepthSniff.php b/Yoast/Sniffs/NamingConventions/ObjectNameDepthSniff.php index c20ffa5e..6d3766e7 100644 --- a/Yoast/Sniffs/NamingConventions/ObjectNameDepthSniff.php +++ b/Yoast/Sniffs/NamingConventions/ObjectNameDepthSniff.php @@ -44,7 +44,7 @@ class ObjectNameDepthSniff extends WPCS_Sniff { * The key is the suffix. The value indicates whether this suffix is * only allowed when the class extends a known test class. * - * @var array + * @var bool[] */ private $test_suffixes = [ 'Test' => true, @@ -55,13 +55,13 @@ class ObjectNameDepthSniff extends WPCS_Sniff { /** * Returns an array of tokens this test wants to listen for. * - * @return array + * @return (int|string)[] */ public function register() { return [ - T_CLASS, - T_INTERFACE, - T_TRAIT, + \T_CLASS, + \T_INTERFACE, + \T_TRAIT, ]; } @@ -85,20 +85,20 @@ public function process_token( $stackPtr ) { return; } - $parts = explode( '_', $object_name ); - $part_count = count( $parts ); + $parts = \explode( '_', $object_name ); + $part_count = \count( $parts ); /* * Allow the class name to be one part longer for confirmed test/mock/double classes. */ - $last = array_pop( $parts ); + $last = \array_pop( $parts ); if ( isset( $this->test_suffixes[ $last ] ) ) { if ( $this->test_suffixes[ $last ] === true && $this->is_test_class( $stackPtr ) ) { --$part_count; } else { $extends = $this->phpcsFile->findExtendedClassName( $stackPtr ); - if ( is_string( $extends ) ) { + if ( \is_string( $extends ) ) { --$part_count; } } @@ -110,13 +110,13 @@ public function process_token( $stackPtr ) { // Check if the class is deprecated. $find = [ - T_ABSTRACT => T_ABSTRACT, - T_FINAL => T_FINAL, - T_WHITESPACE => T_WHITESPACE, + \T_ABSTRACT => \T_ABSTRACT, + \T_FINAL => \T_FINAL, + \T_WHITESPACE => \T_WHITESPACE, ]; $comment_end = $this->phpcsFile->findPrevious( $find, ( $stackPtr - 1 ), null, true ); - if ( $this->tokens[ $comment_end ]['code'] === T_DOC_COMMENT_CLOSE_TAG ) { + if ( $this->tokens[ $comment_end ]['code'] === \T_DOC_COMMENT_CLOSE_TAG ) { // Only check if the class has a docblock. $comment_start = $this->tokens[ $comment_end ]['comment_opener']; foreach ( $this->tokens[ $comment_start ]['comment_tags'] as $tag ) { diff --git a/Yoast/Sniffs/NamingConventions/ValidHookNameSniff.php b/Yoast/Sniffs/NamingConventions/ValidHookNameSniff.php index 13c5a7a4..54ded3cc 100644 --- a/Yoast/Sniffs/NamingConventions/ValidHookNameSniff.php +++ b/Yoast/Sniffs/NamingConventions/ValidHookNameSniff.php @@ -115,9 +115,9 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p $found_prefix = ''; if ( isset( Tokens::$stringTokens[ $this->tokens[ $first_non_empty ]['code'] ] ) ) { - $content = trim( $this->strip_quotes( $this->tokens[ $first_non_empty ]['content'] ) ); + $content = \trim( $this->strip_quotes( $this->tokens[ $first_non_empty ]['content'] ) ); foreach ( $this->validated_prefixes as $prefix ) { - if ( strpos( $content, $prefix ) === 0 ) { + if ( \strpos( $content, $prefix ) === 0 ) { $found_prefix = $prefix; break; } @@ -177,7 +177,7 @@ protected function transform( $string, $regex, $transform_type = 'full' ) { && $string === $this->first_string ) { if ( $this->found_prefix !== '' ) { - $string = substr( $string, strlen( $this->found_prefix ) ); + $string = \substr( $string, \strlen( $this->found_prefix ) ); } return $this->found_prefix . parent::transform( $string, $regex, $transform_type ); @@ -188,8 +188,8 @@ protected function transform( $string, $regex, $transform_type = 'full' ) { $this->first_string = $string; foreach ( $this->validated_prefixes as $prefix ) { - if ( strpos( $string, $prefix ) === 0 ) { - $string = substr( $string, strlen( $prefix ) ); + if ( \strpos( $string, $prefix ) === 0 ) { + $string = \substr( $string, \strlen( $prefix ) ); $this->found_prefix = $prefix; /* @@ -229,14 +229,14 @@ public function verify_yoast_hook_name( $stackPtr, $parameters ) { /* * Check that the namespace-like prefix is used for hooks. */ - if ( strpos( $this->found_prefix, '\\' ) === false ) { + if ( \strpos( $this->found_prefix, '\\' ) === false ) { /* * Find which namespace-based prefix should have been used. * Loop till the end as the shortest prefix will be last. */ $namespace_prefix = ''; foreach ( $this->validated_prefixes as $prefix ) { - if ( strpos( $prefix, '\\' ) !== false ) { + if ( \strpos( $prefix, '\\' ) !== false ) { $namespace_prefix = $prefix; } } @@ -284,10 +284,10 @@ public function verify_yoast_hook_name( $stackPtr, $parameters ) { */ $hook_ptr = $first_non_empty; // If no other tokens were found, the first non empty will be the hook name. $hook_name = $this->strip_quotes( $this->tokens[ $hook_ptr ]['content'] ); - $hook_name = substr( $hook_name, strlen( $this->found_prefix ) ); + $hook_name = \substr( $hook_name, \strlen( $this->found_prefix ) ); - $parts = explode( '_', $hook_name ); - $part_count = count( $parts ); + $parts = \explode( '_', $hook_name ); + $part_count = \count( $parts ); if ( $part_count <= $this->recommended_max_words && $part_count <= $this->max_words ) { return; diff --git a/Yoast/Sniffs/WhiteSpace/FunctionSpacingSniff.php b/Yoast/Sniffs/WhiteSpace/FunctionSpacingSniff.php index 98906489..0f7b1b89 100644 --- a/Yoast/Sniffs/WhiteSpace/FunctionSpacingSniff.php +++ b/Yoast/Sniffs/WhiteSpace/FunctionSpacingSniff.php @@ -25,7 +25,7 @@ class FunctionSpacingSniff extends Squiz_FunctionSpacingSniff { * * {@internal Upstream sniff defaults to 2.}} * - * @var integer + * @var int */ public $spacing = 1; @@ -34,7 +34,7 @@ class FunctionSpacingSniff extends Squiz_FunctionSpacingSniff { * * {@internal Upstream sniff defaults to 2.}} * - * @var integer + * @var int */ public $spacingBeforeFirst = 1; @@ -43,7 +43,7 @@ class FunctionSpacingSniff extends Squiz_FunctionSpacingSniff { * * {@internal Upstream sniff defaults to 2.}} * - * @var integer + * @var int */ public $spacingAfterLast = 0; @@ -54,11 +54,9 @@ class FunctionSpacingSniff extends Squiz_FunctionSpacingSniff { * @param int $stackPtr The position of the current token * in the stack passed in $tokens. * - * @return void + * @return void|int Optionally returns stack pointer to skip to. */ public function process( File $phpcsFile, $stackPtr ) { - $tokens = $phpcsFile->getTokens(); - // Check that the function is nested in an OO structure (class, trait, interface). if ( $phpcsFile->hasCondition( $stackPtr, Tokens::$ooScopeTokens ) === false ) { return; diff --git a/Yoast/Sniffs/Yoast/AlternativeFunctionsSniff.php b/Yoast/Sniffs/Yoast/AlternativeFunctionsSniff.php index 639aafab..ae9c198b 100644 --- a/Yoast/Sniffs/Yoast/AlternativeFunctionsSniff.php +++ b/Yoast/Sniffs/Yoast/AlternativeFunctionsSniff.php @@ -40,8 +40,7 @@ public function getGroups() { * @param string $group_name The name of the group which was matched. * @param string $matched_content The token content (function name) which was matched. * - * @return int|void Integer stack pointer to skip forward or void to continue - * normal file processing. + * @return void */ public function process_matched_token( $stackPtr, $group_name, $matched_content ) { diff --git a/Yoast/Tests/Files/FileNameUnitTest.php b/Yoast/Tests/Files/FileNameUnitTest.php index b76ad069..7c205117 100644 --- a/Yoast/Tests/Files/FileNameUnitTest.php +++ b/Yoast/Tests/Files/FileNameUnitTest.php @@ -18,7 +18,7 @@ class FileNameUnitTest extends AbstractSniffUnitTest { /** * Error files with the expected nr of errors. * - * @var array + * @var int[] */ private $expected_results = [ @@ -89,7 +89,7 @@ public function setCliValues( $testFile, $config ) { return; } - $config->basepath = __DIR__ . DIRECTORY_SEPARATOR . 'FileNameUnitTests'; + $config->basepath = __DIR__ . \DIRECTORY_SEPARATOR . 'FileNameUnitTests'; } /** @@ -100,8 +100,8 @@ public function setCliValues( $testFile, $config ) { * @return string[] */ protected function getTestFiles( $testFileBase ) { - $sep = DIRECTORY_SEPARATOR; - $test_files = glob( dirname( $testFileBase ) . $sep . 'FileNameUnitTests{' . $sep . ',' . $sep . '*' . $sep . '}*.inc', GLOB_BRACE ); + $sep = \DIRECTORY_SEPARATOR; + $test_files = \glob( \dirname( $testFileBase ) . $sep . 'FileNameUnitTests{' . $sep . ',' . $sep . '*' . $sep . '}*.inc', \GLOB_BRACE ); if ( ! empty( $test_files ) ) { return $test_files; diff --git a/Yoast/Tests/Files/TestDoublesUnitTest.php b/Yoast/Tests/Files/TestDoublesUnitTest.php index 25fdd3ff..6feffbf6 100644 --- a/Yoast/Tests/Files/TestDoublesUnitTest.php +++ b/Yoast/Tests/Files/TestDoublesUnitTest.php @@ -28,7 +28,7 @@ public function setCliValues( $testFile, $config ) { return; } - $config->basepath = __DIR__ . DIRECTORY_SEPARATOR . 'TestDoublesUnitTests'; + $config->basepath = __DIR__ . \DIRECTORY_SEPARATOR . 'TestDoublesUnitTests'; } /** @@ -39,8 +39,8 @@ public function setCliValues( $testFile, $config ) { * @return string[] */ protected function getTestFiles( $testFileBase ) { - $sep = DIRECTORY_SEPARATOR; - $test_files = glob( dirname( $testFileBase ) . $sep . 'TestDoublesUnitTests{' . $sep . ',' . $sep . '*' . $sep . '}*.inc', GLOB_BRACE ); + $sep = \DIRECTORY_SEPARATOR; + $test_files = \glob( \dirname( $testFileBase ) . $sep . 'TestDoublesUnitTests{' . $sep . ',' . $sep . '*' . $sep . '}*.inc', \GLOB_BRACE ); if ( ! empty( $test_files ) ) { return $test_files; diff --git a/Yoast/Tests/NamingConventions/NamespaceNameUnitTest.php b/Yoast/Tests/NamingConventions/NamespaceNameUnitTest.php index 20a1e79a..e4e7220f 100644 --- a/Yoast/Tests/NamingConventions/NamespaceNameUnitTest.php +++ b/Yoast/Tests/NamingConventions/NamespaceNameUnitTest.php @@ -25,11 +25,11 @@ class NamespaceNameUnitTest extends AbstractSniffUnitTest { * @return void */ public function setCliValues( $testFile, $config ) { - if ( strpos( $testFile, 'no-basepath' ) === 0 ) { + if ( \strpos( $testFile, 'no-basepath' ) === 0 ) { return; } - $config->basepath = __DIR__ . DIRECTORY_SEPARATOR . 'NamespaceNameUnitTests'; + $config->basepath = __DIR__ . \DIRECTORY_SEPARATOR . 'NamespaceNameUnitTests'; } /** @@ -40,8 +40,8 @@ public function setCliValues( $testFile, $config ) { * @return string[] */ protected function getTestFiles( $testFileBase ) { - $sep = DIRECTORY_SEPARATOR; - $test_files = glob( dirname( $testFileBase ) . $sep . 'NamespaceNameUnitTests{' . $sep . ',' . $sep . '*' . $sep . '}*.inc', GLOB_BRACE ); + $sep = \DIRECTORY_SEPARATOR; + $test_files = \glob( \dirname( $testFileBase ) . $sep . 'NamespaceNameUnitTests{' . $sep . ',' . $sep . '*' . $sep . '}*.inc', \GLOB_BRACE ); if ( ! empty( $test_files ) ) { return $test_files; diff --git a/Yoast/Tests/NamingConventions/NamespaceNameUnitTests/live-coding.inc b/Yoast/Tests/NamingConventions/NamespaceNameUnitTests/live-coding.inc new file mode 100644 index 00000000..24ebec1e --- /dev/null +++ b/Yoast/Tests/NamingConventions/NamespaceNameUnitTests/live-coding.inc @@ -0,0 +1,7 @@ +previous_prefixes = $this->prefixes; $prefixes = (array) $this->prefixes; - $prefixes = array_filter( array_map( 'trim', $prefixes ) ); + $prefixes = \array_filter( \array_map( 'trim', $prefixes ) ); if ( empty( $prefixes ) ) { $this->validated_prefixes = []; @@ -76,19 +76,19 @@ protected function validate_prefixes() { $validated = []; foreach ( $prefixes as $prefix ) { - if ( strpos( $prefix, '\\' ) !== false ) { - $prefix = trim( $prefix, '\\' ); + if ( \strpos( $prefix, '\\' ) !== false ) { + $prefix = \trim( $prefix, '\\' ); $validated[] = $prefix . '\\'; } else { // Old-style prefix. - $prefix = trim( $prefix, '_' ); + $prefix = \trim( $prefix, '_' ); $validated[] = $prefix . '_'; } } // Use reverse natural sorting to get the longest prefix first. - rsort( $validated, ( SORT_NATURAL | SORT_FLAG_CASE ) ); + \rsort( $validated, ( \SORT_NATURAL | \SORT_FLAG_CASE ) ); // Set the validated prefixes cache. $this->validated_prefixes = $validated; @@ -97,9 +97,9 @@ protected function validate_prefixes() { /** * Overloadable method to do custom prefix filtering prior to validation. * - * @param array $prefixes The unvalidated prefixes. + * @param string[] $prefixes The unvalidated prefixes. * - * @return array + * @return string[] */ protected function filter_prefixes( $prefixes ) { return $prefixes; @@ -108,14 +108,14 @@ protected function filter_prefixes( $prefixes ) { /** * Filter out all prefixes which don't contain a namespace separator. * - * @param array $prefixes The unvalidated prefixes. + * @param string[] $prefixes The unvalidated prefixes. * - * @return array + * @return string[] */ protected function filter_allow_only_namespace_prefixes( $prefixes ) { $filtered = []; foreach ( $prefixes as $prefix ) { - if ( strpos( $prefix, '\\' ) === false ) { + if ( \strpos( $prefix, '\\' ) === false ) { continue; } @@ -128,14 +128,14 @@ protected function filter_allow_only_namespace_prefixes( $prefixes ) { /** * Filter out all prefixes which only contain lowercase characters. * - * @param array $prefixes The unvalidated prefixes. + * @param string[] $prefixes The unvalidated prefixes. * - * @return array + * @return string[] */ protected function filter_exclude_lowercase_prefixes( $prefixes ) { $filtered = []; foreach ( $prefixes as $prefix ) { - if ( strtolower( $prefix ) === $prefix ) { + if ( \strtolower( $prefix ) === $prefix ) { continue; } diff --git a/composer.json b/composer.json index 91860281..701362c6 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "squizlabs/php_codesniffer": "^3.5.0", "wp-coding-standards/wpcs": "^2.2.0", "phpcompatibility/phpcompatibility-wp": "^2.1.0", - "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0" + "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || ^0.6" }, "require-dev": { "phpcompatibility/php-compatibility": "^9.2.0",