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,130 @@
<?php
/**
* \Drupal\Sniffs\Files\EndFileNewlineSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace Drupal\Sniffs\Files;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Ensures the file ends with a newline character.
*
* Largely copied from PSR2, but we need to run it on *.txt files and templates as
* well.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class EndFileNewlineSniff implements Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array<string>
*/
public $supportedTokenizers = [
'PHP',
'JS',
'CSS',
];
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [
T_OPEN_TAG,
T_INLINE_HTML,
];
}//end register()
/**
* Processes this sniff, when one of its tokens is encountered.
*
* @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 int|void
*/
public function process(File $phpcsFile, $stackPtr)
{
// Skip to the end of the file.
$tokens = $phpcsFile->getTokens();
if ($phpcsFile->tokenizerType === 'PHP') {
$lastToken = ($phpcsFile->numTokens - 1);
} else {
// JS and CSS have an artificial token at the end which we have to
// ignore.
$lastToken = ($phpcsFile->numTokens - 2);
}
// Hard-coding the expected \n in this sniff as it is PSR-2 specific and
// PSR-2 enforces the use of unix style newlines.
if (substr($tokens[$lastToken]['content'], -1) !== "\n") {
$error = 'Expected 1 newline at end of file; 0 found';
$fix = $phpcsFile->addFixableError($error, $lastToken, 'NoneFound');
if ($fix === true) {
$phpcsFile->fixer->addNewline($lastToken);
}
$phpcsFile->recordMetric($stackPtr, 'Number of newlines at EOF', '0');
return ($phpcsFile->numTokens + 1);
}
// Go looking for the last non-empty line.
$lastLine = $tokens[$lastToken]['line'];
if ($tokens[$lastToken]['code'] === T_WHITESPACE) {
$lastCode = $phpcsFile->findPrevious(T_WHITESPACE, ($lastToken - 1), null, true);
} else if ($tokens[$lastToken]['code'] === T_INLINE_HTML) {
$lastCode = $lastToken;
while ($lastCode > 0 && trim($tokens[$lastCode]['content']) === '') {
$lastCode--;
}
} else {
$lastCode = $lastToken;
}
$lastCodeLine = $tokens[$lastCode]['line'];
$blankLines = (string) ($lastLine - $lastCodeLine + 1);
$phpcsFile->recordMetric($stackPtr, 'Number of newlines at EOF', $blankLines);
if ($blankLines > 1) {
$error = 'Expected 1 newline at end of file; %s found';
$data = [$blankLines];
$fix = $phpcsFile->addFixableError($error, $lastCode, 'TooMany', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($lastCode, rtrim($tokens[$lastCode]['content']));
for ($i = ($lastCode + 1); $i < $lastToken; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->replaceToken($lastToken, $phpcsFile->eolChar);
$phpcsFile->fixer->endChangeset();
}
}
// Skip the rest of the file.
return ($phpcsFile->numTokens + 1);
}//end process()
}//end class

View File

@@ -0,0 +1,88 @@
<?php
/**
* \Drupal\Sniffs\Files\FileEncodingSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace Drupal\Sniffs\Files;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* \Drupal\Sniffs\Files\FileEncodingSniff.
*
* Validates the encoding of a file against a white list of allowed encodings.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class FileEncodingSniff implements Sniff
{
/**
* List of encodings that files may be encoded with.
*
* Any other detected encodings will throw a warning.
*
* @var array<string>
*/
public $allowedEncodings = ['UTF-8'];
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [
T_INLINE_HTML,
T_OPEN_TAG,
];
}//end register()
/**
* Processes this sniff, when one of its tokens is encountered.
*
* @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 int|void
*/
public function process(File $phpcsFile, $stackPtr)
{
// Not all PHP installs have the multi byte extension - nothing we can do.
if (function_exists('mb_check_encoding') === false) {
return $phpcsFile->numTokens;
}
$fileContent = $phpcsFile->getTokensAsString(0, $phpcsFile->numTokens);
$validEncodingFound = false;
foreach ($this->allowedEncodings as $encoding) {
if (mb_check_encoding($fileContent, $encoding) === true) {
$validEncodingFound = true;
}
}
if ($validEncodingFound === false) {
$warning = 'File encoding is invalid, expected %s';
$data = [implode(' or ', $this->allowedEncodings)];
$phpcsFile->addWarning($warning, $stackPtr, 'InvalidEncoding', $data);
}
return $phpcsFile->numTokens;
}//end process()
}//end class

View File

@@ -0,0 +1,138 @@
<?php
/**
* \Drupal\Sniffs\Files\LineLengthSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace Drupal\Sniffs\Files;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff as GenericLineLengthSniff;
use PHP_CodeSniffer\Util\Tokens;
/**
* Checks comment lines in the file, and throws warnings if they are over 80
* characters in length.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class LineLengthSniff extends GenericLineLengthSniff
{
/**
* The limit that the length of a line should not exceed.
*
* @var integer
*/
public $lineLimit = 80;
/**
* The limit that the length of a line must not exceed.
* But just check the line length of comments....
*
* Set to zero (0) to disable.
*
* @var integer
*/
public $absoluteLineLimit = 0;
/**
* Checks if a line is too long.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param array<int, mixed> $tokens The token stack.
* @param int $stackPtr The first token on the next line.
*
* @return void
*/
protected function checkLineLength($phpcsFile, $tokens, $stackPtr)
{
if (isset(Tokens::$commentTokens[$tokens[($stackPtr - 1)]['code']]) === true) {
$docCommentTag = $phpcsFile->findFirstOnLine(T_DOC_COMMENT_TAG, ($stackPtr - 1));
if ($docCommentTag !== false) {
// Allow doc comment tags such as long @param tags to exceed the 80
// character limit.
return;
}
if ($tokens[($stackPtr - 1)]['code'] === T_COMMENT
// Allow @link and @see documentation to exceed the 80 character
// limit.
&& (preg_match('/^[[:space:]]*\/\/ @.+/', $tokens[($stackPtr - 1)]['content']) === 1
// Allow anything that does not contain spaces (like URLs) to be
// longer.
|| strpos(trim($tokens[($stackPtr - 1)]['content'], "/ \n"), ' ') === false)
) {
return;
}
// Code examples between @code and @endcode are allowed to exceed 80
// characters.
if (isset($tokens[$stackPtr]) === true && $tokens[$stackPtr]['code'] === T_DOC_COMMENT_WHITESPACE) {
$tag = $phpcsFile->findPrevious([T_DOC_COMMENT_TAG, T_DOC_COMMENT_OPEN_TAG], ($stackPtr - 1));
if ($tokens[$tag]['content'] === '@code') {
return;
}
}
// Drupal 8 annotations can have long translatable descriptions and we
// allow them to exceed 80 characters.
if ($tokens[($stackPtr - 2)]['code'] === T_DOC_COMMENT_STRING
&& (strpos($tokens[($stackPtr - 2)]['content'], '@Translation(') !== false
// Also allow anything without whitespace (like URLs) to exceed 80
// characters.
|| strpos($tokens[($stackPtr - 2)]['content'], ' ') === false
// Allow long "Contains ..." comments in @file doc blocks.
|| preg_match('/^Contains [a-zA-Z_\\\\.]+$/', $tokens[($stackPtr - 2)]['content']) === 1
// Allow long paths or namespaces in annotations such as
// "list_builder" = "Drupal\rules\Entity\Controller\RulesReactionListBuilder"
// cardinality = \Drupal\webform\WebformHandlerInterface::CARDINALITY_UNLIMITED.
|| preg_match('#= ("|\')?\S+[\\\\/]\S+("|\')?,*$#', $tokens[($stackPtr - 2)]['content']) === 1)
// Allow @link tags in lists.
|| strpos($tokens[($stackPtr - 2)]['content'], '- @link') !== false
// Allow hook implementation line to exceed 80 characters.
|| preg_match('/^Implements hook_[a-zA-Z0-9_]+\(\)/', $tokens[($stackPtr - 2)]['content']) === 1
) {
return;
}
parent::checkLineLength($phpcsFile, $tokens, $stackPtr);
}//end if
}//end checkLineLength()
/**
* Returns the length of a defined line.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $currentLine The current line.
*
* @return int
*/
public function getLineLength(File $phpcsFile, $currentLine)
{
$tokens = $phpcsFile->getTokens();
$tokenCount = 0;
$currentLineContent = '';
$trim = (strlen($phpcsFile->eolChar) * -1);
for (; $tokenCount < $phpcsFile->numTokens; $tokenCount++) {
if ($tokens[$tokenCount]['line'] === $currentLine) {
$currentLineContent .= $tokens[$tokenCount]['content'];
}
}
return strlen($currentLineContent);
}//end getLineLength()
}//end class

View File

@@ -0,0 +1,88 @@
<?php
/**
* \Drupal\Sniffs\Files\TxtFileLineLengthSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace Drupal\Sniffs\Files;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* \Drupal\Sniffs\Files\TxtFileLineLengthSniff.
*
* Checks all lines in a *.txt or *.md file and throws warnings if they are over 80
* characters in length.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class TxtFileLineLengthSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [T_INLINE_HTML];
}//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 current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$fileExtension = strtolower(substr($phpcsFile->getFilename(), -3));
if ($fileExtension === 'txt' || $fileExtension === '.md') {
$tokens = $phpcsFile->getTokens();
$content = rtrim($tokens[$stackPtr]['content']);
$lineLength = mb_strlen($content, 'UTF-8');
if ($lineLength > 80) {
// Often text files contain long URLs that need to be preceded
// with certain textual elements that are significant for
// preserving the formatting of the document - e.g. a long link
// in a bulleted list. If we find that the line does not contain
// any spaces after the 40th character we'll allow it.
if (preg_match('/\s+/', mb_substr($content, 40)) === 0) {
return;
}
// Lines without spaces are allowed to be longer.
// Markdown allowed to be longer for lines
// - without spaces
// - starting with #
// - starting with | (tables)
// - containing a link.
if (preg_match('/^([^ ]+$|#|\||.*\[.+\]\(.+\))/', $content) === 0) {
$data = [
80,
$lineLength,
];
$warning = 'Line exceeds %s characters; contains %s characters';
$phpcsFile->addWarning($warning, $stackPtr, 'TooLong', $data);
}
}//end if
}//end if
}//end process()
}//end class