Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sniff::get_declared_namespace_name(): improve code-style independence #1491

Merged
merged 1 commit into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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