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
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ jobs:
# core is updated to that version.
run: |
cd drupal/core
../../vendor/bin/phpcs -p -s --ignore=lib/Drupal/Core/Entity/EntityType.php,lib/Drupal/Core/Recipe/RecipeInputFormTrait.php,lib/Drupal/Core/Form/FormState.php,modules/migrate/src/Plugin/Migration.php,modules/views/src/ViewExecutable.php,modules/views/src/Plugin/views/style/StylePluginBase.php
../../vendor/bin/phpcs -p -s --ignore=lib/Drupal/Core/Entity/EntityType.php,lib/Drupal/Core/Recipe/RecipeInputFormTrait.php,lib/Drupal/Core/Form/FormState.php,modules/migrate/src/Plugin/Migration.php,modules/views/src/ViewExecutable.php,modules/views/src/Plugin/views/style/StylePluginBase.php,core/lib/Drupal/Core/FileTransfer/FTP.php,core/lib/Drupal/Core/FileTransfer/SSH.php
17 changes: 12 additions & 5 deletions coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,23 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$find = Tokens::$methodPrefixes;
$find[T_WHITESPACE] = T_WHITESPACE;
$find[T_READONLY] = T_READONLY;
$tokens = $phpcsFile->getTokens();
$find = ([
T_ABSTRACT => T_ABSTRACT,
T_FINAL => T_FINAL,
T_READONLY => T_READONLY,
T_WHITESPACE => T_WHITESPACE,
] + Tokens::$phpcsCommentTokens);
$name = $tokens[$stackPtr]['content'];
$classCodeStart = $stackPtr;

$previousContent = null;
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
if (isset($find[$tokens[$commentEnd]['code']]) === true) {
if (isset(Tokens::$phpcsCommentTokens[$tokens[$commentEnd]['code']]) === true) {
$classCodeStart = $commentEnd;
}

continue;
}

Expand All @@ -78,7 +85,7 @@ public function process(File $phpcsFile, $stackPtr)
}

break;
}
}//end for

if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
&& $tokens[$commentEnd]['code'] !== T_COMMENT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* \Drupal\Sniffs\NamingConventions\ValidClassNameSniff.
*
* Ensures class and interface names start with a capital letter
* Ensures class, enum, interface and trait names start with a capital letter
* and do not use _ separators.
*
* @category PHP
Expand All @@ -35,7 +35,9 @@ public function register()
{
return [
T_CLASS,
T_ENUM,
T_INTERFACE,
T_TRAIT,
];

}//end register()
Expand All @@ -60,7 +62,7 @@ public function process(File $phpcsFile, $stackPtr)

// Make sure the first letter is a capital.
if (preg_match('|^[A-Z]|', $name) === 0) {
$error = '%s name must begin with a capital letter';
$error = '%s name must use UpperCamel naming and begin with a capital letter';
$phpcsFile->addError($error, $stackPtr, 'StartWithCapital', $errorData);
}

Expand All @@ -70,6 +72,15 @@ public function process(File $phpcsFile, $stackPtr)
$phpcsFile->addError($error, $stackPtr, 'NoUnderscores', $errorData);
}

// Ensure the name is not all uppercase.
// @todo We could make this more strict to check if there are more than
// 2 upper case characters in a row, but not decided yet.
// See https://www.drupal.org/project/coder/issues/3497433
if (strtoupper($name) === $name) {
$error = '%s name must use UpperCamel naming and not contain multiple upper case letters in a row';
$phpcsFile->addError($error, $stackPtr, 'NoUpperAcronyms', $errorData);
}

}//end process()


Expand Down
25 changes: 25 additions & 0 deletions tests/Drupal/NamingConventions/ValidClassNameUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

class CorrectClassName {}
class CorrectClassWithAReallyLongName {}
class INCORRECT_CLASS_NAME {}
class INCORRECTCLASSNAME {}
class incorrectLowercaseClassName {}

interface CorrectInterfaceName {}
interface CorrectInterfaceWithAReallyLongName {}
interface INCORRECT_INTERFACE_NAME {}
interface INCORRECTINTERFACENAME {}
interface incorrectLowercaseInterfaceName {}

trait CorrectTraitName {}
trait CorrectTraitWithAReallyLongName {}
trait INCORRECT_TRAIT_NAME {}
trait INCORRECTTRAITNAME {}
trait incorrectLowercaseTraitName {}

enum CorrectEnumName {}
enum CorrectEnumWithAReallyLongName {}
enum INCORRECT_ENUM_NAME {}
enum INCORRECTENUMNAME {}
enum incorrectLowercaseEnumName {}
58 changes: 58 additions & 0 deletions tests/Drupal/NamingConventions/ValidClassNameUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Drupal\Test\NamingConventions;

use Drupal\Test\CoderSniffUnitTest;

class ValidClassNameUnitTest 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 [
5 => 2,
6 => 1,
7 => 1,
11 => 2,
12 => 1,
13 => 1,
17 => 2,
18 => 1,
19 => 1,
23 => 2,
24 => 1,
25 => 1,
];

}//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
{
return [];

}//end getWarningList()


}//end class
5 changes: 5 additions & 0 deletions tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ enum Test: int {
case one = 1;
// Must not contain underscores.
case TWO_TEST = 2;
// Must not contain only upper case.
case THREE = 3;
// Upper case parts are allowed for now.
case FourJSONCase = 4;
case FiveAndAHorseCorrect = 5;
}
3 changes: 2 additions & 1 deletion tests/Drupal/NamingConventions/ValidEnumCaseUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ protected function getErrorList(string $testFile): array
{
return [
5 => 1,
7 => 1,
7 => 2,
9 => 1,
];

}//end getErrorList()
Expand Down
2 changes: 1 addition & 1 deletion tests/Drupal/bad/BadUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ protected function getErrorList(string $testFile): array
827 => 1,
829 => 1,
836 => 1,
838 => 1,
838 => 3,
849 => 2,
860 => 2,
867 => 1,
Expand Down
9 changes: 9 additions & 0 deletions tests/Drupal/good/good.php
Original file line number Diff line number Diff line change
Expand Up @@ -1905,3 +1905,12 @@ public function __construct(
) {}

}

/**
* Doc block is here and an ignore directive is ok.
*/
// phpcs:ignore Drupal.NamingConventions.ValidClassName
enum PUROSELY_WRONG_BUT_OK: int {
case One = 1;
case Two = 2;
}
Loading