基础代码

This commit is contained in:
2021-02-26 22:23:13 +08:00
parent 7884df52f0
commit a719feebba
2585 changed files with 328263 additions and 0 deletions
@@ -0,0 +1,147 @@
<?php
/**
* PHPCompatibility, an external standard for PHP_CodeSniffer.
*
* @package PHPCompatibility
* @copyright 2012-2019 PHPCompatibility Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCompatibility/PHPCompatibility
*/
namespace PHPCompatibility\Sniffs\Miscellaneous;
use PHPCompatibility\Sniff;
use PHP_CodeSniffer_File as File;
/**
* PHP 7.4 now supports stand-alone PHP tags at the end of a file (without new line).
*
* > `<?php` at the end of the file (without trailing newline) will now be
* > interpreted as an opening PHP tag. Previously it was interpreted either as
* > `<? php` and resulted in a syntax error (with short_open_tag=1) or was
* > interpreted as a literal `<?php` string (with short_open_tag=0).
*
* {@internal Due to an issue with the Tokenizer, this sniff will not work correctly
* on PHP 5.3 in combination with PHPCS < 2.6.0 when short_open_tag is `On`.
* As this is causing "Undefined offset" notices, there is nothing we can
* do to work-around this.}
*
* PHP version 7.4
*
* @link https://www.php.net/manual/en/migration74.incompatible.php#migration74.incompatible.core.php-tag
* @link https://github.com/php/php-src/blob/30de357fa14480468132bbc22a272aeb91789ba8/UPGRADING#L37-L40
*
* @since 9.3.0
*/
class NewPHPOpenTagEOFSniff extends Sniff
{
/**
* Whether or not short open tags is enabled on the install running the sniffs.
*
* @since 9.3.0
*
* @var bool
*/
protected $shortOpenTags;
/**
* Returns an array of tokens this test wants to listen for.
*
* @since 9.3.0
*
* @return array
*/
public function register()
{
$targets = array(
\T_OPEN_TAG_WITH_ECHO,
);
$this->shortOpenTags = (bool) ini_get('short_open_tag');
if ($this->shortOpenTags === false) {
$targets[] = \T_INLINE_HTML;
} else {
$targets[] = \T_STRING;
}
if (version_compare(\PHP_VERSION_ID, '70399', '>')) {
$targets[] = \T_OPEN_TAG;
}
return $targets;
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @since 9.3.0
*
* @param \PHP_CodeSniffer_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)
{
if ($this->supportsBelow('7.3') === false) {
return;
}
if ($stackPtr !== ($phpcsFile->numTokens - 1)) {
// We're only interested in the last token in the file.
return;
}
$tokens = $phpcsFile->getTokens();
$contents = $tokens[$stackPtr]['content'];
$error = false;
switch ($tokens[$stackPtr]['code']) {
case \T_INLINE_HTML:
// PHP < 7.4 with short open tags off.
if ($contents === '<?php') {
$error = true;
} elseif ($contents === '<?=') {
// Also cover short open echo tags in PHP 5.3 with short open tags off.
$error = true;
}
break;
case \T_STRING:
// PHP < 7.4 with short open tags on.
if ($contents === 'php'
&& $tokens[($stackPtr - 1)]['code'] === \T_OPEN_TAG
&& $tokens[($stackPtr - 1)]['content'] === '<?'
) {
$error = true;
}
break;
case \T_OPEN_TAG_WITH_ECHO:
// PHP 5.4+.
if (rtrim($contents) === '<?=') {
$error = true;
}
break;
case \T_OPEN_TAG:
// PHP 7.4+.
if ($contents === '<?php') {
$error = true;
}
break;
}
if ($error === true) {
$phpcsFile->addError(
'A PHP open tag at the end of a file, without trailing newline, was not supported in PHP 7.3 or earlier and would result in a syntax error or be interpreted as a literal string',
$stackPtr,
'Found'
);
}
}
}
@@ -0,0 +1,172 @@
<?php
/**
* PHPCompatibility, an external standard for PHP_CodeSniffer.
*
* @package PHPCompatibility
* @copyright 2012-2019 PHPCompatibility Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCompatibility/PHPCompatibility
*/
namespace PHPCompatibility\Sniffs\Miscellaneous;
use PHPCompatibility\Sniff;
use PHP_CodeSniffer_File as File;
/**
* Check for use of alternative PHP tags, support for which was removed in PHP 7.0.
*
* {@internal Based on `Generic.PHP.DisallowAlternativePHPTags` by Juliette Reinders Folmer
* (with permission) which was merged into PHPCS 2.7.0.}
*
* PHP version 7.0
*
* @link https://wiki.php.net/rfc/remove_alternative_php_tags
* @link https://www.php.net/manual/en/language.basic-syntax.phptags.php
*
* @since 7.0.4
*/
class RemovedAlternativePHPTagsSniff extends Sniff
{
/**
* Whether ASP tags are enabled or not.
*
* @since 7.0.4
*
* @var bool
*/
private $aspTags = false;
/**
* Returns an array of tokens this test wants to listen for.
*
* @since 7.0.4
*
* @return array
*/
public function register()
{
if (version_compare(\PHP_VERSION_ID, '70000', '<') === true) {
// phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.asp_tagsRemoved
$this->aspTags = (bool) ini_get('asp_tags');
}
return array(
\T_OPEN_TAG,
\T_OPEN_TAG_WITH_ECHO,
\T_INLINE_HTML,
);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @since 7.0.4
*
* @param \PHP_CodeSniffer_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)
{
if ($this->supportsAbove('7.0') === false) {
return;
}
$tokens = $phpcsFile->getTokens();
$openTag = $tokens[$stackPtr];
$content = trim($openTag['content']);
if ($content === '' || $content === '<?php') {
return;
}
if ($openTag['code'] === \T_OPEN_TAG || $openTag['code'] === \T_OPEN_TAG_WITH_ECHO) {
if ($content === '<%' || $content === '<%=') {
$data = array(
'ASP',
$content,
);
$errorCode = 'ASPOpenTagFound';
} elseif (strpos($content, '<script ') !== false) {
$data = array(
'Script',
$content,
);
$errorCode = 'ScriptOpenTagFound';
} else {
return;
}
}
// Account for incorrect script open tags.
// The "(?:<s)?" in the regex is to work-around a bug in the tokenizer in PHP 5.2.
elseif ($openTag['code'] === \T_INLINE_HTML
&& preg_match('`((?:<s)?cript (?:[^>]+)?language=[\'"]?php[\'"]?(?:[^>]+)?>)`i', $content, $match) === 1
) {
$found = $match[1];
$data = array(
'Script',
$found,
);
$errorCode = 'ScriptOpenTagFound';
}
if (isset($errorCode, $data)) {
$phpcsFile->addError(
'%s style opening tags have been removed in PHP 7.0. Found "%s"',
$stackPtr,
$errorCode,
$data
);
return;
}
// If we're still here, we can't be sure if what we found was really intended as ASP open tags.
if ($openTag['code'] === \T_INLINE_HTML && $this->aspTags === false) {
if (strpos($content, '<%') !== false) {
$error = 'Possible use of ASP style opening tags detected. ASP style opening tags have been removed in PHP 7.0. Found: %s';
$snippet = $this->getSnippet($content, '<%');
$data = array('<%' . $snippet);
$phpcsFile->addWarning($error, $stackPtr, 'MaybeASPOpenTagFound', $data);
}
}
}
/**
* Get a snippet from a HTML token.
*
* @since 7.0.4
*
* @param string $content The content of the HTML token.
* @param string $startAt Partial string to use as a starting point for the snippet.
* @param int $length The target length of the snippet to get. Defaults to 25.
*
* @return string
*/
protected function getSnippet($content, $startAt = '', $length = 25)
{
$startPos = 0;
if ($startAt !== '') {
$startPos = strpos($content, $startAt);
if ($startPos !== false) {
$startPos += \strlen($startAt);
}
}
$snippet = substr($content, $startPos, $length);
if ((\strlen($content) - $startPos) > $length) {
$snippet .= '...';
}
return $snippet;
}
}
@@ -0,0 +1,250 @@
<?php
/**
* PHPCompatibility, an external standard for PHP_CodeSniffer.
*
* @package PHPCompatibility
* @copyright 2012-2019 PHPCompatibility Contributors
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
* @link https://github.com/PHPCompatibility/PHPCompatibility
*/
namespace PHPCompatibility\Sniffs\Miscellaneous;
use PHPCompatibility\Sniff;
use PHP_CodeSniffer_File as File;
/**
* Check for valid integer types and values.
*
* Checks:
* - PHP 5.4 introduced binary integers.
* - PHP 7.0 removed tolerance for invalid octals. These were truncated prior to PHP 7
* and give a parse error since PHP 7.
* - PHP 7.0 removed support for recognizing hexadecimal numeric strings as numeric.
* Type juggling and recognition was inconsistent prior to PHP 7. As of PHP 7, they
* are no longer treated as numeric.
*
* PHP version 5.4+
*
* @link https://wiki.php.net/rfc/binnotation4ints
* @link https://wiki.php.net/rfc/remove_hex_support_in_numeric_strings
* @link https://www.php.net/manual/en/language.types.integer.php
*
* @since 7.0.3
* @since 7.0.8 This sniff now throws a warning instead of an error for invalid binary integers.
*/
class ValidIntegersSniff extends Sniff
{
/**
* Whether PHPCS is run on a PHP < 5.4.
*
* @since 7.0.3
*
* @var bool
*/
protected $isLowPHPVersion = false;
/**
* Returns an array of tokens this test wants to listen for.
*
* @since 7.0.3
*
* @return array
*/
public function register()
{
$this->isLowPHPVersion = version_compare(\PHP_VERSION_ID, '50400', '<');
return array(
\T_LNUMBER, // Binary, octal integers.
\T_CONSTANT_ENCAPSED_STRING, // Hex numeric string.
);
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @since 7.0.3
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
if ($this->couldBeBinaryInteger($tokens, $stackPtr) === true) {
if ($this->supportsBelow('5.3')) {
$error = 'Binary integer literals were not present in PHP version 5.3 or earlier. Found: %s';
if ($this->isLowPHPVersion === false) {
$data = array($token['content']);
} else {
$data = array($this->getBinaryInteger($phpcsFile, $tokens, $stackPtr));
}
$phpcsFile->addError($error, $stackPtr, 'BinaryIntegerFound', $data);
}
if ($this->isInvalidBinaryInteger($tokens, $stackPtr) === true) {
$error = 'Invalid binary integer detected. Found: %s';
$data = array($this->getBinaryInteger($phpcsFile, $tokens, $stackPtr));
$phpcsFile->addWarning($error, $stackPtr, 'InvalidBinaryIntegerFound', $data);
}
return;
}
$isError = $this->supportsAbove('7.0');
$data = array( $token['content'] );
if ($this->isInvalidOctalInteger($tokens, $stackPtr) === true) {
$this->addMessage(
$phpcsFile,
'Invalid octal integer detected. Prior to PHP 7 this would lead to a truncated number. From PHP 7 onwards this causes a parse error. Found: %s',
$stackPtr,
$isError,
'InvalidOctalIntegerFound',
$data
);
return;
}
if ($this->isHexidecimalNumericString($tokens, $stackPtr) === true) {
$this->addMessage(
$phpcsFile,
'The behaviour of hexadecimal numeric strings was inconsistent prior to PHP 7 and support has been removed in PHP 7. Found: %s',
$stackPtr,
$isError,
'HexNumericStringFound',
$data
);
return;
}
}
/**
* Could the current token potentially be a binary integer ?
*
* @since 7.0.3
*
* @param array $tokens Token stack.
* @param int $stackPtr The current position in the token stack.
*
* @return bool
*/
private function couldBeBinaryInteger($tokens, $stackPtr)
{
$token = $tokens[$stackPtr];
if ($token['code'] !== \T_LNUMBER) {
return false;
}
if ($this->isLowPHPVersion === false) {
return (preg_match('`^0b[0-1]+$`iD', $token['content']) === 1);
}
// Pre-5.4, binary strings are tokenized as T_LNUMBER (0) + T_STRING ("b01010101").
// At this point, we don't yet care whether it's a valid binary int, that's a separate check.
else {
return($token['content'] === '0' && $tokens[$stackPtr + 1]['code'] === \T_STRING && preg_match('`^b[0-9]+$`iD', $tokens[$stackPtr + 1]['content']) === 1);
}
}
/**
* Is the current token an invalid binary integer ?
*
* @since 7.0.3
*
* @param array $tokens Token stack.
* @param int $stackPtr The current position in the token stack.
*
* @return bool
*/
private function isInvalidBinaryInteger($tokens, $stackPtr)
{
if ($this->couldBeBinaryInteger($tokens, $stackPtr) === false) {
return false;
}
if ($this->isLowPHPVersion === false) {
// If it's an invalid binary int, the token will be split into two T_LNUMBER tokens.
return ($tokens[$stackPtr + 1]['code'] === \T_LNUMBER);
} else {
return (preg_match('`^b[0-1]+$`iD', $tokens[$stackPtr + 1]['content']) === 0);
}
}
/**
* Retrieve the content of the tokens which together look like a binary integer.
*
* @since 7.0.3
*
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param array $tokens Token stack.
* @param int $stackPtr The position of the current token in
* the stack.
*
* @return string
*/
private function getBinaryInteger(File $phpcsFile, $tokens, $stackPtr)
{
$length = 2; // PHP < 5.4 T_LNUMBER + T_STRING.
if ($this->isLowPHPVersion === false) {
$i = $stackPtr;
while ($tokens[$i]['code'] === \T_LNUMBER) {
$i++;
}
$length = ($i - $stackPtr);
}
return $phpcsFile->getTokensAsString($stackPtr, $length);
}
/**
* Is the current token an invalid octal integer ?
*
* @since 7.0.3
*
* @param array $tokens Token stack.
* @param int $stackPtr The current position in the token stack.
*
* @return bool
*/
private function isInvalidOctalInteger($tokens, $stackPtr)
{
$token = $tokens[$stackPtr];
if ($token['code'] === \T_LNUMBER && preg_match('`^0[0-7]*[8-9]+[0-9]*$`D', $token['content']) === 1) {
return true;
}
return false;
}
/**
* Is the current token a hexidecimal numeric string ?
*
* @since 7.0.3
*
* @param array $tokens Token stack.
* @param int $stackPtr The current position in the token stack.
*
* @return bool
*/
private function isHexidecimalNumericString($tokens, $stackPtr)
{
$token = $tokens[$stackPtr];
if ($token['code'] === \T_CONSTANT_ENCAPSED_STRING && preg_match('`^0x[a-f0-9]+$`iD', $this->stripQuotes($token['content'])) === 1) {
return true;
}
return false;
}
}