Skip to content

Commit

Permalink
Merge pull request #19 from cmikle/FullyQualifiedSniffBugfix
Browse files Browse the repository at this point in the history
Fixed problem in FullyQualifiedSniff with slashes in string params
  • Loading branch information
MichelHartmann committed Oct 12, 2020
2 parents ec5f359 + 85863d6 commit ebfe388
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
22 changes: 20 additions & 2 deletions custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php
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 @@ -43,18 +43,36 @@ private function getClassCall(File $phpcsFile, $stackPtr): string

switch ($tokens[$stackPtr]['code']) {
case T_NEW:
return $phpcsFile->getTokensAsString(
$tokensAsString = $phpcsFile->getTokensAsString(
$stackPtr,
$phpcsFile->findEndOfStatement($stackPtr) - $stackPtr
);

return substr(
$tokensAsString,
0,
strpos($tokensAsString, '(')
);

case T_DOUBLE_COLON:
$classCallStart = $phpcsFile->findStartOfStatement($stackPtr);

return $phpcsFile->getTokensAsString(
$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
10 changes: 9 additions & 1 deletion tests/rules/classes/allowed/FullyQualifiedSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
namespace flyeralarm\Test;

use RuntimeException;
use stdClass;

class FullyQualifiedSniff
class FullyQualifiedSniff extends stdClass
{
public function a()
{
$className = RuntimeException::class;
$a = new RuntimeException();
}

public function b()
{
$a = new RuntimeException(
'We can\'t explain'
);
}
}
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
{
}
2 changes: 1 addition & 1 deletion tests/rules/classes/not-allowed/FullyQualifiedSniff.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

namespace flyeralarm\Test;

Expand Down
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 ebfe388

Please sign in to comment.