Skip to content

Commit

Permalink
DisallowFqn - fixed issues when backslash was removed but there was n…
Browse files Browse the repository at this point in the history
…o space
  • Loading branch information
michalbundyra committed May 6, 2019
1 parent d0d09ff commit 838784a
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/WebimpressCodingStandard/Sniffs/PHP/DisallowFqnSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use const GLOB_NOSORT;
use const T_BITWISE_AND;
use const T_BITWISE_OR;
use const T_CASE;
use const T_CATCH;
use const T_CLOSE_PARENTHESIS;
use const T_CLOSURE;
Expand All @@ -45,17 +46,29 @@
use const T_DOC_COMMENT_TAG;
use const T_DOC_COMMENT_WHITESPACE;
use const T_DOUBLE_COLON;
use const T_ECHO;
use const T_ELLIPSIS;
use const T_EXTENDS;
use const T_FUNCTION;
use const T_IMPLEMENTS;
use const T_INCLUDE;
use const T_INCLUDE_ONCE;
use const T_INSTANCEOF;
use const T_INSTEADOF;
use const T_LOGICAL_AND;
use const T_LOGICAL_OR;
use const T_LOGICAL_XOR;
use const T_NAMESPACE;
use const T_NEW;
use const T_NS_SEPARATOR;
use const T_NULLABLE;
use const T_OPEN_PARENTHESIS;
use const T_PRINT;
use const T_REQUIRE;
use const T_REQUIRE_ONCE;
use const T_RETURN;
use const T_STRING;
use const T_THROW;
use const T_USE;
use const T_VARIABLE;

Expand Down Expand Up @@ -260,7 +273,17 @@ private function processString(File $phpcsFile, int $stackPtr, string $namespace
true
);

if (in_array($tokens[$prev]['code'], [T_NEW, T_USE, T_EXTENDS, T_IMPLEMENTS, T_INSTANCEOF, T_NULLABLE], true)
$prevClassTokens = [
T_NEW,
T_USE,
T_EXTENDS,
T_IMPLEMENTS,
T_INSTANCEOF,
T_INSTEADOF,
T_NULLABLE,
];

if (in_array($tokens[$prev]['code'], $prevClassTokens, true)
|| in_array($tokens[$next]['code'], [T_VARIABLE, T_ELLIPSIS, T_DOUBLE_COLON], true)
) {
$type = 'class';
Expand Down Expand Up @@ -465,7 +488,31 @@ private function error(
if ($fix) {
$tokens = $phpcsFile->getTokens();

if (in_array($tokens[$stackPtr - 1]['code'], [
T_NEW,
T_USE,
T_EXTENDS,
T_IMPLEMENTS,
T_INSTANCEOF,
T_INSTEADOF,
T_CASE,
T_PRINT,
T_ECHO,
T_REQUIRE,
T_REQUIRE_ONCE,
T_INCLUDE,
T_INCLUDE_ONCE,
T_RETURN,
T_LOGICAL_AND,
T_LOGICAL_OR,
T_LOGICAL_XOR,
T_THROW,
], true)) {
$expected = ' ' . $expected;
}

$phpcsFile->fixer->beginChangeset();

$phpcsFile->fixer->replaceToken($stackPtr, $expected);
$i = $stackPtr;
while (isset($tokens[++$i])) {
Expand Down
40 changes: 40 additions & 0 deletions test/Sniffs/PHP/DisallowFqnUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,44 @@ class TheClass extends \ MyNamespace \ Hello \ ParentClass implements \ArrayAcce

return \T_WHITESPACE + T_ELLIPSIS;
}

public function missingSpace()
{
return new class () extends\DateTime implements\Countable
{
use\MyTrait {
A::big insteadof\B;
}

public function checkInstance($a) : bool
{
return $a instanceof\Exception;
}

public function conditions(string $a) : ?string
{
switch ($a) {
case\DateTime::class:
return\DateTime::class;
case\Countable::class:
print\Countable::class;
echo\Countable::class;
return null;
case 'require':
require\DateTime::class.'.php';
require_once\DateTime::class.'.php';
include\DateTime::class.'.php';
include_once\DateTime::class.'.php';
case 'new':
new\DateTime();
}

if ($a and\DateTime::class) {
} elseif ($a or\DateTime::class) {
} elseif ($a xor\DateTime::class) {}

throw\RuntimeException::something();
}
};
}
}
42 changes: 42 additions & 0 deletions test/Sniffs/PHP/DisallowFqnUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use const Bar\Baz\BAR_BAZ;
use const Name\NAME_CONST;
use const Name\CONST_IN_SWITCH;
use const Name\OTHER_CONST_IN_SWITCH;
use MyTrait;
use B;

use \ArrayObject as AO;

Expand Down Expand Up @@ -127,4 +129,44 @@ class TheClass extends ParentClass implements ArrayAccess, Countable

return \T_WHITESPACE + T_ELLIPSIS;
}

public function missingSpace()
{
return new class () extends DateTime implements Countable
{
use MyTrait {
A::big insteadof B;
}

public function checkInstance($a) : bool
{
return $a instanceof Exception;
}

public function conditions(string $a) : ?string
{
switch ($a) {
case DateTime::class:
return DateTime::class;
case Countable::class:
print Countable::class;
echo Countable::class;
return null;
case 'require':
require DateTime::class.'.php';
require_once DateTime::class.'.php';
include DateTime::class.'.php';
include_once DateTime::class.'.php';
case 'new':
new DateTime();
}

if ($a and DateTime::class) {
} elseif ($a or DateTime::class) {
} elseif ($a xor DateTime::class) {}

throw RuntimeException::something();
}
};
}
}
18 changes: 18 additions & 0 deletions test/Sniffs/PHP/DisallowFqnUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ protected function getErrorList(string $testFile = '') : array
86 => 2,
90 => 1,
94 => 1,
110 => 2,
112 => 1,
113 => 1,
118 => 1,
124 => 1,
125 => 1,
126 => 1,
127 => 1,
128 => 1,
131 => 1,
132 => 1,
133 => 1,
134 => 1,
136 => 1,
139 => 1,
140 => 1,
141 => 1,
143 => 1,
];
}

Expand Down

0 comments on commit 838784a

Please sign in to comment.