diff --git a/Yoast/Docs/Commenting/CoversTagStandard.xml b/Yoast/Docs/Commenting/CoversTagStandard.xml index 47ad4dff..b898bf4b 100644 --- a/Yoast/Docs/Commenting/CoversTagStandard.xml +++ b/Yoast/Docs/Commenting/CoversTagStandard.xml @@ -107,6 +107,41 @@ class Some_Test extends TestCase { * @covers ::globalFunction */ function test_something() {} +} + ]]> + + + + tags is deprecated since PHPUnit 9.0 and support has been removed in PHPUnit 10.0. + These type of annotations should not be used. + ]]> + + + + ::globalFunction + * @covers \ClassName::methodName + */ + function test_something() {} +} + ]]> + + + \ClassName:: + * @covers \ClassName:: + * @covers \ClassName + */ + function test_something() {} } ]]> diff --git a/Yoast/Sniffs/Commenting/CoversTagSniff.php b/Yoast/Sniffs/Commenting/CoversTagSniff.php index 246c6778..5f6e8bc3 100644 --- a/Yoast/Sniffs/Commenting/CoversTagSniff.php +++ b/Yoast/Sniffs/Commenting/CoversTagSniff.php @@ -13,7 +13,8 @@ * - each @covers tag has an annotation; * - there are no duplicate @covers tags; * - there are no duplicate @coversNothing tags; - * - a method does not have both a @covers as well as a @coversNothing tag. + * - a method does not have both a @covers as well as a @coversNothing tag; + * - deprecated @covers tag formats are flagged. (since 3.0.0) * * @since 1.3.0 */ @@ -26,6 +27,26 @@ final class CoversTagSniff implements Sniff { */ private const VALID_CONTENT_REGEX = '(?:\\\\?(?:(?[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\\\\)*(?P>OOName)(?:|::<[!]?(?:public|protected|private)>|::(?(?!public$|protected$|private$)(?P>OOName)))?|::(?P>functionName)|::<[!]?(?:public|protected|private)>|\\\\?(?:(?P>OOName)\\\\)+(?P>functionName))'; + /** + * Regex to check for deprecated `@covers ClassName` tag format. + * + * @link https://github.com/sebastianbergmann/phpunit/issues/3630 PHPUnit 9.0 deprecation. + * @link https://github.com/sebastianbergmann/phpunit/issues/3631 PHPUnit 10.0 removal. + * + * @var string + */ + private const DEPRECATED_FORMAT_EXTENDED = '`^(?:\\\\)?(?:(?[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\\\\)*(?P>OOName)$`'; + + /** + * Regex to check for deprecated `@covers *::<[!]public|protected|private>` tag format. + * + * @link https://github.com/sebastianbergmann/phpunit/issues/3630 PHPUnit 9.0 deprecation. + * @link https://github.com/sebastianbergmann/phpunit/issues/3631 PHPUnit 10.0 removal. + * + * @var string + */ + private const DEPRECATED_FORMAT_VISIBILITY = '`::<[!]?(?:public|protected|private)>$`'; + /** * Base error message. * @@ -95,6 +116,16 @@ public function process( File $phpcsFile, $stackPtr ) { $annotation = $tokens[ $next ]['content']; $coversTags[ "$tag-$next" ] = $annotation; + // Check for deprecated/removed @covers formats. + if ( \preg_match( self::DEPRECATED_FORMAT_EXTENDED, $annotation ) === 1 + || \preg_match( self::DEPRECATED_FORMAT_VISIBILITY, $annotation ) === 1 + ) { + $warning = 'Use of the "ClassName<*>" type values for @covers annotations has been deprecated in PHPUnit 9.0'; + $warning .= ' and support has been removed in PHPUnit 10.0. Found: %s'; + $data = [ $annotation ]; + $phpcsFile->addWarning( $warning, $next, 'RemovedFormat', $data ); + } + if ( \preg_match( '`^' . self::VALID_CONTENT_REGEX . '$`', $annotation ) === 1 ) { continue; } diff --git a/Yoast/Tests/Commenting/CoversTagUnitTest.inc b/Yoast/Tests/Commenting/CoversTagUnitTest.inc index 63fa3c41..4ff3c918 100644 --- a/Yoast/Tests/Commenting/CoversTagUnitTest.inc +++ b/Yoast/Tests/Commenting/CoversTagUnitTest.inc @@ -1,30 +1,14 @@ - * @covers Class_Name:: - * @covers Class_Name:: - * @covers Class_Name:: - * @covers Class_Name:: - * @covers Class_Name:: - * @covers Class_Name:: * @covers Name\Space\Class_Name * @covers \Name\Space\Class_Name - * @covers Name\Space\Class_Name:: - * @covers Name\Space\Class_Name:: - * @covers Name\Space\Class_Name:: - * @covers Name\Space\Class_Name:: - * @covers Name\Space\Class_Name:: - * @covers Name\Space\Class_Name:: * @covers Class_Name::method_name * @covers \Class_name::method_name * @covers Name\Space\Class_Name::method_name @@ -42,8 +26,24 @@ class ClassNameTest { public function testCoversTag() {} /** - * Docblock. - * + * @covers Class_Name + * @covers Class_Name:: + * @covers Class_Name:: + * @covers Class_Name:: + * @covers Class_Name:: + * @covers Class_Name:: + * @covers Class_Name:: + * @covers \Name\Space\Class_Name + * @covers \Name\Space\Class_Name:: + * @covers Name\Space\Class_Name:: + * @covers Name\Space\Class_Name:: + * @covers \Name\Space\Class_Name:: + * @covers Name\Space\Class_Name:: + * @covers \Name\Space\Class_Name:: + */ + public function testDeprecatedRemovedTagTypes() {} + + /** * @covers ::global_functionA && ::other_functionA * @covers ::global_functionB & ::other_functionB * @covers ::global_functionC|::other_functionC diff --git a/Yoast/Tests/Commenting/CoversTagUnitTest.inc.fixed b/Yoast/Tests/Commenting/CoversTagUnitTest.inc.fixed index bef7bee0..cc03668d 100644 --- a/Yoast/Tests/Commenting/CoversTagUnitTest.inc.fixed +++ b/Yoast/Tests/Commenting/CoversTagUnitTest.inc.fixed @@ -1,30 +1,14 @@ - * @covers Class_Name:: - * @covers Class_Name:: - * @covers Class_Name:: - * @covers Class_Name:: - * @covers Class_Name:: - * @covers Class_Name:: * @covers Name\Space\Class_Name * @covers \Name\Space\Class_Name - * @covers Name\Space\Class_Name:: - * @covers Name\Space\Class_Name:: - * @covers Name\Space\Class_Name:: - * @covers Name\Space\Class_Name:: - * @covers Name\Space\Class_Name:: - * @covers Name\Space\Class_Name:: * @covers Class_Name::method_name * @covers \Class_name::method_name * @covers Name\Space\Class_Name::method_name @@ -42,8 +26,24 @@ class ClassNameTest { public function testCoversTag() {} /** - * Docblock. - * + * @covers Class_Name + * @covers Class_Name:: + * @covers Class_Name:: + * @covers Class_Name:: + * @covers Class_Name:: + * @covers Class_Name:: + * @covers Class_Name:: + * @covers \Name\Space\Class_Name + * @covers \Name\Space\Class_Name:: + * @covers Name\Space\Class_Name:: + * @covers Name\Space\Class_Name:: + * @covers \Name\Space\Class_Name:: + * @covers Name\Space\Class_Name:: + * @covers \Name\Space\Class_Name:: + */ + public function testDeprecatedRemovedTagTypes() {} + + /** * @covers ::global_functionA * @covers ::other_functionA * @covers ::global_functionB diff --git a/Yoast/Tests/Commenting/CoversTagUnitTest.php b/Yoast/Tests/Commenting/CoversTagUnitTest.php index 415cea7d..f85d2463 100644 --- a/Yoast/Tests/Commenting/CoversTagUnitTest.php +++ b/Yoast/Tests/Commenting/CoversTagUnitTest.php @@ -20,11 +20,11 @@ final class CoversTagUnitTest extends AbstractSniffUnitTest { */ public function getErrorList(): array { return [ - 36 => 1, - 37 => 1, - 38 => 1, - 39 => 1, - 40 => 1, + 20 => 1, + 21 => 1, + 22 => 1, + 23 => 1, + 24 => 1, 47 => 1, 48 => 1, 49 => 1, @@ -58,6 +58,27 @@ public function getErrorList(): array { * @return array Key is the line number, value is the number of expected warnings. */ public function getWarningList(): array { - return []; + return [ + 29 => 1, + 30 => 1, + 31 => 1, + 32 => 1, + 33 => 1, + 34 => 1, + 35 => 1, + 36 => 1, + 37 => 1, + 38 => 1, + 39 => 1, + 40 => 1, + 41 => 1, + 42 => 1, + 190 => 1, + 191 => 1, + 192 => 1, + 193 => 1, + 194 => 1, + 195 => 1, + ]; } }