| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
|
|
11 |
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
|
|
12 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
13 |
* @version CVS: $Id: UnconditionalIfStatementSniff.php 252356 2008-02-06 02:38:37Z squiz $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Detects unconditional if- and elseif-statements.
|
|
|
19 |
*
|
|
|
20 |
* This rule is based on the PMD rule catalog. The Unconditional If Statment
|
|
|
21 |
* sniff detects statement conditions that are only set to one of the constant
|
|
|
22 |
* values <b>true</b> or <b>false</b>
|
|
|
23 |
*
|
|
|
24 |
* <code>
|
|
|
25 |
* class Foo
|
|
|
26 |
* {
|
|
|
27 |
* public function close()
|
|
|
28 |
* {
|
|
|
29 |
* if (true)
|
|
|
30 |
* {
|
|
|
31 |
* // ...
|
|
|
32 |
* }
|
|
|
33 |
* }
|
|
|
34 |
* }
|
|
|
35 |
* </code>
|
|
|
36 |
*
|
|
|
37 |
* @category PHP
|
|
|
38 |
* @package PHP_CodeSniffer
|
|
|
39 |
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
|
|
40 |
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
|
|
41 |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
|
|
42 |
* @version Release: 1.2.1
|
|
|
43 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
44 |
*/
|
|
|
45 |
class Generic_Sniffs_CodeAnalysis_UnconditionalIfStatementSniff implements PHP_CodeSniffer_Sniff
|
|
|
46 |
{
|
|
|
47 |
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Registers the tokens that this sniff wants to listen for.
|
|
|
51 |
*
|
|
|
52 |
* @return array(integer)
|
|
|
53 |
*/
|
|
|
54 |
public function register()
|
|
|
55 |
{
|
|
|
56 |
return array(
|
|
|
57 |
T_IF,
|
|
|
58 |
T_ELSEIF,
|
|
|
59 |
);
|
|
|
60 |
|
|
|
61 |
}//end register()
|
|
|
62 |
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Processes this test, when one of its tokens is encountered.
|
|
|
66 |
*
|
|
|
67 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
68 |
* @param int $stackPtr The position of the current token
|
|
|
69 |
* in the stack passed in $tokens.
|
|
|
70 |
*
|
|
|
71 |
* @return void
|
|
|
72 |
*/
|
|
|
73 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
74 |
{
|
|
|
75 |
$tokens = $phpcsFile->getTokens();
|
|
|
76 |
$token = $tokens[$stackPtr];
|
|
|
77 |
|
|
|
78 |
// Skip for-loop without body.
|
|
|
79 |
if (isset($token['parenthesis_opener']) === false) {
|
|
|
80 |
return;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
$next = ++$token['parenthesis_opener'];
|
|
|
84 |
$end = --$token['parenthesis_closer'];
|
|
|
85 |
|
|
|
86 |
$goodCondition = false;
|
|
|
87 |
for (; $next <= $end; ++$next) {
|
|
|
88 |
$code = $tokens[$next]['code'];
|
|
|
89 |
|
|
|
90 |
if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
|
|
|
91 |
continue;
|
|
|
92 |
} else if ($code !== T_TRUE && $code !== T_FALSE) {
|
|
|
93 |
$goodCondition = true;
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
if ($goodCondition === false) {
|
|
|
98 |
$error = 'Avoid IF statements that are always true or false';
|
|
|
99 |
$phpcsFile->addWarning($error, $stackPtr);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
}//end process()
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
}//end class
|
|
|
106 |
|
|
|
107 |
?>
|