Skip to content

Commit 15b2b46

Browse files
committed
Don't try to find references in double quoted string - it's difficult to parse it with PHPCS
This reverts commit add9082. This reverts commit db24381.
1 parent d58c167 commit 15b2b46

File tree

6 files changed

+6
-38
lines changed

6 files changed

+6
-38
lines changed

SlevomatCodingStandard/Helpers/ReferencedNameHelper.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use function array_values;
1010
use function count;
1111
use function in_array;
12-
use function preg_match;
1312
use const T_ANON_CLASS;
1413
use const T_ARRAY;
1514
use const T_AS;
@@ -24,7 +23,6 @@
2423
use const T_CONST;
2524
use const T_DECLARE;
2625
use const T_DOUBLE_COLON;
27-
use const T_DOUBLE_QUOTED_STRING;
2826
use const T_ELLIPSIS;
2927
use const T_EXTENDS;
3028
use const T_FUNCTION;
@@ -129,7 +127,7 @@ private static function createAllReferencedNames(File $phpcsFile, int $openTagPo
129127
$referencedNames = [];
130128

131129
$beginSearchAtPointer = $openTagPointer + 1;
132-
$nameTokenCodes = array_merge([T_DOUBLE_QUOTED_STRING], TokenHelper::getNameTokenCodes());
130+
$nameTokenCodes = TokenHelper::getNameTokenCodes();
133131
$tokens = $phpcsFile->getTokens();
134132

135133
while (true) {
@@ -138,18 +136,6 @@ private static function createAllReferencedNames(File $phpcsFile, int $openTagPo
138136
break;
139137
}
140138

141-
if ($tokens[$nameStartPointer]['code'] === T_DOUBLE_QUOTED_STRING) {
142-
if (
143-
preg_match('~(\$)?(' . TypeHelper::REGEXP . ')::~', $tokens[$nameStartPointer]['content'], $matches) === 1
144-
&& $matches[1] === ''
145-
) {
146-
$referencedNames[] = new ReferencedName($matches[2], $nameStartPointer, $nameStartPointer, ReferencedName::TYPE_CLASS);
147-
}
148-
149-
$beginSearchAtPointer = $nameStartPointer + 1;
150-
continue;
151-
}
152-
153139
// Attributes are parsed in specific method
154140
$attributeStartPointerBefore = TokenHelper::findPrevious($phpcsFile, T_ATTRIBUTE, $nameStartPointer - 1, $beginSearchAtPointer);
155141
if ($attributeStartPointerBefore !== null) {

SlevomatCodingStandard/Helpers/TypeHelper.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
class TypeHelper
1313
{
1414

15-
public const REGEXP = '\\\\?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*';
16-
1715
/**
1816
* Validates type name according to the allowed characters in type names + namespaces
1917
*
@@ -24,7 +22,11 @@ class TypeHelper
2422
public static function isTypeName(string $typeName): bool
2523
{
2624
$matches = [];
27-
$result = preg_match('~^' . self::REGEXP . '$~', $typeName, $matches);
25+
$result = preg_match(
26+
'~^\\\\?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*$~',
27+
$typeName,
28+
$matches
29+
);
2830
if ($result === false) {
2931
// @codeCoverageIgnoreStart
3032
throw new Exception('PREG error ' . preg_last_error());

tests/Helpers/ReferencedNameHelperTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public function testGetAllReferencedNames(): void
6767
['STR_PAD_RIGHT', false, true],
6868
['EnumType', false, false],
6969
['UrlGeneratorInterface', false, false],
70-
['ClassInString', false, false],
7170
];
7271

7372
$names = ReferencedNameHelper::getAllReferencedNames($phpcsFile, 0);

tests/Helpers/data/lotsOfReferencedNames.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,3 @@ public function generateRoute($router): string
176176
], referenceType: UrlGeneratorInterface::RELATIVE_PATH);
177177
}
178178
}
179-
180-
class InString
181-
{
182-
183-
public function doSomething()
184-
{
185-
$a = "String interpolation:{$this->getParameter(ClassInString::PARAM_NAME)}";
186-
$b = "String interpolation:{$this->getParameter($noClassInString::PARAM_NAME)}";
187-
188-
return $a . $b;
189-
}
190-
191-
}

tests/Sniffs/Namespaces/UnusedUsesSniffTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ public function testUnusedUse(): void
6666

6767
// Used class with static variable
6868
self::assertNoSniffError($report, 29);
69-
70-
// Used in string
71-
self::assertNoSniffError($report, 30);
7269
}
7370

7471
public function testUnusedUseWithMultipleNamespaces(): void

tests/Sniffs/Namespaces/data/unusedUses.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use EnumClass;
2828
use ReferenceClass;
2929
use ClassWithStaticVariable;
30-
use ClassInString;
3130

3231
class TestClass implements FirstInterface, SecondInterface
3332
{
@@ -57,8 +56,6 @@ enum_type: EnumClass::VALUE(),
5756
reference_type: ReferenceClass::SOME_CONSTANT
5857
);
5958

60-
$string = "String interpolation:{$this->getParameter(ClassInString::PARAM_NAME)}";
61-
6259
return new NewObject();
6360
}
6461

0 commit comments

Comments
 (0)