Completed
Push — master ( a0c8a1...aee42a )
by Wim
9s
created

InternalInterfacesSniff::process()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 12
c 1
b 0
f 1
nc 4
nop 2
dl 0
loc 21
rs 8.7624
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_InternalInterfacesSniff.
4
 *
5
 * PHP version 5.5
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Juliette Reinders Folmer <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_InternalInterfacesSniff.
14
 *
15
 * @category  PHP
16
 * @package   PHPCompatibility
17
 * @author    Juliette Reinders Folmer <[email protected]>
18
 */
19
class PHPCompatibility_Sniffs_PHP_InternalInterfacesSniff extends PHPCompatibility_Sniff
20
{
21
22
    /**
23
     * A list of PHP internal interfaces, not intended to be implemented by userland classes.
24
     *
25
     * The array lists : the error message to use.
26
     *
27
     * @var array(string => string)
28
     */
29
    protected $internalInterfaces = array(
30
        'Traversable'       => 'shouldn\'t be implemented directly, implement the Iterator or IteratorAggregate interface instead.',
31
        'DateTimeInterface' => 'is intended for type hints only and is not implementable.',
32
        'Throwable'         => 'cannot be implemented directly, extend the Exception class instead.',
33
    );
34
35
36
    /**
37
     * Returns an array of tokens this test wants to listen for.
38
     *
39
     * @return array
40
     */
41
    public function register()
42
    {
43
        // Handle case-insensitivity of class names.
44
        $keys = array_keys( $this->internalInterfaces );
45
        $keys = array_map( 'strtolower', $keys );
46
        $this->internalInterfaces = array_combine( $keys, $this->internalInterfaces );
47
48
        return array(T_CLASS);
49
50
    }//end register()
51
52
53
    /**
54
     * Processes this test, when one of its tokens is encountered.
55
     *
56
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
57
     * @param int                  $stackPtr  The position of the current token in
58
     *                                        the stack passed in $tokens.
59
     *
60
     * @return void
61
     */
62
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
63
    {
64
        $interfaces = $this->findImplementedInterfaceNames($phpcsFile, $stackPtr);
65
66
        if (is_array($interfaces) === false || $interfaces === array()) {
67
            return;
68
        }
69
70
        foreach ($interfaces as $interface) {
71
            $lcInterface = strtolower($interface);
72
            if (isset($this->internalInterfaces[$lcInterface]) === true) {
73
                $error = 'The interface %s %s';
74
                $data  = array(
75
                    $interface,
76
                    $this->internalInterfaces[$lcInterface],
77
                );
78
                $phpcsFile->addError($error, $stackPtr, 'Found', $data);
79
            }
80
        }
81
82
    }//end process()
83
84
}//end class
85