Skip to content

Commit

Permalink
Merge 6e6c31b into d0d09ff
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed May 9, 2019
2 parents d0d09ff + 6e6c31b commit c355161
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
use function class_exists;
use function interface_exists;
use function ltrim;
use function str_replace;
use function preg_match;
use function strpos;
use function strtr;
use function trait_exists;
use function trim;

use const T_CONSTANT_ENCAPSED_STRING;

Expand All @@ -37,7 +37,18 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$name = trim(str_replace(['"', "'"], '', $tokens[$stackPtr]['content']));
$name = strtr($tokens[$stackPtr]['content'], [
'"' => '',
"'" => '',
'\\\\' => '\\',
]);

if (strpos($name, '\\\\') !== false
|| preg_match('/\s/', $name)
) {
return;
}

if (class_exists($name) || interface_exists($name) || trait_exists($name)) {
$error = 'String "%s" contains class reference, use ::class instead';
$data = [$name];
Expand Down
9 changes: 9 additions & 0 deletions test/Sniffs/Formatting/StringClassReferenceUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ $trait = 'WebimpressCodingStandard\Helper\NamespacesTrait';

$nonExistingClass = 'MyNamespace\ClassDoesNotExist';
$nonSlash = 'DateTime';

$doubleNamespaceSeparator = 'WebimpressCodingStandardTest\\Ruleset';
$fourBackslashes = 'WebimpressCodingStandardTest\\\\Ruleset';
$spaceInTheName = 'WebimpressCodingStandardTest Ruleset';
$newLineInTheName = 'WebimpressCodingStandardTest
Ruleset';

$spaceAtTheBeginning = ' WebimpressCodingStandardTest\\Ruleset';
$spaceAtTheEnd = ' WebimpressCodingStandardTest\\Ruleset ';
9 changes: 9 additions & 0 deletions test/Sniffs/Formatting/StringClassReferenceUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ $trait = \WebimpressCodingStandard\Helper\NamespacesTrait::class;

$nonExistingClass = 'MyNamespace\ClassDoesNotExist';
$nonSlash = 'DateTime';

$doubleNamespaceSeparator = \WebimpressCodingStandardTest\Ruleset::class;
$fourBackslashes = 'WebimpressCodingStandardTest\\\\Ruleset';
$spaceInTheName = 'WebimpressCodingStandardTest Ruleset';
$newLineInTheName = 'WebimpressCodingStandardTest
Ruleset';

$spaceAtTheBeginning = ' WebimpressCodingStandardTest\\Ruleset';
$spaceAtTheEnd = ' WebimpressCodingStandardTest\\Ruleset ';
1 change: 1 addition & 0 deletions test/Sniffs/Formatting/StringClassReferenceUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ protected function getErrorList(string $testFile = '') : array
7 => 1,
8 => 1,
10 => 1,
15 => 1,
];
}

Expand Down

0 comments on commit c355161

Please sign in to comment.