mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-07-25 08:34:29 +00:00
PHPCI
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* PHP Version 5
|
||||
*
|
||||
* FHComplete
|
||||
*/
|
||||
|
||||
/**
|
||||
* Disallow short open tags
|
||||
*
|
||||
* But permit short-open echo tags (<?=) [T_OPEN_TAG_WITH_ECHO] as they are part of PHP 5.4+
|
||||
*
|
||||
*/
|
||||
class FHComplete_Sniffs_PHP_DisallowShortOpenTagSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* If short open tags are NOT enabled, <? is not considered a T_OPEN_TAG
|
||||
* So include T_INLINE_HTML which is what "<?" is detected as
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(
|
||||
T_OPEN_TAG,
|
||||
T_INLINE_HTML
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
||||
* @param integer $stackPtr The position of the current token in the
|
||||
* stack passed in $tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
$openTag = $tokens[$stackPtr];
|
||||
|
||||
if (trim($openTag['content']) === '<?') {
|
||||
$error = 'Short PHP opening tag used; expected "<?php" but found "%s"';
|
||||
$data = array(trim($openTag['content']));
|
||||
$phpcsFile->addError($error, $stackPtr, 'Found', $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
/**
|
||||
* FHComplete
|
||||
*/
|
||||
|
||||
/**
|
||||
* Asserts that type casts are in the short form:
|
||||
* - bool instead of boolean
|
||||
* - int instead of integer
|
||||
*/
|
||||
class FHComplete_Sniffs_PHP_TypeCastingSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* Note, that this sniff only checks the value and casing of a cast.
|
||||
* It does not check for whitespace issues regarding casts, as
|
||||
* - Squiz.WhiteSpace.CastSpacing.ContainsWhiteSpace checks for whitespace in the cast
|
||||
* - Generic.Formatting.NoSpaceAfterCast.SpaceFound checks for whitespace after the cast
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array_merge(PHP_CodeSniffer_Tokens::$castTokens, array(T_BOOLEAN_NOT));
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes this test, when one of its tokens is encountered.
|
||||
*
|
||||
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
||||
* @param integer $stackPtr The position of the current token in the
|
||||
* stack passed in $tokens.
|
||||
* @return void
|
||||
*/
|
||||
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
||||
{
|
||||
$tokens = $phpcsFile->getTokens();
|
||||
|
||||
// Process !! casts
|
||||
if ($tokens[$stackPtr]['code'] == T_BOOLEAN_NOT) {
|
||||
$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
|
||||
if (!$nextToken) {
|
||||
return;
|
||||
}
|
||||
if ($tokens[$nextToken]['code'] != T_BOOLEAN_NOT) {
|
||||
return;
|
||||
}
|
||||
$error = 'Usage of !! cast is not allowed. Please use (bool) to cast.';
|
||||
$phpcsFile->addError($error, $stackPtr, 'NotAllowed');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Only allow short forms if both short and long forms are possible
|
||||
$matching = array(
|
||||
'(boolean)' => '(bool)',
|
||||
'(integer)' => '(int)',
|
||||
);
|
||||
$content = $tokens[$stackPtr]['content'];
|
||||
$key = strtolower($content);
|
||||
if (isset($matching[$key])) {
|
||||
$error = 'Please use ' . $matching[$key] . ' instead of ' . $content . '.';
|
||||
$phpcsFile->addError($error, $stackPtr, 'NotAllowed');
|
||||
return;
|
||||
}
|
||||
if ($content !== $key) {
|
||||
$error = 'Please use ' . $key . ' instead of ' . $content . '.';
|
||||
$phpcsFile->addError($error, $stackPtr, 'NotAllowed');
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user