Skip to content

Commit

Permalink
Improve fully qualified class sniff to check inheritance classes too
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Nozdrevatykh committed Oct 9, 2020
1 parent 6a5a156 commit 85863d6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FullyQualifiedSniff implements Sniff
*/
public function register()
{
return array(T_DOUBLE_COLON, T_NEW);
return array(T_DOUBLE_COLON, T_NEW, T_EXTENDS);
}

/**
Expand Down Expand Up @@ -61,6 +61,18 @@ private function getClassCall(File $phpcsFile, $stackPtr): string
$classCallStart,
$stackPtr - $classCallStart
);

case T_EXTENDS:
$tokensAsString = $phpcsFile->getTokensAsString(
$stackPtr,
$phpcsFile->findEndOfStatement($stackPtr) - $stackPtr
);

return trim(substr(
$tokensAsString,
0,
strpos($tokensAsString, '{')
));
}

throw new RuntimeException(sprintf(
Expand Down
3 changes: 2 additions & 1 deletion tests/rules/classes/allowed/FullyQualifiedSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace flyeralarm\Test;

use RuntimeException;
use stdClass;

class FullyQualifiedSniff
class FullyQualifiedSniff extends stdClass
{
public function a()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace flyeralarm\FooBar;

class NamespaceVendorLowercasePackageUpperCamelCase extends \PHP_CodeSniffer_File
use PHP_CodeSniffer_File;

class NamespaceVendorLowercasePackageUpperCamelCase extends PHP_CodeSniffer_File
{
}
13 changes: 13 additions & 0 deletions tests/rules/classes/not-allowed/FullyQualifiedSniffExtends.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

// @expectedError Qualifier should be replaced with an import: "extends \stdClass"

namespace flyeralarm\Test;

class FullyQualifiedSniffExtends extends \stdClass
{
public function a()
{
$a = 0;
}
}

0 comments on commit 85863d6

Please sign in to comment.