clean install

This commit is contained in:
matze committed 2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 files changed
+3555714

No files matched your search

@@ -0,0 +1,76 @@
<?php
/**
* \DrupalPractice\Sniffs\General\AccessAdminPagesSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\General;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionDefinition;
/**
* Throws a warning if the "access administration pages" string is found in
* hook_menu().
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class AccessAdminPagesSniff extends FunctionDefinition
{
/**
* Process this function definition.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function
* name in the stack.
* @param int $functionPtr The position of the function
* keyword in the stack.
*
* @return void
*/
public function processFunction(File $phpcsFile, $stackPtr, $functionPtr)
{
$fileExtension = strtolower(substr($phpcsFile->getFilename(), -6));
// Only check in *.module files.
if ($fileExtension !== 'module') {
return;
}
$tokens = $phpcsFile->getTokens();
$fileName = substr(basename($phpcsFile->getFilename()), 0, -7);
if ($tokens[$stackPtr]['content'] !== ($fileName.'_menu')) {
return;
}
// Search in the function body for "access administration pages" strings.
$string = $phpcsFile->findNext(
T_CONSTANT_ENCAPSED_STRING,
$tokens[$functionPtr]['scope_opener'],
$tokens[$functionPtr]['scope_closer']
);
while ($string !== false) {
if (substr($tokens[$string]['content'], 1, -1) === 'access administration pages') {
$warning = 'The administration menu callback should probably use "administer site configuration" - which implies the user can change something - rather than "access administration pages" which is about viewing but not changing configurations.';
$phpcsFile->addWarning($warning, $string, 'PermissionFound');
}
$string = $phpcsFile->findNext(
T_CONSTANT_ENCAPSED_STRING,
($string + 1),
$tokens[$functionPtr]['scope_closer']
);
}//end while
}//end processFunction()
}//end class
@@ -0,0 +1,96 @@
<?php
/**
* \DrupalPractice\Sniffs\General\ClassNameSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\General;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use DrupalPractice\Project;
/**
* Checks that classes without namespaces are properly prefixed with the module
* name.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class ClassNameSniff 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 function
* name in the stack.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
// If there is a PHP 5.3 namespace declaration in the file we return
// immediately as classes can be named arbitrary within a namespace.
$namespace = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1));
if ($namespace !== false) {
return;
}
$moduleName = Project::getName($phpcsFile);
if ($moduleName === false) {
return;
}
$tokens = $phpcsFile->getTokens();
$className = $phpcsFile->findNext(T_STRING, $stackPtr);
$name = trim($tokens[$className]['content']);
// Underscores are omitted in class names. Also convert all characters
// to lower case to compare them later.
$classPrefix = strtolower(str_replace('_', '', $moduleName));
// Views classes might have underscores in the name, which is also fine.
$viewsPrefix = strtolower($moduleName);
$name = strtolower($name);
if (strpos($name, $classPrefix) !== 0 && strpos($name, $viewsPrefix) !== 0) {
$warning = '%s name must be prefixed with the project name "%s"';
$nameParts = explode('_', $moduleName);
$camelName = '';
foreach ($nameParts as &$part) {
$camelName .= ucfirst($part);
}
$errorData = [
ucfirst($tokens[$stackPtr]['content']),
$camelName,
];
$phpcsFile->addWarning($warning, $className, 'ClassPrefix', $errorData);
}
}//end process()
}//end class
@@ -0,0 +1,78 @@
<?php
/**
* \DrupalPractice\Sniffs\General\DescriptionTSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\General;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Checks that string values for #description in render arrays are translated.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class DescriptionTSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [T_CONSTANT_ENCAPSED_STRING];
}//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 function
* name in the stack.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
// Look for the string "#description".
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] !== '"#description"' && $tokens[$stackPtr]['content'] !== "'#description'") {
return;
}
// Look for an array pattern that starts to define #description values.
$statementEnd = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
$arrayString = $phpcsFile->getTokensAsString(($stackPtr + 1), ($statementEnd - $stackPtr));
// Cut out all the white space.
$arrayString = preg_replace('/\s+/', '', $arrayString);
if (strpos($arrayString, '=>"') !== 0 && strpos($arrayString, ']="') !== 0
&& strpos($arrayString, "=>'") !== 0 && strpos($arrayString, "]='") !== 0
) {
return;
}
$stringToken = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($stackPtr + 1));
$content = strip_tags($tokens[$stringToken]['content']);
if (strlen($content) > 5) {
$warning = '#description values usually have to run through t() for translation';
$phpcsFile->addWarning($warning, $stringToken, 'DescriptionT');
}
}//end process()
}//end class
@@ -0,0 +1,71 @@
<?php
/**
* \DrupalPractice\Sniffs\General\ExceptionTSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\General;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Checks that exceptions aren't translated.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class ExceptionTSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [T_THROW];
}//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 function
* name in the stack.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$endPtr = $phpcsFile->findEndOfStatement($stackPtr);
$newPtr = $phpcsFile->findNext(T_NEW, ($stackPtr + 1), $endPtr);
if ($newPtr !== false) {
$openPtr = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($newPtr + 1), $endPtr);
if ($openPtr !== false) {
for ($i = ($openPtr + 1); $i < $tokens[$openPtr]['parenthesis_closer']; $i++) {
if ($tokens[$i]['code'] === T_STRING && $tokens[$i]['content'] === 't') {
$warning = 'Exceptions should not be translated';
$phpcsFile->addWarning($warning, $stackPtr, 'ExceptionT');
return;
}
}
}
}
}//end process()
}//end class
@@ -0,0 +1,60 @@
<?php
/**
* \DrupalPractice\Sniffs\General\FormStateInputSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\General;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Throws a message whenever $form_state['input'] is used. $form_state['values']
* is preferred.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class FormStateInputSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [T_VARIABLE];
}//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 function
* name in the stack.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
if ($phpcsFile->getTokensAsString($stackPtr, 4) === '$form_state[\'input\']'
|| $phpcsFile->getTokensAsString($stackPtr, 4) === '$form_state["input"]'
) {
$warning = 'Do not use the raw $form_state[\'input\'], use $form_state[\'values\'] instead where possible';
$phpcsFile->addWarning($warning, $stackPtr, 'Input');
}
}//end process()
}//end class
@@ -0,0 +1,61 @@
<?php
/**
* \DrupalPractice\Sniffs\General\LanguageNoneSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\General;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Checks that ['und'] is not used, should be LANGUAGE_NONE.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class LanguageNoneSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [
T_OPEN_SQUARE_BRACKET,
T_OPEN_SHORT_ARRAY,
];
}//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 function
* name in the stack.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$sequence = $phpcsFile->getTokensAsString($stackPtr, 3);
if ($sequence === "['und']" || $sequence === '["und"]') {
$warning = "Are you accessing field values here? Then you should use LANGUAGE_NONE instead of 'und'";
$phpcsFile->addWarning($warning, ($stackPtr + 1), 'Und');
}
}//end process()
}//end class
@@ -0,0 +1,141 @@
<?php
/**
* \DrupalPractice\Sniffs\General\OptionsTSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\General;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Checks that values in #options form arrays are translated.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class OptionsTSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [T_CONSTANT_ENCAPSED_STRING];
}//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 function
* name in the stack.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
// Look for the string "#options".
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] !== '"#options"' && $tokens[$stackPtr]['content'] !== "'#options'") {
return;
}
// Look for an opening array pattern that starts to define #options
// values.
$statementEnd = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1));
$arrayString = $phpcsFile->getTokensAsString(($stackPtr + 1), ($statementEnd - $stackPtr));
// Cut out all the white space.
$arrayString = preg_replace('/\s+/', '', $arrayString);
if (strpos($arrayString, '=>array(') !== 0
&& strpos($arrayString, ']=array(') !== 0
&& strpos($arrayString, '=>[') !== 0
&& strpos($arrayString, ']=[') !== 0
) {
return;
}
// We only search within the #options array.
$arrayToken = $phpcsFile->findNext([T_ARRAY, T_OPEN_SHORT_ARRAY], ($stackPtr + 1));
$nestedParenthesis = [];
if (isset($tokens[$arrayToken]['nested_parenthesis']) === true) {
$nestedParenthesis = $tokens[$arrayToken]['nested_parenthesis'];
}
if ($tokens[$arrayToken]['code'] === T_ARRAY) {
$statementEnd = $tokens[$arrayToken]['parenthesis_closer'];
$nestedParenthesis[$tokens[$arrayToken]['parenthesis_opener']] = $tokens[$arrayToken]['parenthesis_closer'];
} else {
$statementEnd = $tokens[$arrayToken]['bracket_closer'];
}
// We want to find if the element "#options" belongs to a form element.
// Array with selectable options for a form element.
$formElements = [
"'checkboxes'",
"'radios'",
"'select'",
"'tableselect'",
];
// Find beginning of the array containing "#options" element.
$startArray = $phpcsFile->findStartOfStatement($stackPtr, [T_DOUBLE_ARROW, T_OPEN_SHORT_ARRAY, T_OPEN_PARENTHESIS, T_COMMA]);
// Find next element on array of "#type".
$findType = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($startArray + 1), $statementEnd, false, "'#type'");
// Stop checking the array if its #type cannot be determined.
if ($findType === false) {
return;
}
// Get the value of "#type".
$valueType = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($findType + 1), null, false);
// Go through the array by examining stuff after "=>".
$arrow = $phpcsFile->findNext(T_DOUBLE_ARROW, ($arrayToken + 1), $statementEnd, false, null, true);
while ($arrow !== false) {
$arrayValue = $phpcsFile->findNext(T_WHITESPACE, ($arrow + 1), $statementEnd, true);
$valueNestedParenthesis = [];
if (isset($tokens[$arrayValue]['nested_parenthesis']) === true) {
$valueNestedParenthesis = $tokens[$arrayValue]['nested_parenthesis'];
}
// We are only interested in string literals that are not numbers
// and more than 3 characters long.
if ($tokens[$arrayValue]['code'] === T_CONSTANT_ENCAPSED_STRING
&& is_numeric(substr($tokens[$arrayValue]['content'], 1, -1)) === false
&& strlen($tokens[$arrayValue]['content']) > 5 && in_array($tokens[$valueType]['content'], $formElements) === true
// Make sure that we don't check stuff in nested arrays within
// t() for example.
&& $valueNestedParenthesis === $nestedParenthesis
) {
// We need to make sure that the string is the one and only part
// of the array value.
$afterValue = $phpcsFile->findNext(T_WHITESPACE, ($arrayValue + 1), $statementEnd, true);
if ($tokens[$afterValue]['code'] === T_COMMA || $tokens[$afterValue]['code'] === T_CLOSE_PARENTHESIS) {
$warning = '#options values usually have to run through t() for translation';
// cspell:ignore TforValue
$phpcsFile->addWarning($warning, $arrayValue, 'TforValue');
}
}
$arrow = $phpcsFile->findNext(T_DOUBLE_ARROW, ($arrow + 1), $statementEnd, false, null, true);
}//end while
}//end process()
}//end class
@@ -0,0 +1,101 @@
<?php
/**
* \DrupalPractice\Sniffs\General\VariableNameSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\General;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionCall;
use DrupalPractice\Project;
use PHP_CodeSniffer\Util\Tokens;
/**
* Checks the usage of variable_get() in forms and the variable name.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class VariableNameSniff extends FunctionCall
{
/**
* Returns an array of function names this test wants to listen for.
*
* @return array<string>
*/
public function registerFunctionNames()
{
return ['variable_get'];
}//end registerFunctionNames()
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
* @param int $openBracket The position of the opening
* parenthesis in the stack.
* @param int $closeBracket The position of the closing
* parenthesis in the stack.
*
* @return void
*/
public function processFunctionCall(
File $phpcsFile,
$stackPtr,
$openBracket,
$closeBracket
) {
$tokens = $phpcsFile->getTokens();
// We assume that the sequence '#default_value' => variable_get(...)
// indicates a variable that the module owns.
$arrow = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if ($arrow === false || $tokens[$arrow]['code'] !== T_DOUBLE_ARROW) {
return;
}
$arrayKey = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($arrow - 1), null, true);
if ($arrayKey === false
|| $tokens[$arrayKey]['code'] !== T_CONSTANT_ENCAPSED_STRING
|| substr($tokens[$arrayKey]['content'], 1, -1) !== '#default_value'
) {
return;
}
$argument = $this->getArgument(1);
// Variable name is not a literal string, so we return early.
if ($argument === false || $tokens[$argument['start']]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
return;
}
$moduleName = Project::getName($phpcsFile);
if ($moduleName === false) {
return;
}
$variableName = substr($tokens[$argument['start']]['content'], 1, -1);
if (strpos($variableName, $moduleName) !== 0) {
$warning = 'All variables defined by your module must be prefixed with your module\'s name to avoid name collisions with others. Expected start with "%s" but found "%s"';
$data = [
$moduleName,
$variableName,
];
$phpcsFile->addWarning($warning, $argument['start'], 'VariableName', $data);
}
}//end processFunctionCall()
}//end class