Skip to content

Commit 85863d6

Browse files
author
Michael Nozdrevatykh
committed
Improve fully qualified class sniff to check inheritance classes too
1 parent 6a5a156 commit 85863d6

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

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

Lines changed: 13 additions & 1 deletion
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
/**
@@ -61,6 +61,18 @@ private function getClassCall(File $phpcsFile, $stackPtr): string
6161
$classCallStart,
6262
$stackPtr - $classCallStart
6363
);
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+
));
6476
}
6577

6678
throw new RuntimeException(sprintf(

tests/rules/classes/allowed/FullyQualifiedSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
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
{

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
}
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)