Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squiz/ClassFileName: various improvements #845

Merged
merged 6 commits into from
Mar 7, 2025
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
38 changes: 21 additions & 17 deletions src/Standards/Squiz/Sniffs/Classes/ClassFileNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

class ClassFileNameSniff implements Sniff
{
Expand All @@ -23,12 +24,10 @@ class ClassFileNameSniff implements Sniff
*/
public function register()
{
return [
T_CLASS,
T_INTERFACE,
T_TRAIT,
T_ENUM,
];
$targets = Tokens::$ooScopeTokens;
unset($targets[T_ANON_CLASS]);

return $targets;

}//end register()

Expand All @@ -44,22 +43,27 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
$fullPath = basename($phpcsFile->getFilename());
$fileName = substr($fullPath, 0, strrpos($fullPath, '.'));
if ($fileName === '') {
// No filename probably means STDIN, so we can't do this check.
return;
$fullPath = $phpcsFile->getFilename();
if ($fullPath === 'STDIN') {
return $phpcsFile->numTokens;
}

$tokens = $phpcsFile->getTokens();
$decName = $phpcsFile->findNext(T_STRING, $stackPtr);
$fileName = basename($fullPath);
$fileNoExt = substr($fileName, 0, strrpos($fileName, '.'));
$extension = substr($fileName, (strrpos($fileName, '.') + 1));

$tokens = $phpcsFile->getTokens();
$ooName = $phpcsFile->getDeclarationName($stackPtr);
if ($ooName === null) {
// Probably parse error/live coding.
return;
}

if ($tokens[$decName]['content'] !== $fileName) {
$error = '%s name doesn\'t match filename; expected "%s %s"';
if ($ooName !== $fileNoExt) {
$error = 'Filename doesn\'t match %s name; expected file name "%s"';
$data = [
ucfirst($tokens[$stackPtr]['content']),
$tokens[$stackPtr]['content'],
$fileName,
$ooName.'.'.$extension,
];
$phpcsFile->addError($error, $stackPtr, 'NoMatch', $data);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Test to demonstrate that file names should follow class names, not the other way around.
// Making the class name comply with the file name would result in a parse error for this file,
// as the file name doesn't comply with the naming conventions for PHP symbol names.

class ClassFileNameSpacesInFilenameUnitTest {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Test to demonstrate that file names should follow class names, not the other way around.
// Making the class name comply with the file name would result in a parse error for this file,
// as the file name doesn't comply with the naming conventions for PHP symbol names.

class ClassFileNameDashesInFilenameUnitTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Intentional parse error. This should be the only test in this file.
// Live coding/missing curlies should not block the sniff from functioning.

class Class_FileName_Live_Coding_Fail_UnitTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Intentional parse error. This should be the only test in this file.
// Live coding/missing curlies should not block the sniff from functioning.

class ClassFileNameLiveCodingPassUnitTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Intentional parse error. This should be the only test in this file.
// Class missing class name should be ignored.

class
9 changes: 4 additions & 5 deletions src/Standards/Squiz/Tests/Classes/ClassFileNameUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CLASSFILENAMEUNITTEST {}
interface classFileNameUnitTest {}
interface classfilenameunittest {}
interface CLASSFILENAMEUNITTEST {}
trait classFileNameUnitTest {}
trait /*comment*/ classFileNameUnitTest {}
trait classfilenameunittest {}
trait CLASSFILENAMEUNITTEST {}
enum classFileNameUnitTest {}
Expand All @@ -39,7 +39,6 @@ trait ExtraClassFileNameUnitTest {}
enum CompletelyWrongClassName {}
enum ClassFileNameUnitTestExtra {}
enum ClassFileNameUnitTestInc {}
enum ExtraClassFileNameUnitTest {}


?>
enum
// Comment
ExtraClassFileNameUnitTest {}
126 changes: 95 additions & 31 deletions src/Standards/Squiz/Tests/Classes/ClassFileNameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace PHP_CodeSniffer\Standards\Squiz\Tests\Classes;

use DirectoryIterator;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
Expand All @@ -20,46 +21,109 @@ final class ClassFileNameUnitTest extends AbstractSniffUnitTest
{


/**
* Get a list of all test files to check.
*
* These will have the same base as the sniff name but different extensions.
* We ignore the .php file as it is the class.
*
* @param string $testFileBase The base path that the unit tests files will have.
*
* @return string[]
*/
protected function getTestFiles($testFileBase)
{
$testFiles = [];

$dir = substr($testFileBase, 0, strrpos($testFileBase, DIRECTORY_SEPARATOR));
$di = new DirectoryIterator($dir);

// Strip off the path and the "UnitTest." suffix from the $testFileBase to allow
// for some less conventionally named test case files.
$fileBase = str_replace($dir, '', $testFileBase);
$fileBase = substr($fileBase, 1, -9);

foreach ($di as $file) {
$fileName = $file->getBasename('UnitTest.inc');
$extension = $file->getExtension();
if (substr($fileName, 0, strlen($fileBase)) === $fileBase
&& $extension === 'inc'
) {
$testFiles[] = $file->getPathname();
}
}

// Put them in order.
sort($testFiles, SORT_NATURAL);

return $testFiles;

}//end getTestFiles()


/**
* 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>
*/
public function getErrorList()
public function getErrorList($testFile='')
{
return [
12 => 1,
13 => 1,
14 => 1,
15 => 1,
16 => 1,
17 => 1,
18 => 1,
19 => 1,
20 => 1,
21 => 1,
22 => 1,
23 => 1,
27 => 1,
28 => 1,
29 => 1,
30 => 1,
31 => 1,
32 => 1,
33 => 1,
34 => 1,
35 => 1,
36 => 1,
37 => 1,
38 => 1,
39 => 1,
40 => 1,
41 => 1,
42 => 1,
];
switch ($testFile) {
case 'ClassFileNameUnitTest.inc':
return [
12 => 1,
13 => 1,
14 => 1,
15 => 1,
16 => 1,
17 => 1,
18 => 1,
19 => 1,
20 => 1,
21 => 1,
22 => 1,
23 => 1,
27 => 1,
28 => 1,
29 => 1,
30 => 1,
31 => 1,
32 => 1,
33 => 1,
34 => 1,
35 => 1,
36 => 1,
37 => 1,
38 => 1,
39 => 1,
40 => 1,
41 => 1,
42 => 1,
];

case 'ClassFileNameLiveCodingFailUnitTest.inc':
return [
6 => 1,
];

case 'ClassFileName Spaces In FilenameUnitTest.inc':
return [
7 => 1,
];

case 'ClassFileName-Dashes-In-FilenameUnitTest.inc':
return [
7 => 1,
];

default:
return [];
}//end switch

}//end getErrorList()

Expand Down