Failed Conditions
Push — test-scrutinizer-coverage ( 1fb662...215aeb )
by Juliette
03:50
created

InternalInterfacesSniff   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 73
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 23 5
A register() 0 14 2
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_InternalInterfacesSniff.
4
 *
5
 * @category PHP
6
 * @package  PHPCompatibility
7
 * @author   Juliette Reinders Folmer <[email protected]>
8
 */
9
10
/**
11
 * PHPCompatibility_Sniffs_PHP_InternalInterfacesSniff.
12
 *
13
 * @category PHP
14
 * @package  PHPCompatibility
15
 * @author   Juliette Reinders Folmer <[email protected]>
16
 */
17
class PHPCompatibility_Sniffs_PHP_InternalInterfacesSniff extends PHPCompatibility_Sniff
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
18
{
19
20
    /**
21
     * A list of PHP internal interfaces, not intended to be implemented by userland classes.
22
     *
23
     * The array lists : the error message to use.
24
     *
25
     * @var array(string => string)
26
     */
27
    protected $internalInterfaces = array(
28
        'Traversable'       => 'shouldn\'t be implemented directly, implement the Iterator or IteratorAggregate interface instead.',
29
        'DateTimeInterface' => 'is intended for type hints only and is not implementable.',
30
        'Throwable'         => 'cannot be implemented directly, extend the Exception class instead.',
31
    );
32
33
34
    /**
35
     * Returns an array of tokens this test wants to listen for.
36
     *
37
     * @return array
38
     */
39 13
    public function register()
40
    {
41
        // Handle case-insensitivity of interface names.
42 13
        $this->internalInterfaces = $this->arrayKeysToLowercase($this->internalInterfaces);
43
44 13
        $targets = array(T_CLASS);
45
46 13
        if (defined('T_ANON_CLASS')) {
47 13
            $targets[] = constant('T_ANON_CLASS');
48 13
        }
49
50 13
        return $targets;
51
52
    }//end register()
53
54
55
    /**
56
     * Processes this test, when one of its tokens is encountered.
57
     *
58
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
59
     * @param int                  $stackPtr  The position of the current token in
60
     *                                        the stack passed in $tokens.
61
     *
62
     * @return void
63
     */
64 1
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
65
    {
66 1
        $interfaces = $this->findImplementedInterfaceNames($phpcsFile, $stackPtr);
67
68 1
        if (is_array($interfaces) === false || $interfaces === array()) {
69
            return;
70
        }
71
72 1
        foreach ($interfaces as $interface) {
73 1
            $interfaceLc = strtolower($interface);
74 1
            if (isset($this->internalInterfaces[$interfaceLc]) === true) {
75 1
                $error     = 'The interface %s %s';
76 1
                $errorCode = $this->stringToErrorCode($interfaceLc).'Found';
77
                $data      = array(
78 1
                    $interface,
79 1
                    $this->internalInterfaces[$interfaceLc],
80 1
                );
81
82 1
                $phpcsFile->addError($error, $stackPtr, $errorCode, $data);
83 1
            }
84 1
        }
85
86 1
    }//end process()
87
88
89
}//end class
90