Skip to content

Commit

Permalink
Merge pull request #1491 from WordPress-Coding-Standards/feature/get-…
Browse files Browse the repository at this point in the history
…namespace-name-codestyle-independence

Sniff::get_declared_namespace_name(): improve code-style independence
  • Loading branch information
jrfnl authored Oct 31, 2018
2 parents 3d685d5 + 600d5c7 commit 62c6cc7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 8 additions & 6 deletions WordPress/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2369,29 +2369,31 @@ public function get_declared_namespace_name( $stackPtr ) {
return false;
}

if ( \T_NS_SEPARATOR === $this->tokens[ ( $stackPtr + 1 ) ]['code'] ) {
$nextToken = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true );
if ( \T_NS_SEPARATOR === $this->tokens[ $nextToken ]['code'] ) {
// Not a namespace declaration, but use of, i.e. `namespace\someFunction();`.
return false;
}

$nextToken = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true, null, true );
if ( \T_OPEN_CURLY_BRACKET === $this->tokens[ $nextToken ]['code'] ) {
// Declaration for global namespace when using multiple namespaces in a file.
// I.e.: `namespace {}`.
return '';
}

// Ok, this should be a namespace declaration, so get all the parts together.
$validTokens = array(
$acceptedTokens = array(
\T_STRING => true,
\T_NS_SEPARATOR => true,
\T_WHITESPACE => true,
);
$validTokens = $acceptedTokens + Tokens::$emptyTokens;

$namespaceName = '';
while ( isset( $validTokens[ $this->tokens[ $nextToken ]['code'] ] ) ) {
$namespaceName .= trim( $this->tokens[ $nextToken ]['content'] );
$nextToken++;
if ( isset( $acceptedTokens[ $this->tokens[ $nextToken ]['code'] ] ) ) {
$namespaceName .= trim( $this->tokens[ $nextToken ]['content'] );
}
++$nextToken;
}

return $namespaceName;
Expand Down
14 changes: 14 additions & 0 deletions WordPress/Tests/WP/GlobalVariablesOverrideUnitTest.4.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

// @codingStandardsChangeSetting WordPress.WP.GlobalVariablesOverride custom_test_class_whitelist My\NameSp\TestClass

namespace My \ /* comment */ NameSp;

class Test_Class_D extends TestClass {

public function test_something() {
global $tabs;
$tabs = 50; // Ok.
}
}
// @codingStandardsChangeSetting WordPress.WP.GlobalVariablesOverride custom_test_class_whitelist false

0 comments on commit 62c6cc7

Please sign in to comment.