Completed
Pull Request — master (#64)
by Bill
02:04
created

MethodFlagParameterSniff   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A process() 0 15 3
1
<?php
2
3
namespace Codor\Sniffs\Files;
4
5
use PHP_CodeSniffer_Sniff;
6
use PHP_CodeSniffer_File;
7
8
class MethodFlagParameterSniff implements PHP_CodeSniffer_Sniff
9
{
10
11
    protected $booleans = ['T_FALSE', 'T_TRUE'];
12
13
    /**
14
     * Returns the token types that this sniff is interested in.
15
     * @return array
16
     */
17
    public function register()
18
    {
19
        return [T_FUNCTION];
20
    }
21
22
    /**
23
     * Processes the tokens that this sniff is interested in.
24
     *
25
     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
26
     * @param integer              $stackPtr  The position in the stack where
27
     *                                    the token was found.
28
     * @return void
29
     */
30
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
31
    {
32
        $tokens           = $phpcsFile->getTokens();
33
34
        $token            = $tokens[$stackPtr];
35
        $openParenIndex   = $token['parenthesis_opener'];
36
        $closedParenIndex = $token['parenthesis_closer'];
37
38
        for ($index=$openParenIndex+1; $index <= $closedParenIndex; $index++) {
39
            if (in_array($tokens[$index]['type'], $this->booleans)) {
40
                $phpcsFile->addError("Function/method contains a flag parameter.", $stackPtr);
41
                continue;
42
            }
43
        }
44
    }
45
}
46