Skip to content

Commit

Permalink
Address coding standard errors
Browse files Browse the repository at this point in the history
  • Loading branch information
djoos committed Feb 13, 2018
1 parent d5298a2 commit 2387856
Show file tree
Hide file tree
Showing 22 changed files with 259 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -13,7 +13,7 @@ before_script:
- phpunit --version

script:
- ant travis -Dcomposer.path=composer
- ant test -Dcomposer.path=composer

matrix:
allow_failures:
Expand Down
8 changes: 4 additions & 4 deletions Symfony/Sniffs/Arrays/MultiLineArrayCommaSniff.php
Expand Up @@ -107,10 +107,10 @@ public function process(File $phpcsFile, $stackPtr)

if ($fix === true) {
$ptr = $phpcsFile->findPrevious(
array(T_WHITESPACE, T_COMMENT),
$closePtr-1,
$stackPtr,
true
array(T_WHITESPACE, T_COMMENT),
$closePtr-1,
$stackPtr,
true
);

$phpcsFile->fixer->addContent($ptr, ',');
Expand Down
44 changes: 33 additions & 11 deletions Symfony/Sniffs/Commenting/AnnotationsSniff.php
Expand Up @@ -29,10 +29,12 @@
class AnnotationsSniff implements Sniff
{

private static $pattern = '/^@([^\\\(]+).*$/i';
private static $_pattern = '/^@([^\\\(]+).*$/i';

/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array
*/
public function register()
{
Expand All @@ -45,11 +47,9 @@ public function register()
* Called when one of the token types that this sniff is listening for
* is found.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the
* token was found.
* @param int $stackPtr The position in the PHP_CodeSniffer
* file's token stack where the token
* was found.
* @param File $phpcsFile The file where the token was found.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void|int Optionally returns a stack pointer. The sniff will not be
* called again on the current file until the returned stack
Expand All @@ -61,13 +61,35 @@ public function process(File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();
$closer = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, $stackPtr);

if (false !== $next = $phpcsFile->findNext($this->register(), $stackPtr + 1, $closer)) {
$first = preg_replace(self::$pattern, '$1', $tokens[$stackPtr]['content']);
$second = preg_replace(self::$pattern, '$1', $tokens[$next]['content']);
if (false !== $next = $phpcsFile->findNext(
$this->register(),
$stackPtr + 1,
$closer
)
) {
$first = preg_replace(
self::$_pattern,
'$1',
$tokens[$stackPtr]['content']
);
$second = preg_replace(
self::$_pattern,
'$1',
$tokens[$next]['content']
);

$stackPtrLine = $tokens[$stackPtr]['line'];
$nextLine = $tokens[$next]['line'];

if ($first !== $second && $stackPtrLine + 2 > $nextLine) {
$error = 'Group annotations together ';
$error .= 'so that annotations of the same type ';
$error .= 'immediately follow each other, and annotations ';
$error .= 'of a different type are separated ';
$error .= 'by a single blank line';

if ($first !== $second && $tokens[$stackPtr]['line'] + 2 > $tokens[$next]['line']) {
$phpcsFile->addError(
'Group annotations together so that annotations of the same type immediately follow each other, and annotations of a different type are separated by a single blank line',
$error,
$stackPtr,
'Invalid'
);
Expand Down
12 changes: 6 additions & 6 deletions Symfony/Sniffs/Commenting/ClassCommentSniff.php
Expand Up @@ -14,7 +14,7 @@

namespace Symfony\Sniffs\Commenting;

use PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\ClassCommentSniff as PearClassCommentSniff;
use PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\ClassCommentSniff as Sniff;

/**
* Parses and verifies the doc comments for classes.
Expand All @@ -38,7 +38,7 @@
* @license http://spdx.org/licenses/MIT MIT License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class ClassCommentSniff extends PearClassCommentSniff
class ClassCommentSniff extends Sniff
{
/**
* Tags in correct order and related info.
Expand Down Expand Up @@ -101,10 +101,10 @@ class ClassCommentSniff extends PearClassCommentSniff
/**
* Processes each tag and raise an error if there are blacklisted tags.
*
* @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.
* @param int $commentStart Position in the stack where the comment started.
* @param File $phpcsFile The file where the token was found.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param int $commentStart Position in the stack where the comment started.
*
* @return void
*/
Expand Down
4 changes: 2 additions & 2 deletions Symfony/Sniffs/Commenting/FunctionCommentSniff.php
Expand Up @@ -15,7 +15,7 @@
namespace Symfony\Sniffs\Commenting;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FunctionCommentSniff as PearFunctionCommentSniff;
use PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FunctionCommentSniff as Sniff;

/**
* Symfony standard customization to PEARs FunctionCommentSniff.
Expand All @@ -35,7 +35,7 @@
* @license http://spdx.org/licenses/MIT MIT License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class FunctionCommentSniff extends PearFunctionCommentSniff
class FunctionCommentSniff extends Sniff
{
/**
* Process the return comment of this function comment.
Expand Down
19 changes: 12 additions & 7 deletions Symfony/Sniffs/Commenting/LicenseSniff.php
Expand Up @@ -31,6 +31,8 @@ class LicenseSniff implements Sniff

/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array
*/
public function register()
{
Expand All @@ -43,11 +45,9 @@ public function register()
* Called when one of the token types that this sniff is listening for
* is found.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the
* token was found.
* @param int $stackPtr The position in the PHP_CodeSniffer
* file's token stack where the token
* was found.
* @param File $phpcsFile The file where the token was found.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void|int Optionally returns a stack pointer. The sniff will not be
* called again on the current file until the returned stack
Expand All @@ -56,9 +56,14 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
if (false === $phpcsFile->findPrevious([T_COMMENT, T_DOC_COMMENT_OPEN_TAG], $stackPtr)) {
if (false === $phpcsFile->findPrevious(
[T_COMMENT, T_DOC_COMMENT_OPEN_TAG],
$stackPtr
)
) {
$phpcsFile->addWarning(
'The license block has to be present at the top of every PHP file, before the namespace',
'The license block has to be present at the top
of every PHP file, before the namespace',
$stackPtr,
'Warning'
);
Expand Down
40 changes: 28 additions & 12 deletions Symfony/Sniffs/Commenting/TypeHintingSniff.php
Expand Up @@ -28,21 +28,23 @@
*/
class TypeHintingSniff implements Sniff
{
private static $blacklist = [
private static $_blacklist = [
'boolean' => 'bool',
'integer' => 'int',
'double' => 'float',
'real' => 'float',
];

private static $casts = [
private static $_casts = [
T_BOOL_CAST,
T_INT_CAST,
T_DOUBLE_CAST,
];

/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array
*/
public function register()
{
Expand All @@ -58,11 +60,9 @@ public function register()
* Called when one of the token types that this sniff is listening for
* is found.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the
* token was found.
* @param int $stackPtr The position in the PHP_CodeSniffer
* file's token stack where the token
* was found.
* @param File $phpcsFile The file where the token was found.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void|int Optionally returns a stack pointer. The sniff will not be
* called again on the current file until the returned stack
Expand All @@ -76,13 +76,29 @@ public function process(File $phpcsFile, $stackPtr)

if ('@var' === $tag['content']) {
$type = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $stackPtr + 1);
$hint = strtolower(preg_replace('/([^\s]+)[\s]+.*/', '$1', $tokens[$type]['content']));
} elseif (in_array($tag['code'], self::$casts)) {
$hint = strtolower(preg_replace('/\(([^\s]+)\)/', '$1', $tag['content']));
$hint = strtolower(
preg_replace(
'/([^\s]+)[\s]+.*/',
'$1',
$tokens[$type]['content']
)
);
} elseif (in_array($tag['code'], self::$_casts)) {
$hint = strtolower(
preg_replace(
'/\(([^\s]+)\)/',
'$1',
$tag['content']
)
);
}

if (isset($hint) && isset(self::$blacklist[$hint])) {
$error = sprintf('For type-hinting in PHPDocs and casting, use %s instead of %s', self::$blacklist[$hint], $hint);
if (isset($hint) && isset(self::$_blacklist[$hint])) {
$error = sprintf(
'For type-hinting in PHPDocs and casting, use %s instead of %s',
self::$_blacklist[$hint],
$hint
);

$phpcsFile->addError($error, $stackPtr, 'Invalid');
}
Expand Down
16 changes: 10 additions & 6 deletions Symfony/Sniffs/ControlStructure/IdenticalComparisonSniff.php
Expand Up @@ -30,6 +30,8 @@ class IdenticalComparisonSniff implements Sniff
{
/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array
*/
public function register()
{
Expand All @@ -43,11 +45,9 @@ public function register()
* Called when one of the token types that this sniff is listening for
* is found.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the
* token was found.
* @param int $stackPtr The position in the PHP_CodeSniffer
* file's token stack where the token
* was found.
* @param File $phpcsFile The file where the token was found.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void|int Optionally returns a stack pointer. The sniff will not be
* called again on the current file until the returned stack
Expand All @@ -56,7 +56,11 @@ public function register()
*/
public function process(File $phpcsFile, $stackPtr)
{
$phpcsFile->addWarning('Always use identical comparison unless you need type juggling', $stackPtr, 'Warning');
$phpcsFile->addWarning(
'Always use identical comparison unless you need type juggling',
$stackPtr,
'Warning'
);
}

}
10 changes: 5 additions & 5 deletions Symfony/Sniffs/ControlStructure/UnaryOperatorsSniff.php
Expand Up @@ -30,6 +30,8 @@ class UnaryOperatorsSniff implements Sniff
{
/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array
*/
public function register()
{
Expand All @@ -43,11 +45,9 @@ public function register()
* Called when one of the token types that this sniff is listening for
* is found.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the
* token was found.
* @param int $stackPtr The position in the PHP_CodeSniffer
* file's token stack where the token
* was found.
* @param File $phpcsFile The file where the token was found.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void|int Optionally returns a stack pointer. The sniff will not be
* called again on the current file until the returned stack
Expand Down
23 changes: 14 additions & 9 deletions Symfony/Sniffs/ControlStructure/YodaConditionsSniff.php
Expand Up @@ -28,7 +28,7 @@
*/
class YodaConditionsSniff implements Sniff
{
private $yodas = array(
private $_yodas = array(
T_IS_EQUAL,
T_IS_IDENTICAL,
T_IS_NOT_EQUAL,
Expand All @@ -37,6 +37,8 @@ class YodaConditionsSniff implements Sniff

/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array
*/
public function register()
{
Expand All @@ -50,11 +52,9 @@ public function register()
* Called when one of the token types that this sniff is listening for
* is found.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where the
* token was found.
* @param int $stackPtr The position in the PHP_CodeSniffer
* file's token stack where the token
* was found.
* @param File $phpcsFile The file where the token was found.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void|int Optionally returns a stack pointer. The sniff will not be
* called again on the current file until the returned stack
Expand All @@ -68,14 +68,19 @@ public function process(File $phpcsFile, $stackPtr)
$closer = $tokens[$stackPtr]['parenthesis_closer'];

do {
$comparison = $phpcsFile->findNext($this->yodas, $opener + 1, $closer);
$comparison = $phpcsFile->findNext($this->_yodas, $opener + 1, $closer);

if (false === $comparison) {
break;
}

if (T_VARIABLE === $tokens[$comparison - 2]['code'] && T_VARIABLE !== $tokens[$comparison + 2]['code']) {
$error = 'Use Yoda conditions when checking a variable against an expression to avoid an accidental assignment inside the condition statement';
if (T_VARIABLE === $tokens[$comparison - 2]['code']
&& T_VARIABLE !== $tokens[$comparison + 2]['code']
) {
$error = 'Use Yoda conditions when checking a variable against ';
$error .= 'an expression to avoid an accidental assignment ';
$error .= 'inside the condition statement.';

$phpcsFile->addError($error, $stackPtr, 'Invalid');
}

Expand Down

0 comments on commit 2387856

Please sign in to comment.