Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* \Drupal\Sniffs\Classes\InterfaceImplementedSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

namespace Drupal\Sniffs\Classes;

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

/**
* Checks that class or interface name does not identical with the filename.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class ClassOrInterfaceNameNotIdenticalWithFilenameSniff implements Sniff
{


/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [
T_CLASS,
T_INTERFACE,
];

}//end register()


/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$pathInfo = pathinfo($phpcsFile->getFilename());
$filename = $pathInfo['filename'];
$nameIndex = $phpcsFile->findNext(T_STRING, $stackPtr);
if ($nameIndex === false) {
return;
}

$classOrInterfaceName = $tokens[$nameIndex]['content'];
if ($filename !== $classOrInterfaceName && strtolower($filename) === strtolower($classOrInterfaceName)) {
$type = $tokens[$stackPtr]['type'];
if ($type === 'T_CLASS') {
$phpcsFile->addFixableWarning("Class name '$classOrInterfaceName' is not identical with its filename '$filename' ", $nameIndex, 'ClassOrInterfaceNameNotIdenticalWithFilename');
} else {
$phpcsFile->addFixableWarning("Interface name '$classOrInterfaceName' is not identical with its filename '$filename' ", $nameIndex, 'ClassOrInterfaceNameNotIdenticalWithFilename');
}

if ($phpcsFile->fixer !== null) {
$phpcsFile->fixer->replaceToken($nameIndex, $filename);
}
}

}//end process()


}//end class
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

class ClassOrInterfaceNameNotIdenticalWithFilename {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/**
*
*/
class classOrInterfaceNameNotIdenticalWithFilename {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/**
*
*/
class ClassOrInterfaceNameNotIdenticalWithFilename {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/**
*
*/
interface classOrInterfaceNameNotIdenticalWithFilenameInterface {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/**
*
*/
interface ClassOrInterfaceNameNotIdenticalWithFilenameInterface {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Drupal\Test\Classes;

use Drupal\Test\CoderSniffUnitTest;

class ClassOrInterfaceNameNotIdenticalWithFilenameUnitTest extends CoderSniffUnitTest
{


/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
protected function getErrorList(string $testFile): array
{
return [];

}//end getErrorList()


/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
protected function getWarningList(string $testFile): array
{
if ($testFile === 'ClassOrInterfaceNameNotIdenticalWithFilename.inc') {
return [6 => 1];
}

if ($testFile === 'ClassOrInterfaceNameNotIdenticalWithFilenameInterface.inc') {
return [6 => 1];
}

return [];

}//end getWarningList()


/**
* Returns a list of test files that should be checked.
*
* @param string $testFileBase The base path that the unit tests files will have.
*
* @return array<string>
*/
protected function getTestFiles($testFileBase): array
{
return [
__DIR__.'/ClassOrInterfaceNameNotIdenticalWithFilename.inc',
__DIR__.'/ClassOrInterfaceNameNotIdenticalWithFilenameInterface.inc',
];

}//end getTestFiles()


}//end class