Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Flyeralarm\CodingGuidelines\Flyeralarm\Sniffs\Classes;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use RuntimeException;

class FullyQualifiedSniff implements Sniff
{
/**
* @return array
*/
public function register()
{
return array(T_DOUBLE_COLON, T_NEW);
}

/**
* @param File $phpcsFile
* @param int $stackPtr
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$classCall = $this->getClassCall($phpcsFile, $stackPtr);

if (strpos($classCall, '\\') === false) {
return;
}

$phpcsFile->addError(
'Qualifier should be replaced with an import: "%s"',
$stackPtr,
'Found',
[$classCall]
);
}

private function getClassCall(File $phpcsFile, $stackPtr): string
{
$tokens = $phpcsFile->getTokens();

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

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

return $phpcsFile->getTokensAsString(
$classCallStart,
$stackPtr - $classCallStart
);
}

throw new RuntimeException(sprintf(
'Unknown token type: "%s"',
$tokens[$stackPtr]['type']
));
}
}
16 changes: 16 additions & 0 deletions tests/rules/classes/allowed/FullyQualifiedSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

// @expectedPass

namespace flyeralarm\Test;

use RuntimeException;

class FullyQualifiedSniff
{
public function a()
{
$className = RuntimeException::class;
$a = new RuntimeException();
}
}
13 changes: 13 additions & 0 deletions tests/rules/classes/not-allowed/FullyQualifiedSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

namespace flyeralarm\Test;

class FullyQualifiedSniff
{
public function a()
{
$a = new \RuntimeException();
}
}
13 changes: 13 additions & 0 deletions tests/rules/classes/not-allowed/FullyQualifiedSniffStatic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

// @expectedError Qualifier should be replaced with an import: "$a = \RuntimeException"

namespace flyeralarm\Test;

class FullyQualifiedSniffStatic
{
public function a()
{
$a = \RuntimeException::class;
}
}
18 changes: 18 additions & 0 deletions tests/ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<ruleset name="FLYERALARM Coding Guidelines">
<description>A custom coding standard for FLYERALARM</description>
<rule ref="PSR2"/>
<rule ref="Generic.Formatting.SpaceAfterCast"/>
<rule ref="Generic.Arrays.DisallowLongArraySyntax.Found"/>
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
<rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
<rule ref="./custom-standards/Flyeralarm/Sniffs/File/ExceptionMessageSniff.php"/>
<rule ref="./custom-standards/Flyeralarm/Sniffs/File/ForbiddenKeywordsSniff.php"/>
<rule ref="./custom-standards/Flyeralarm/Sniffs/File/NamespacesSniff.php"/>
<rule ref="./custom-standards/Flyeralarm/Sniffs/File/NoClassKindSuffixSniff.php"/>
<rule ref="./custom-standards/Flyeralarm/Sniffs/Docblock/ReturnTypeSniff.php"/>
<rule ref="./custom-standards/Flyeralarm/Sniffs/Docblock/ExpectedExceptionMessageSniff.php"/>
<rule ref="./custom-standards/Flyeralarm/Sniffs/Variable/LowerCamelCaseSniff.php"/>
<rule ref="./custom-standards/Flyeralarm/Sniffs/ControlStructures/YodaSniff.php"/>
<rule ref="./custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php"/>
</ruleset>
2 changes: 1 addition & 1 deletion tests/runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function processDir($dirPath)
sprintf(
"%s -w -p -s --standard=%s %s",
escapeshellcmd(__DIR__ . '/../vendor/bin/phpcs'),
escapeshellarg(__DIR__ . '/../ruleset.xml'),
escapeshellarg(__DIR__ . '/ruleset.xml'),
escapeshellarg($dirPath . $file)
)
);
Expand Down