Skip to content

Commit

Permalink
MDL-19247 Finished cleaning up Sniffer files
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasconnault committed May 21, 2009
1 parent d764d4c commit 7f2068f
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_codeanalysis_emptystatementsniff implements php_codesniffer_sniff
{
class moodle_sniffs_codeanalysis_emptystatementsniff implements php_codesniffer_sniff {

/**
* List of block tokens that this sniff covers.
Expand Down Expand Up @@ -77,10 +76,8 @@ class moodle_sniffs_codeanalysis_emptystatementsniff implements php_codesniffer_
*
* @return array(integer)
*/
public function register()
{
public function register() {
return array_keys($this->_tokens);

}


Expand All @@ -93,8 +90,7 @@ public function register()
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
$token = $tokens[$stackptr];

Expand All @@ -106,28 +102,27 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
$next = ++$token['scope_opener'];
$end = --$token['scope_closer'];

$emptyBody = true;
$emptybody = true;

for (; $next <= $end; ++$next) {

if (in_array($tokens[$next]['code'], PHP_CodeSniffer_tokens::$emptyTokens) === false) {
$emptyBody = false;
$emptybody = false;
break;
}
}

if ($emptyBody === true) {
if ($emptybody === true) {
// Get token identifier.
$name = $phpcsfile->gettokensAsString($stackptr, 1);
$error = sprintf('Empty %s statement detected', strtoupper($name));

if ($this->_tokens[$token['code']] === true) {
$phpcsfile->adderror($error, $stackptr);

} else {
$phpcsfile->addwarning($error, $stackptr);
}
}

}


}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,16 @@
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_codeanalysis_forloopshouldbewhileloopsniff implements php_codesniffer_sniff
{
class moodle_sniffs_codeanalysis_forloopshouldbewhileloopsniff implements php_codesniffer_sniff {


/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array(integer)
*/
public function register()
{
public function register() {
return array(T_FOR);

}


Expand All @@ -67,8 +64,7 @@ public function register()
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
$token = $tokens[$stackptr];

Expand All @@ -85,8 +81,10 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)

for (; $next <= $end; ++$next) {
$code = $tokens[$next]['code'];

if ($code === T_SEMICOLON) {
++$index;

} else if (in_array($code, PHP_CodeSniffer_tokens::$emptyTokens) === false) {
++$parts[$index];
}
Expand All @@ -96,10 +94,5 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
$error = 'This FOR loop can be simplified to a WHILE loop';
$phpcsfile->addwarning($error, $stackptr);
}

}


}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,16 @@
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_codeanalysis_forloopwithtestfunctioncallsniff implements php_codesniffer_sniff
{
class moodle_sniffs_codeanalysis_forloopwithtestfunctioncallsniff implements php_codesniffer_sniff {


/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array(integer)
*/
public function register()
{
public function register() {
return array(T_FOR);

}


Expand All @@ -70,8 +67,7 @@ public function register()
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
$token = $tokens[$stackptr];

Expand All @@ -87,14 +83,17 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)

for (; $next <= $end; ++$next) {
$code = $tokens[$next]['code'];

if ($code === T_SEMICOLON) {
++$position;
}

if ($position < 1) {
continue;

} else if ($position > 1) {
break;

} else if ($code !== T_VARIABLE && $code !== T_STRING) {
continue;
}
Expand All @@ -109,10 +108,5 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
break;
}
}

}


}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,16 @@
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_codeanalysis_jumbledincrementersniff implements php_codesniffer_sniff
{
class moodle_sniffs_codeanalysis_jumbledincrementersniff implements php_codesniffer_sniff {


/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array(integer)
*/
public function register()
{
public function register() {
return array(T_FOR);

}


Expand All @@ -74,8 +71,7 @@ public function register()
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
$token = $tokens[$stackptr];

Expand All @@ -85,7 +81,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
}

// Find incrementors for outer loop.
$outer = $this->findIncrementers($tokens, $token);
$outer = $this->findincrementers($tokens, $token);

// Skip if empty.
if (count($outer) === 0) {
Expand All @@ -97,19 +93,19 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
$end = --$token['scope_closer'];

for (; $start <= $end; ++$start) {

if ($tokens[$start]['code'] !== T_FOR) {
continue;
}

$inner = $this->findIncrementers($tokens, $tokens[$start]);
$inner = $this->findincrementers($tokens, $tokens[$start]);
$diff = array_intersect($outer, $inner);

if (count($diff) !== 0) {
$error = sprintf('Loop incrementor (%s) jumbling with inner loop', join(', ', $diff));
$phpcsfile->addwarning($error, $stackptr);
}
}

}


Expand All @@ -121,8 +117,7 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
*
* @return array(string) List of all found incrementer variables.
*/
protected function findIncrementers(array $tokens, array $token)
{
protected function findincrementers(array $tokens, array $token) {
// Skip invalid statement.
if (isset($token['parenthesis_opener']) === false) {
return array();
Expand All @@ -133,10 +128,13 @@ protected function findIncrementers(array $tokens, array $token)

$incrementers = array();
$semicolons = 0;

for ($next = $start; $next <= $end; ++$next) {
$code = $tokens[$next]['code'];

if ($code === T_SEMICOLON) {
++$semicolons;

} else if ($semicolons === 2 && $code === T_VARIABLE) {
$incrementers[] = $tokens[$next]['content'];
}
Expand All @@ -145,8 +143,4 @@ protected function findIncrementers(array $tokens, array $token)
return $incrementers;

}


}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,16 @@
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_codeanalysis_unconditionalifstatementsniff implements php_codesniffer_sniff
{
class moodle_sniffs_codeanalysis_unconditionalifstatementsniff implements php_codesniffer_sniff {


/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array(integer)
*/
public function register()
{
return array(
T_IF,
T_ELSEIF,
);

public function register() {
return array(T_IF, T_ELSEIF);
}


Expand All @@ -74,8 +68,7 @@ public function register()
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
$token = $tokens[$stackptr];

Expand All @@ -87,25 +80,22 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
$next = ++$token['parenthesis_opener'];
$end = --$token['parenthesis_closer'];

$goodCondition = false;
$goodcondition = false;

for (; $next <= $end; ++$next) {
$code = $tokens[$next]['code'];

if (in_array($code, PHP_CodeSniffer_tokens::$emptyTokens) === true) {
continue;

} else if ($code !== T_TRUE && $code !== T_FALSE) {
$goodCondition = true;
$goodcondition = true;
}
}

if ($goodCondition === false) {
if ($goodcondition === false) {
$error = 'Avoid IF statements that are always true or false';
$phpcsfile->addwarning($error, $stackptr);
}

}


}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,16 @@
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_sniffs_codeanalysis_unnecessaryfinalmodifiersniff implements php_codesniffer_sniff
{
class moodle_sniffs_codeanalysis_unnecessaryfinalmodifiersniff implements php_codesniffer_sniff {


/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array(integer)
*/
public function register()
{
public function register() {
return array(T_CLASS);

}


Expand All @@ -67,8 +64,7 @@ public function register()
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
{
public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->gettokens();
$token = $tokens[$stackptr];

Expand All @@ -89,15 +85,11 @@ public function process(PHP_CodeSniffer_File $phpcsfile, $stackptr)
$end = --$token['scope_closer'];

for (; $next <= $end; ++$next) {

if ($tokens[$next]['code'] === T_FINAL) {
$error = 'Unnecessary FINAL modifier in FINAL class';
$phpcsfile->addwarning($error, $next);
}
}

}


}

?>
Loading

0 comments on commit 7f2068f

Please sign in to comment.