Completed
Pull Request — master (#116)
by Bill
03:51
created

ForbiddenMethodsSniff::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
1
<?php declare(strict_types = 1);
2
3
namespace Codor\Sniffs\Files;
4
5
use PHP_CodeSniffer_Sniff;
6
use PHP_CodeSniffer_File;
7
8
class ForbiddenMethodsSniff implements PHP_CodeSniffer_Sniff
9
{
10
    /**
11
     * The methods that are forbidden.
12
     * @var array
13
     */
14
    public $forbiddenMethods = ['raw', 'statement'];
15
16
    /**
17
     * Returns the token types that this sniff is interested in.
18
     * @return array
19
     */
20
    public function register(): array
21
    {
22
        return [T_OBJECT_OPERATOR, T_DOUBLE_COLON];
23
    }
24
25
    /**
26
     * Processes the tokens that this sniff is interested in.
27
     *
28
     * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
29
     * @param integer              $stackPtr  The position in the stack where
30
     *                                    the token was found.
31
     * @return void
32
     */
33
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
34
    {
35
        $tokens = $phpcsFile->getTokens();
36
        $potentialMethodToken = $tokens[$stackPtr + 1];
37
        $openParenToken = $tokens[$stackPtr + 2];
38
39
        if ($openParenToken['type'] !== 'T_OPEN_PARENTHESIS') {
40
            return;
41
        }
42
43
        $methodName = $potentialMethodToken['content'];
44
        if (in_array($methodName, $this->forbiddenMethods, true)) {
45
            $phpcsFile->addError("Call to method {$methodName} is forbidden.", $potentialMethodToken['line']);
46
        }
47
    }
48
}
49