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,102 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionDefinitions\AccessHookMenuSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionDefinitions;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionDefinition;
use PHP_CodeSniffer\Util\Tokens;
/**
* Checks that there are no undocumented open access callbacks in hook_menu().
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class AccessHookMenuSniff 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;
}
$fileName = substr(basename($phpcsFile->getFilename()), 0, -7);
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] !== ($fileName.'_menu')) {
return;
}
// Search for 'access callback' => TRUE in the function body.
$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 callback') {
$arrayOperator = $phpcsFile->findNext(
Tokens::$emptyTokens,
($string + 1),
null,
true
);
if ($arrayOperator !== false
&& $tokens[$arrayOperator]['code'] === T_DOUBLE_ARROW
) {
$callback = $phpcsFile->findNext(
Tokens::$emptyTokens,
($arrayOperator + 1),
null,
true
);
if ($callback !== false && $tokens[$callback]['code'] === T_TRUE) {
// Check if there is a comment before the line that might
// explain stuff.
$commentBefore = $phpcsFile->findPrevious(
T_WHITESPACE,
($string - 1),
$tokens[$functionPtr]['scope_opener'],
true
);
if ($commentBefore !== false && in_array($tokens[$commentBefore]['code'], Tokens::$commentTokens) === false) {
$warning = 'Open page callback found, please add a comment before the line why there is no access restriction';
$phpcsFile->addWarning($warning, $callback, 'OpenCallback');
}
}
}//end if
}//end if
$string = $phpcsFile->findNext(
T_CONSTANT_ENCAPSED_STRING,
($string + 1),
$tokens[$functionPtr]['scope_closer']
);
}//end while
}//end processFunction()
}//end class

View File

@@ -0,0 +1,81 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionDefinitions\FormAlterDocSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionDefinitions;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionDefinition;
use DrupalPractice\Project;
/**
* Checks that the comment "Implements hook_form_alter()." actually matches the
* function signature.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class FormAlterDocSniff 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)
{
$tokens = $phpcsFile->getTokens();
$docCommentEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($functionPtr - 1), null, true);
// If there is no doc comment there is nothing we can check.
if ($docCommentEnd === false || $tokens[$docCommentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
return;
}
$commentLine = ($docCommentEnd - 1);
$commentFound = false;
while ($tokens[$commentLine]['code'] !== T_DOC_COMMENT_OPEN_TAG) {
if (strpos($tokens[$commentLine]['content'], 'Implements hook_form_alter().') === 0) {
$commentFound = true;
break;
}
$commentLine--;
}
if ($commentFound === false) {
return;
}
$projectName = Project::getName($phpcsFile);
if ($projectName === false) {
return;
}
if ($tokens[$stackPtr]['content'] !== $projectName.'_form_alter') {
$warning = 'Doc comment indicates hook_form_alter() but function signature is "%s" instead of "%s". Did you mean hook_form_FORM_ID_alter()?';
$data = [
$tokens[$stackPtr]['content'],
$projectName.'_form_alter',
];
$phpcsFile->addWarning($warning, $commentLine, 'Different', $data);
}
}//end processFunction()
}//end class

View File

@@ -0,0 +1,95 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionDefinitions\HookInitCssSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionDefinitions;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionDefinition;
use DrupalPractice\Project;
use PHP_CodeSniffer\Util\Tokens;
/**
* Checks that drupal_add_css() is not used in hook_init().
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class HookInitCssSniff 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|int
*/
public function processFunction(File $phpcsFile, $stackPtr, $functionPtr)
{
$fileExtension = strtolower(substr($phpcsFile->getFilename(), -6));
// Only check in *.module files.
if ($fileExtension !== 'module') {
return ($phpcsFile->numTokens + 1);
}
// This check only applies to Drupal 7, not Drupal 6.
if (Project::getCoreVersion($phpcsFile) !== 7) {
return ($phpcsFile->numTokens + 1);
}
$fileName = substr(basename($phpcsFile->getFilename()), 0, -7);
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] !== ($fileName.'_init') && $tokens[$stackPtr]['content'] !== ($fileName.'_page_build')) {
return;
}
// Search in the function body for drupal_add_css() calls.
$string = $phpcsFile->findNext(
T_STRING,
$tokens[$functionPtr]['scope_opener'],
$tokens[$functionPtr]['scope_closer']
);
while ($string !== false) {
if ($tokens[$string]['content'] === 'drupal_add_css' || $tokens[$string]['content'] === 'drupal_add_js') {
$opener = $phpcsFile->findNext(
Tokens::$emptyTokens,
($string + 1),
null,
true
);
if ($opener !== false
&& $tokens[$opener]['code'] === T_OPEN_PARENTHESIS
) {
if ($tokens[$stackPtr]['content'] === ($fileName.'_init')) {
$warning = 'Do not use %s() in hook_init(), use #attached for CSS and JS in your page/form callback or in hook_page_build() instead';
$phpcsFile->addWarning($warning, $string, 'AddFunctionFound', [$tokens[$string]['content']]);
} else {
$warning = 'Do not use %s() in hook_page_build(), use #attached for CSS and JS on the $page render array instead';
$phpcsFile->addWarning($warning, $string, 'AddFunctionFoundPageBuild', [$tokens[$string]['content']]);
}
}
}
$string = $phpcsFile->findNext(
T_STRING,
($string + 1),
$tokens[$functionPtr]['scope_closer']
);
}//end while
}//end processFunction()
}//end class

View File

@@ -0,0 +1,92 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionDefinitions\InstallTSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionDefinitions;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionDefinition;
use DrupalPractice\Project;
use PHP_CodeSniffer\Util\Tokens;
/**
* Checks that t() and st() are not used in hook_install() and hook_requirements().
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class InstallTSniff 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|int
*/
public function processFunction(File $phpcsFile, $stackPtr, $functionPtr)
{
$fileExtension = strtolower(substr($phpcsFile->getFilename(), -7));
// Only check in *.install files.
if ($fileExtension !== 'install') {
return ($phpcsFile->numTokens + 1);
}
$fileName = substr(basename($phpcsFile->getFilename()), 0, -8);
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] !== ($fileName.'_install')
&& $tokens[$stackPtr]['content'] !== ($fileName.'_requirements')
) {
return;
}
// This check only applies to Drupal 7, not Drupal 8.
if (Project::getCoreVersion($phpcsFile) !== 7) {
return ($phpcsFile->numTokens + 1);
}
// Search in the function body for t() calls.
$string = $phpcsFile->findNext(
T_STRING,
$tokens[$functionPtr]['scope_opener'],
$tokens[$functionPtr]['scope_closer']
);
while ($string !== false) {
if ($tokens[$string]['content'] === 't' || $tokens[$string]['content'] === 'st') {
$opener = $phpcsFile->findNext(
Tokens::$emptyTokens,
($string + 1),
null,
true
);
if ($opener !== false
&& $tokens[$opener]['code'] === T_OPEN_PARENTHESIS
) {
$error = 'Do not use t() or st() in installation phase hooks, use $t = get_t() to retrieve the appropriate localization function name';
$phpcsFile->addError($error, $string, 'TranslationFound');
}
}
$string = $phpcsFile->findNext(
T_STRING,
($string + 1),
$tokens[$functionPtr]['scope_closer']
);
}//end while
}//end processFunction()
}//end class