clean install

This commit is contained in:
2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 changed files with 3555714 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
<?php
/**
* \Drupal\Sniffs\NamingConventions\ValidClassNameSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace Drupal\Sniffs\NamingConventions;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* \Drupal\Sniffs\NamingConventions\ValidClassNameSniff.
*
* Ensures class and interface names start with a capital letter
* and do not use _ separators.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class ValidClassNameSniff 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 current file being processed.
* @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();
$className = $phpcsFile->findNext(T_STRING, $stackPtr);
$name = trim($tokens[$className]['content']);
$errorData = [ucfirst($tokens[$stackPtr]['content'])];
// Make sure the first letter is a capital.
if (preg_match('|^[A-Z]|', $name) === 0) {
$error = '%s name must begin with a capital letter';
$phpcsFile->addError($error, $stackPtr, 'StartWithCapital', $errorData);
}
// Search for underscores.
if (strpos($name, '_') !== false) {
$error = '%s name must use UpperCamel naming without underscores';
$phpcsFile->addError($error, $stackPtr, 'NoUnderscores', $errorData);
}
}//end process()
}//end class

View File

@@ -0,0 +1,158 @@
<?php
/**
* \Drupal\Sniffs\NamingConventions\ValidFunctionNameSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace Drupal\Sniffs\NamingConventions;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff;
use PHP_CodeSniffer\Util\Common;
/**
* \Drupal\Sniffs\NamingConventions\ValidFunctionNameSniff.
*
* Extends
* \PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\CamelCapsFunctionNameSniff
* to also check global function names outside the scope of classes and to not
* allow methods beginning with an underscore.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class ValidFunctionNameSniff extends CamelCapsFunctionNameSniff
{
/**
* A list of function prefixes which may not respect naming convention.
*
* @var string[]
*/
protected $allowedFunctionPrefixes = [
'template_preprocess',
'theme',
];
/**
* Processes the tokens within the scope.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
* @param int $currScope The position of the current scope.
*
* @return void
*/
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === null) {
// Ignore closures.
return;
}
$className = $phpcsFile->getDeclarationName($currScope);
$errorData = [$className.'::'.$methodName];
// Is this a magic method. i.e., is prefixed with "__" ?
if (preg_match('|^__|', $methodName) !== 0) {
$magicPart = strtolower(substr($methodName, 2));
if (isset($this->magicMethods[$magicPart]) === false
&& isset($this->methodsDoubleUnderscore[$magicPart]) === false
) {
$error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
}
return;
}
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
if (Common::isCamelCaps($methodName, false, true, $this->strict) === false) {
if ($methodProps['scope_specified'] === true) {
$error = '%s method name "%s" is not in lowerCamel format';
$data = [
ucfirst($methodProps['scope']),
$errorData[0],
];
$phpcsFile->addError($error, $stackPtr, 'ScopeNotCamelCaps', $data);
} else {
$error = 'Method name "%s" is not in lowerCamel format';
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
}
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no');
return;
} else {
$phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes');
}
}//end processTokenWithinScope()
/**
* Processes the tokens outside the scope.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being processed.
* @param int $stackPtr The position where this token was
* found.
*
* @return void
*/
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
{
$functionName = $phpcsFile->getDeclarationName($stackPtr);
if ($functionName === null) {
// Ignore closures.
return;
}
$isApiFile = substr($phpcsFile->getFilename(), -8) === '.api.php';
$isHookExample = substr($functionName, 0, 5) === 'hook_';
if ($isApiFile === true && $isHookExample === true) {
// Ignore for example hook_ENTITY_TYPE_insert() functions in .api.php
// files.
return;
}
if ($functionName !== strtolower($functionName)) {
$expected = strtolower(preg_replace('/([^_])([A-Z])/', '$1_$2', $functionName));
$error = 'Invalid function name, expected %s but found %s';
$data = [
$expected,
$functionName,
];
$phpcsFile->addError($error, $stackPtr, 'InvalidName', $data);
}
// Validate function names only in *.module files.
$isModuleFile = substr($phpcsFile->getFilename(), -7) === '.module';
if ($isModuleFile === true) {
// Check if the function prefix is allowed to not respect standard.
foreach ($this->allowedFunctionPrefixes as $allowedFunctionPrefix) {
if (substr($functionName, 0, strlen($allowedFunctionPrefix)) === $allowedFunctionPrefix) {
return;
}
}
$moduleName = substr(basename($phpcsFile->getFilename()), 0, -7);
if (preg_match("/^_?$moduleName\_.+/", $functionName) === 0) {
$error = 'All functions defined in a module file must be prefixed with the module\'s name, found "%s" but expected "%s"';
$data = [
$functionName,
$moduleName.'_'.$functionName,
];
$phpcsFile->addError($error, $stackPtr, 'InvalidPrefix', $data);
}
}
}//end processTokenOutsideScope()
}//end class

View File

@@ -0,0 +1,125 @@
<?php
/**
* \Drupal\Sniffs\NamingConventions\ValidGlobalSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace Drupal\Sniffs\NamingConventions;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
/**
* Ensures that global variables start with an underscore.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class ValidGlobalSniff implements Sniff
{
/**
* List of allowed Drupal core global variable names.
*
* @var array<string>
*/
public $coreGlobals = [
'$argc',
'$argv',
'$base_insecure_url',
'$base_path',
'$base_root',
'$base_secure_url',
'$base_theme_info',
'$base_url',
'$channel',
'$conf',
'$config',
'$config_directories',
'$cookie_domain',
'$databases',
'$db_prefix',
'$db_type',
'$db_url',
'$drupal_hash_salt',
'$drupal_test_info',
'$element',
'$forum_topic_list_header',
'$image',
'$install_state',
'$installed_profile',
'$is_https',
'$is_https_mock',
'$item',
'$items',
'$language',
'$language_content',
'$language_url',
'$locks',
'$menu_admin',
'$multibyte',
'$pager_limits',
'$pager_page_array',
'$pager_total',
'$pager_total_items',
'$tag',
'$theme',
'$theme_engine',
'$theme_info',
'$theme_key',
'$theme_path',
'$timers',
'$update_free_access',
'$update_rewrite_settings',
'$user',
];
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [T_GLOBAL];
}//end register()
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being processed.
* @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();
$varToken = $stackPtr;
// Find variable names until we hit a semicolon.
$ignore = Tokens::$emptyTokens;
$ignore[] = T_SEMICOLON;
while (($varToken = $phpcsFile->findNext($ignore, ($varToken + 1), null, true, null, true)) !== false) {
if ($tokens[$varToken]['code'] === T_VARIABLE
&& in_array($tokens[$varToken]['content'], $this->coreGlobals) === false
&& $tokens[$varToken]['content'][1] !== '_'
) {
$error = 'global variables should start with a single underscore followed by the module and another underscore';
$phpcsFile->addError($error, $varToken, 'GlobalUnderScore');
}
}
}//end process()
}//end class

View File

@@ -0,0 +1,156 @@
<?php
/**
* \Drupal\Sniffs\NamingConventions\ValidVariableNameSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace Drupal\Sniffs\NamingConventions;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
/**
* \Drupal\Sniffs\NamingConventions\ValidVariableNameSniff.
*
* Checks the naming of member variables.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class ValidVariableNameSniff extends AbstractVariableSniff
{
/**
* Processes class member variables.
*
* @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
*/
protected function processMemberVar(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$memberProps = $phpcsFile->getMemberProperties($stackPtr);
if (empty($memberProps) === true) {
return;
}
// Check if the class extends another class and get the name of the class
// that is extended.
if (empty($tokens[$stackPtr]['conditions']) === false) {
$classPtr = key($tokens[$stackPtr]['conditions']);
$extendsName = $phpcsFile->findExtendedClassName($classPtr);
// Special case config entities: those are allowed to have underscores in
// their class property names. If a class extends something like
// ConfigEntityBase then we consider it a config entity class and allow
// underscores.
if ($extendsName !== false && strpos($extendsName, 'ConfigEntity') !== false) {
return;
}
// Plugin annotations may have underscores in class properties.
// For example, see \Drupal\Core\Field\Annotation\FieldFormatter.
// The only class named "Plugin" in Drupal core is
// \Drupal\Component\Annotation\Plugin while many Views plugins
// extend \Drupal\views\Annotation\ViewsPluginAnnotationBase.
if ($extendsName !== false && in_array(
$extendsName,
[
'Plugin',
'ViewsPluginAnnotationBase',
]
) !== false
) {
return;
}
$implementsNames = $phpcsFile->findImplementedInterfaceNames($classPtr);
if ($implementsNames !== false && in_array('AnnotationInterface', $implementsNames) !== false) {
return;
}
}//end if
// The name of a property must start with a lowercase letter, properties
// with underscores are not allowed, except the cases handled above.
$memberName = ltrim($tokens[$stackPtr]['content'], '$');
if (preg_match('/^[a-z]/', $memberName) === 1 && strpos($memberName, '_') === false) {
return;
}
$error = 'Class property %s should use lowerCamel naming without underscores';
$data = [$tokens[$stackPtr]['content']];
$phpcsFile->addError($error, $stackPtr, 'LowerCamelName', $data);
}//end processMemberVar()
/**
* Processes normal variables.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
*
* @return void
*/
protected function processVariable(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$varName = ltrim($tokens[$stackPtr]['content'], '$');
$phpReservedVars = [
'_SERVER',
'_GET',
'_POST',
'_REQUEST',
'_SESSION',
'_ENV',
'_COOKIE',
'_FILES',
'GLOBALS',
];
// If it's a php reserved var, then its ok.
if (in_array($varName, $phpReservedVars) === true) {
return;
}
// If it is a static public variable of a class, then its ok.
if ($tokens[($stackPtr - 1)]['code'] === T_DOUBLE_COLON) {
return;
}
if (preg_match('/^[A-Z]/', $varName) === 1) {
$error = "Variable \"$varName\" starts with a capital letter, but only \$lowerCamelCase or \$snake_case is allowed";
$phpcsFile->addError($error, $stackPtr, 'LowerStart');
}
}//end processVariable()
/**
* Processes variables in double quoted strings.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
*
* @return void
*/
protected function processVariableInString(File $phpcsFile, $stackPtr)
{
// We don't care about variables in strings.
return;
}//end processVariableInString()
}//end class