Skip to content

Commit ebfe388

Browse files
Merge pull request #19 from cmikle/FullyQualifiedSniffBugfix
Fixed problem in FullyQualifiedSniff with slashes in string params
2 parents ec5f359 + 85863d6 commit ebfe388

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class FullyQualifiedSniff implements Sniff
1313
*/
1414
public function register()
1515
{
16-
return array(T_DOUBLE_COLON, T_NEW);
16+
return array(T_DOUBLE_COLON, T_NEW, T_EXTENDS);
1717
}
1818

1919
/**
@@ -43,18 +43,36 @@ private function getClassCall(File $phpcsFile, $stackPtr): string
4343

4444
switch ($tokens[$stackPtr]['code']) {
4545
case T_NEW:
46-
return $phpcsFile->getTokensAsString(
46+
$tokensAsString = $phpcsFile->getTokensAsString(
4747
$stackPtr,
4848
$phpcsFile->findEndOfStatement($stackPtr) - $stackPtr
4949
);
5050

51+
return substr(
52+
$tokensAsString,
53+
0,
54+
strpos($tokensAsString, '(')
55+
);
56+
5157
case T_DOUBLE_COLON:
5258
$classCallStart = $phpcsFile->findStartOfStatement($stackPtr);
5359

5460
return $phpcsFile->getTokensAsString(
5561
$classCallStart,
5662
$stackPtr - $classCallStart
5763
);
64+
65+
case T_EXTENDS:
66+
$tokensAsString = $phpcsFile->getTokensAsString(
67+
$stackPtr,
68+
$phpcsFile->findEndOfStatement($stackPtr) - $stackPtr
69+
);
70+
71+
return trim(substr(
72+
$tokensAsString,
73+
0,
74+
strpos($tokensAsString, '{')
75+
));
5876
}
5977

6078
throw new RuntimeException(sprintf(

tests/rules/classes/allowed/FullyQualifiedSniff.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
namespace flyeralarm\Test;
66

77
use RuntimeException;
8+
use stdClass;
89

9-
class FullyQualifiedSniff
10+
class FullyQualifiedSniff extends stdClass
1011
{
1112
public function a()
1213
{
1314
$className = RuntimeException::class;
1415
$a = new RuntimeException();
1516
}
17+
18+
public function b()
19+
{
20+
$a = new RuntimeException(
21+
'We can\'t explain'
22+
);
23+
}
1624
}

tests/rules/classes/allowed/NamespaceVendorLowercasePackageUpperCamelCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace flyeralarm\FooBar;
66

7-
class NamespaceVendorLowercasePackageUpperCamelCase extends \PHP_CodeSniffer_File
7+
use PHP_CodeSniffer_File;
8+
9+
class NamespaceVendorLowercasePackageUpperCamelCase extends PHP_CodeSniffer_File
810
{
911
}

tests/rules/classes/not-allowed/FullyQualifiedSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
// @expectedError Qualifier should be replaced with an import: "new \RuntimeException()"
3+
// @expectedError Qualifier should be replaced with an import: "new \RuntimeException"
44

55
namespace flyeralarm\Test;
66

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
// @expectedError Qualifier should be replaced with an import: "extends \stdClass"
4+
5+
namespace flyeralarm\Test;
6+
7+
class FullyQualifiedSniffExtends extends \stdClass
8+
{
9+
public function a()
10+
{
11+
$a = 0;
12+
}
13+
}

0 commit comments

Comments
 (0)