diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index b9378458..e271b194 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -100,4 +100,5 @@ jobs: sed -i 's/Drupal.Methods.MethodDeclaration/PSR2.Methods.MethodDeclaration/g' phpcs.xml.dist sed -i '//d' phpcs.xml.dist sed -i 's/Drupal.Classes.InterfaceName/Generic.NamingConventions.InterfaceNameSuffix/g' phpcs.xml.dist + sed -i '//d' phpcs.xml.dist ../../vendor/bin/phpcs -p -s --parallel=$(nproc) --exclude=Drupal.ControlStructures.ControlSignature --ignore=lib/Drupal/Core/Command/GenerateTheme.php,modules/mysql/tests/src/Kernel/mysql/Console/DbDumpCommandTest.php,modules/big_pipe/tests/modules/big_pipe_test/src/BigPipePlaceholderTestCases.php,tests/fixtures/plugins/CustomPlugin.php,modules/package_manager/tests/modules/package_manager_test_api/src/ApiController.php,modules/views/tests/src/Kernel/Plugin/StyleGridTest.php diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/DataTypeNamespaceSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/DataTypeNamespaceSniff.php deleted file mode 100644 index b38c131e..00000000 --- a/coder_sniffer/Drupal/Sniffs/Commenting/DataTypeNamespaceSniff.php +++ /dev/null @@ -1,103 +0,0 @@ - - */ - public function register() - { - return [T_USE]; - } - - - /** - * Processes this test, when one of its tokens is encountered. - * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the current token in - * the stack passed in $tokens. - * - * @return void - */ - public function process(File $phpcsFile, $stackPtr) - { - $tokens = $phpcsFile->getTokens(); - - // Only check use statements in the global scope. - if (empty($tokens[$stackPtr]['conditions']) === false) { - return; - } - - // Seek to the end of the statement and get the string before the semi colon. - $semiColon = $phpcsFile->findEndOfStatement($stackPtr); - if ($tokens[$semiColon]['code'] !== T_SEMICOLON) { - return; - } - - $classPtr = $phpcsFile->findPrevious( - Tokens::EMPTY_TOKENS, - ($semiColon - 1), - null, - true - ); - - if (in_array($tokens[$classPtr]['code'], Tokens::NAME_TOKENS) === false) { - return; - } - - // Replace @var data types in doc comments with the fully qualified class - // name. - $fullNamespace = $tokens[$classPtr]['content']; - $className = substr($fullNamespace, (strrpos($fullNamespace, '\\') + 1)); - - $tag = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($stackPtr + 1)); - - while ($tag !== false) { - if (($tokens[$tag]['content'] === '@var' - || $tokens[$tag]['content'] === '@return' - || $tokens[$tag]['content'] === '@param' - || $tokens[$tag]['content'] === '@throws') - && isset($tokens[($tag + 1)]) === true - && $tokens[($tag + 1)]['code'] === T_DOC_COMMENT_WHITESPACE - && isset($tokens[($tag + 2)]) === true - && $tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING - && strpos($tokens[($tag + 2)]['content'], $className) === 0 - ) { - $error = 'Data types in %s tags need to be fully namespaced'; - $data = [$tokens[$tag]['content']]; - $fix = $phpcsFile->addFixableError($error, ($tag + 2), 'DataTypeNamespace', $data); - if ($fix === true) { - $replacement = '\\' . $fullNamespace . substr($tokens[($tag + 2)]['content'], strlen($className)); - $phpcsFile->fixer->replaceToken(($tag + 2), $replacement); - } - } - - $tag = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($tag + 1)); - }//end while - } -} diff --git a/coder_sniffer/Drupal/ruleset.xml b/coder_sniffer/Drupal/ruleset.xml index d109843d..21f1634e 100644 --- a/coder_sniffer/Drupal/ruleset.xml +++ b/coder_sniffer/Drupal/ruleset.xml @@ -136,6 +136,7 @@ + diff --git a/tests/Drupal/Commenting/DataTypeNamespaceUnitTest.php b/tests/Drupal/Commenting/DataTypeNamespaceUnitTest.php deleted file mode 100644 index 9e5d6d0b..00000000 --- a/tests/Drupal/Commenting/DataTypeNamespaceUnitTest.php +++ /dev/null @@ -1,46 +0,0 @@ - - */ - protected function getErrorList(string $testFile): array - { - return [ - 15 => 1, - 18 => 1, - 21 => 1, - 31 => 1, - ]; - } - - - /** - * Returns the lines where warnings should occur. - * - * The key of the array should represent the line number and the value - * should represent the number of warnings that should occur on that line. - * - * @param string $testFile The name of the file being tested. - * - * @return array - */ - protected function getWarningList(string $testFile): array - { - return []; - } -} diff --git a/tests/Drupal/Commenting/DocCommentUnitTest.inc.fixed b/tests/Drupal/Commenting/DocCommentUnitTest.inc.fixed index 2ca9fa0b..2b917132 100644 --- a/tests/Drupal/Commenting/DocCommentUnitTest.inc.fixed +++ b/tests/Drupal/Commenting/DocCommentUnitTest.inc.fixed @@ -14,7 +14,7 @@ * @return bool * Returns FALSE. * - * @throws Exception + * @throws \Exception * Thrown when $param is TRUE. * * @ingroup sniffer @@ -97,7 +97,7 @@ function test12() { * | Unseen University | Mustrum Ridcully | Alberto Malich the Wise | 1281 AM | Nvnc Id Vides, Nvnc Ne Vides | * phpcs:enable * - * @param TableNode $organisation_table + * @param \TableNode $organisation_table * The organisation data. * * @Given (the following )organisations: diff --git a/tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed b/tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed index 817400ca..0dcd0883 100644 --- a/tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed +++ b/tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed @@ -257,11 +257,11 @@ function test21($arg1, $arg2, $arg3) { * * @param array|bool|float|int|mixed|object|string|resource|callable $arg1 * All of the above types are valid. - * @param array|bool|int|string|null $arg2 + * @param array|bool|int|\str|null $arg2 * All of the above types are invalid. * @param array|bool|int|string $arg3 * All of the above types are invalid. - * @param mixed $arg4 + * @param \type $arg4 * All of the above types are invalid. * @param false|true|int $arg5 * All of the above types are invalid. @@ -536,7 +536,7 @@ function test37(array $matches, array $sub_key, $to) { /** * Yield from should be a recognised return statement. * - * @return Generator + * @return \Generator * Generator value. */ function test38($a, $b) { @@ -605,7 +605,7 @@ class Test41 { * NULL. * @param float $param8 * Float. - * @param double $param9 + * @param \double $param9 * Double. * @param scalar $param10 * Scalar. @@ -685,15 +685,15 @@ function test_return_void2(): void { /** * PHPStan: General arrays. * - * @param Type[] $param1 + * @param \Type[] $param1 * Parameter. - * @param array $param2 + * @param array<\Type> $param2 * Parameter. - * @param array $param3 + * @param array $param3 * Parameter. - * @param non-empty-array $param4 + * @param non-empty-array<\Type> $param4 * Parameter. - * @param non-empty-array $param5 + * @param non-empty-array $param5 * Parameter. * * @see https://phpstan.org/writing-php-code/phpdoc-types#general-arrays @@ -702,7 +702,7 @@ function test_arrays(array $param1, array $param2, array $param3, array $param4, } /** - * @return Type[] + * @return \Type[] * Square brackets. */ function test_return_type_array(): array { @@ -710,7 +710,7 @@ function test_return_type_array(): array { } /** - * @return array + * @return array<\Type> * Arrow brackets. */ function test_return_arrow_array(): array { @@ -718,7 +718,7 @@ function test_return_arrow_array(): array { } /** - * @return array + * @return array * Keyed array. */ function test_return_keyed_array(): array { @@ -726,7 +726,7 @@ function test_return_keyed_array(): array { } /** - * @return non-empty-array + * @return non-empty-array<\Type> * Non empty array with type. */ function test_return_non_empty_array(): array { @@ -734,7 +734,7 @@ function test_return_non_empty_array(): array { } /** - * @return non-empty-array + * @return non-empty-array * Non empty keyed array with type. */ function test_return_non_empty_keyed_array(): array { @@ -854,7 +854,7 @@ function test_return_integer_max(): int { * * @param class-string $param1 * Parameter. - * @param class-string $param2 + * @param class-string<\Foo> $param2 * Parameter. * @param callable-string $param3 * Parameter. @@ -883,7 +883,7 @@ function test_return_class_string(): string { } /** - * @return class-string + * @return class-string<\Foo> * Class string. */ function test_return_class_string_foo(): string { @@ -933,10 +933,10 @@ function test_return_literal_string(): string { /** * PHP 8 intersection types are ok. * - * @param Foo&Bar $a + * @param \Foo&\Bar $a * Intersection type parameter. * - * @return Foo&Bar + * @return \Foo&\Bar * Intersection type return declaration. */ function test_intersection_types(Foo&Bar $a): Foo&Bar { diff --git a/tests/Drupal/Commenting/VariableCommentUnitTest.inc.fixed b/tests/Drupal/Commenting/VariableCommentUnitTest.inc.fixed index c0f26b08..4c761f35 100644 --- a/tests/Drupal/Commenting/VariableCommentUnitTest.inc.fixed +++ b/tests/Drupal/Commenting/VariableCommentUnitTest.inc.fixed @@ -58,7 +58,7 @@ class Test { /** * Property fooBar. * - * @var Foo|Bar|false|null + * @var \Foo|\Bar|false|null */ public Foo|Bar|FALSE|NULL $fooBar; @@ -84,7 +84,7 @@ class Test { /** * PHPStan constant wildcard. * - * @var Foo::* + * @var \Foo::* */ protected string $constant; diff --git a/tests/Drupal/bad/BadUnitTest.php b/tests/Drupal/bad/BadUnitTest.php index 8a465f2c..d7d8b4f8 100644 --- a/tests/Drupal/bad/BadUnitTest.php +++ b/tests/Drupal/bad/BadUnitTest.php @@ -309,9 +309,9 @@ protected function getErrorList(string $testFile): array 638 => 1, 646 => 2, 648 => 1, - 656 => 1, + 656 => 2, 658 => 1, - 661 => 1, + 661 => 2, 671 => 1, 678 => 1, 685 => 1, @@ -387,6 +387,14 @@ protected function getErrorList(string $testFile): array 13 => 2, 14 => 2, 16 => 1, + 26 => 1, + 31 => 1, + ]; + case 'DataTypeNamespaceUnitTest.inc': + return [ + 15 => 1, + 18 => 1, + 21 => 1, 31 => 1, ]; case 'FinallySpacingUnitTest.inc': @@ -421,8 +429,10 @@ protected function getErrorList(string $testFile): array 22 => 1, 23 => 1, 35 => 1, + 42 => 1, 56 => 1, 85 => 1, + 87 => 1, 98 => 1, ]; case 'UseLeadingBackslashUnitTest.inc': diff --git a/tests/Drupal/bad/ClassCreateInstanceUnitTest.inc.fixed b/tests/Drupal/bad/ClassCreateInstanceUnitTest.inc.fixed index db58a59c..a5f01fde 100644 --- a/tests/Drupal/bad/ClassCreateInstanceUnitTest.inc.fixed +++ b/tests/Drupal/bad/ClassCreateInstanceUnitTest.inc.fixed @@ -26,7 +26,7 @@ class ClassCreateInstanceUnitTest { /** * Using PHP 7 return type hints is fine. * - * @return ValidatorInterface[] + * @return \ValidatorInterface[] * The validators. */ public function getValidators(): array { diff --git a/tests/Drupal/Commenting/DataTypeNamespaceUnitTest.inc b/tests/Drupal/bad/DataTypeNamespaceUnitTest.inc similarity index 93% rename from tests/Drupal/Commenting/DataTypeNamespaceUnitTest.inc rename to tests/Drupal/bad/DataTypeNamespaceUnitTest.inc index b138236c..7833a0aa 100644 --- a/tests/Drupal/Commenting/DataTypeNamespaceUnitTest.inc +++ b/tests/Drupal/bad/DataTypeNamespaceUnitTest.inc @@ -7,7 +7,7 @@ use Some\Namespaced\TestClass; /** * Test. */ -class Test { +class DataTypeNamespaceUnitTest { /** * Param and Return data types should be fully namespaced. diff --git a/tests/Drupal/Commenting/DataTypeNamespaceUnitTest.inc.fixed b/tests/Drupal/bad/DataTypeNamespaceUnitTest.inc.fixed similarity index 94% rename from tests/Drupal/Commenting/DataTypeNamespaceUnitTest.inc.fixed rename to tests/Drupal/bad/DataTypeNamespaceUnitTest.inc.fixed index 4b0d53cc..857f158e 100644 --- a/tests/Drupal/Commenting/DataTypeNamespaceUnitTest.inc.fixed +++ b/tests/Drupal/bad/DataTypeNamespaceUnitTest.inc.fixed @@ -7,7 +7,7 @@ use Some\Namespaced\TestClass; /** * Test. */ -class Test { +class DataTypeNamespaceUnitTest { /** * Param and Return data types should be fully namespaced. diff --git a/tests/Drupal/bad/UnusedUseStatementUnitTest.inc.fixed b/tests/Drupal/bad/UnusedUseStatementUnitTest.inc.fixed index 8ec3a968..fbfed291 100644 --- a/tests/Drupal/bad/UnusedUseStatementUnitTest.inc.fixed +++ b/tests/Drupal/bad/UnusedUseStatementUnitTest.inc.fixed @@ -26,7 +26,7 @@ class UnusedUseStatementUnitTest { /** * Aliased type that is otherwise unused. * - * @var AliasVarName2 + * @var \Some\Data\VarName2 */ protected $y; @@ -71,7 +71,7 @@ class UnusedUseStatementUnitTest { protected function test6($x) { /** @var \Some\Data\VarName $y */ $y = $x['test']; - /** @var AliasVarName2 $z */ + /** @var \Some\Data\VarName2 $z */ $z = $x['test2']; return $y; } diff --git a/tests/Drupal/bad/bad.php.fixed b/tests/Drupal/bad/bad.php.fixed index 60d48904..265e4a86 100644 --- a/tests/Drupal/bad/bad.php.fixed +++ b/tests/Drupal/bad/bad.php.fixed @@ -672,7 +672,7 @@ function test14() { /** * Return data type documentation must not be the variable name. * - * @return foo + * @return \foo * Description bla. */ function test15() { @@ -690,12 +690,12 @@ function test16() { /** * Invalid data types. * - * @param mixed $x + * @param \type $x * Description here. * @param bool $y * Description here. * - * @return unknown_type + * @return \unknown_type * Description here. */ function test17($x, $y) { diff --git a/tests/Drupal/good/GoodDocBlock.php b/tests/Drupal/good/GoodDocBlock.php index 358a3325..46614624 100644 --- a/tests/Drupal/good/GoodDocBlock.php +++ b/tests/Drupal/good/GoodDocBlock.php @@ -105,7 +105,7 @@ public function getConfiguration() { * The second version of this test with error name with underscores * is added below. * - * @throws Exception + * @throws \Exception */ public function test6() { throw new Exception(); diff --git a/tests/Drupal/good/GoodReferenceDocs.php b/tests/Drupal/good/GoodReferenceDocs.php index 18a62213..e36ba74a 100644 --- a/tests/Drupal/good/GoodReferenceDocs.php +++ b/tests/Drupal/good/GoodReferenceDocs.php @@ -10,7 +10,7 @@ class GoodReferenceDocs { * * @param array &$form * The form array. - * @param Drupal\Core\Form\FormStateInterface $form_state + * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state. * * @return array @@ -32,7 +32,7 @@ public function removeQueueItem(array &$form, FormStateInterface $form_state) { /** * Parameters described by reference are OK. * - * @param Drupal\Core\Form\FormStateInterface $form_state + * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state. * @param array &$old_form * The old form build. diff --git a/tests/Drupal/good/good.php b/tests/Drupal/good/good.php index 1979f649..668a7c03 100644 --- a/tests/Drupal/good/good.php +++ b/tests/Drupal/good/good.php @@ -821,7 +821,7 @@ function mymodule_foo_bar_block() { /** * Not documenting all parameters is allowed. * - * @param Node $node + * @param \Drupal\node\Entity\Node $node * The loaded node entity that we will use to do whatever. */ function mymodule_form_callback($form, &$form_state, Node $node) { @@ -947,7 +947,7 @@ function test7($x) { * @param int $x * Exclamation mark allowed! * - * @throws MyException + * @throws \MyException * Exclamation mark allowed! */ function test8($x) {