The Commenting.DataTypeNamespace sniff tries to match each use statement against each @var, @param and @return statement to check that they are fully namespaced. However, the way that it is coded currently can lead to matches with more than one 'use' statement and we get the same error reported more than once. Take this real snippet from Rules module http://cgit.drupalcode.org/rules/tree/tests/src/Unit/Integration/Action/...

use Drupal\rules\Engine\RulesComponent;
use Drupal\rules\Entity\RulesComponentConfig;

then later

   * @param RulesComponentConfig[] $rules_configs

The method for matching involves the if clause strpos($tokens[($tag + 2)]['content'], $tokens[$classPtr]['content']) === 0 where $tokens[($tag + 2) is going to be RulesComponentConfig[] $rules_configs and $tokens[$classPtr] will be the class name in the use statement. The strPos( ) line matches both of these use statements, when it should match only 'RulesComponentConfig'. This is because 'RulesComponent' is a substring of 'RulesComponentConfig' which also starts at position 0.

Maybe we shoud explode $tokens[($tag + 2)]['content'] on space and [ ] and then check the value directly, instead of using strPos()

Comments

jonathan1055 created an issue. See original summary.

jonathan1055’s picture

Something like

preg_split('/\W/', $tokens[($tag + 2)]['content'])[0] === $tokens[$classPtr]['content']

instead of

strpos($tokens[($tag + 2)]['content'], $tokens[$classPtr]['content']) === 0

This works for the example in Rules module, and we get only one error on line 177 instead of two. Patch to follow - but I guess this will also need a test to prove it is the correct fix.

jonathan1055’s picture

StatusFileSize
new1.13 KB

Here's the current phpcs output on the Rules file in the example

klausi’s picture

Version: 8.x-2.x-dev » 9.x-dev
Status: Active » Closed (won't fix)

The DataTypeNamespace sniff has been removed in Coder 9. We use sniffs from Slevomat now instead. Please open a new issue if there is still a problem with that.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.