Failed Conditions
Branch test-scrutinizer-coverage (7e25b2)
by Wim
13:51 queued 10:07
created

processClassToken()   C

Complexity

Conditions 11
Paths 23

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 11.0044

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 29
cts 30
cp 0.9667
rs 5.7333
c 0
b 0
f 0
cc 11
eloc 31
nc 23
nop 2
crap 11.0044

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewInterfacesSniff.
4
 *
5
 * @category PHP
6
 * @package  PHPCompatibility
7
 * @author   Juliette Reinders Folmer <[email protected]>
8
 */
9
10
/**
11
 * PHPCompatibility_Sniffs_PHP_NewInterfacesSniff.
12
 *
13
 * @category PHP
14
 * @package  PHPCompatibility
15
 * @author   Juliette Reinders Folmer <[email protected]>
16
 */
17
class PHPCompatibility_Sniffs_PHP_NewInterfacesSniff extends PHPCompatibility_AbstractNewFeatureSniff
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 new interfaces, not present in older versions.
22
     *
23
     * The array lists : version number with false (not present) or true (present).
24
     * If's sufficient to list the first version where the interface appears.
25
     *
26
     * @var array(string => array(string => int|string|null))
27
     */
28
    protected $newInterfaces = array(
29
        'Traversable' => array(
30
            '4.4' => false,
31
            '5.0' => true,
32
        ),
33
34
        'Countable' => array(
35
            '5.0' => false,
36
            '5.1' => true,
37
        ),
38
        'OuterIterator' => array(
39
            '5.0' => false,
40
            '5.1' => true,
41
        ),
42
        'RecursiveIterator' => array(
43
            '5.0' => false,
44
            '5.1' => true,
45
        ),
46
        'SeekableIterator' => array(
47
            '5.0' => false,
48
            '5.1' => true,
49
        ),
50
        'Serializable' => array(
51
            '5.0' => false,
52
            '5.1' => true,
53
        ),
54
        'SplObserver' => array(
55
            '5.0' => false,
56
            '5.1' => true,
57
        ),
58
        'SplSubject' => array(
59
            '5.0' => false,
60
            '5.1' => true,
61
        ),
62
63
        'JsonSerializable' => array(
64
            '5.3' => false,
65
            '5.4' => true,
66
        ),
67
        'SessionHandlerInterface' => array(
68
            '5.3' => false,
69
            '5.4' => true,
70
        ),
71
72
        'DateTimeInterface' => array(
73
            '5.4' => false,
74
            '5.5' => true,
75
        ),
76
77
        'Throwable' => array(
78
            '5.6' => false,
79
            '7.0' => true,
80
        ),
81
82
    );
83
84
    /**
85
     * A list of methods which cannot be used in combination with particular interfaces.
86
     *
87
     * @var array(string => array(string => string))
88
     */
89
    protected $unsupportedMethods = array(
90
        'Serializable' => array(
91
            '__sleep'  => 'http://php.net/serializable',
92
            '__wakeup' => 'http://php.net/serializable',
93
        ),
94
    );
95
96
    /**
97
     * Returns an array of tokens this test wants to listen for.
98
     *
99
     * @return array
100
     */
101 22
    public function register()
102
    {
103
        // Handle case-insensitivity of interface names.
104 22
        $this->newInterfaces      = $this->arrayKeysToLowercase($this->newInterfaces);
105 22
        $this->unsupportedMethods = $this->arrayKeysToLowercase($this->unsupportedMethods);
106
107
        $targets = array(
108 22
            T_CLASS,
109
            T_FUNCTION,
110 22
            T_CLOSURE,
111
        );
112
113 22
        if (defined('T_ANON_CLASS')) {
114 22
            $targets[] = constant('T_ANON_CLASS');
115
        }
116
117 22
        return $targets;
118
119
    }//end register()
120
121
122
    /**
123
     * Processes this test, when one of its tokens is encountered.
124
     *
125
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
126
     * @param int                  $stackPtr  The position of the current token in
127
     *                                        the stack passed in $tokens.
128
     *
129
     * @return void
130
     */
131 5 View Code Duplication
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133 5
        $tokens = $phpcsFile->getTokens();
134
135 5
        switch ($tokens[$stackPtr]['type']) {
136 5
            case 'T_CLASS':
137 5
            case 'T_ANON_CLASS':
138 5
                $this->processClassToken($phpcsFile, $stackPtr);
139 5
                break;
140
141 5
            case 'T_FUNCTION':
142 5
            case 'T_CLOSURE':
143 5
                $this->processFunctionToken($phpcsFile, $stackPtr);
144 5
                break;
145
146
            default:
147
                // Deliberately left empty.
148
                break;
149
        }
150
151 5
    }//end process()
152
153
154
    /**
155
     * Processes this test for when a class token is encountered.
156
     *
157
     * - Detect classes implementing the new interfaces.
158
     * - Detect classes implementing the new interfaces with unsupported functions.
159
     *
160
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
161
     * @param int                  $stackPtr  The position of the current token in
162
     *                                        the stack passed in $tokens.
163
     *
164
     * @return void
165
     */
166 5
    private function processClassToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
167
    {
168 5
        $interfaces = $this->findImplementedInterfaceNames($phpcsFile, $stackPtr);
169
170 5
        if (is_array($interfaces) === false || $interfaces === array()) {
171 5
            return;
172
        }
173
174 5
        $tokens       = $phpcsFile->getTokens();
175 5
        $checkMethods = false;
176
177 5
        if (isset($tokens[$stackPtr]['scope_closer'])) {
178 5
            $checkMethods = true;
179 5
            $scopeCloser = $tokens[$stackPtr]['scope_closer'];
180
        }
181
182 5
        foreach ($interfaces as $interface) {
183 5
            $interfaceLc = strtolower($interface);
184
185 5
            if (isset($this->newInterfaces[$interfaceLc]) === true) {
186
                $itemInfo = array(
187 5
                    'name'   => $interface,
188 5
                    'nameLc' => $interfaceLc,
189
                );
190 5
                $this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
191
            }
192
193 5
            if ($checkMethods === true && isset($this->unsupportedMethods[$interfaceLc]) === true) {
194 5
                $nextFunc = $stackPtr;
195 5
                while (($nextFunc = $phpcsFile->findNext(T_FUNCTION, ($nextFunc + 1), $scopeCloser)) !== false) {
0 ignored issues
show
Bug introduced by
The variable $scopeCloser does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
196 5
                    $funcName   = $phpcsFile->getDeclarationName($nextFunc);
197 5
                    $funcNameLc = strtolower($funcName);
198 5
                    if ($funcNameLc === '') {
199
                        continue;
200
                    }
201
202 5
                    if (isset($this->unsupportedMethods[$interfaceLc][$funcNameLc]) === true) {
203 5
                        $error     = 'Classes that implement interface %s do not support the method %s(). See %s';
204 5
                        $errorCode = $this->stringToErrorCode($interface).'UnsupportedMethod';
205
                        $data      = array(
206 5
                            $interface,
207 5
                            $funcName,
208 5
                            $this->unsupportedMethods[$interfaceLc][$funcNameLc],
209
                        );
210
211 5
                        $phpcsFile->addError($error, $nextFunc, $errorCode, $data);
212
                    }
213
                }
214
            }
215
        }
216 5
    }//end processClassToken()
217
218
219
    /**
220
     * Processes this test for when a function token is encountered.
221
     *
222
     * - Detect new interfaces when used as a type hint.
223
     *
224
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
225
     * @param int                  $stackPtr  The position of the current token in
226
     *                                        the stack passed in $tokens.
227
     *
228
     * @return void
229
     */
230 5 View Code Duplication
    private function processFunctionToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
    {
232 5
        $typeHints = $this->getTypeHintsFromFunctionDeclaration($phpcsFile, $stackPtr);
233 5
        if (empty($typeHints) || is_array($typeHints) === false) {
234 5
            return;
235
        }
236
237 5
        foreach ($typeHints as $hint) {
238
239 5
            $typeHintLc = strtolower($hint);
240
241 5
            if (isset($this->newInterfaces[$typeHintLc]) === true) {
242
                $itemInfo = array(
243 5
                    'name'   => $hint,
244 5
                    'nameLc' => $typeHintLc,
245
                );
246 5
                $this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
247
            }
248
        }
249 5
    }
250
251
252
    /**
253
     * Get the relevant sub-array for a specific item from a multi-dimensional array.
254
     *
255
     * @param array $itemInfo Base information about the item.
256
     *
257
     * @return array Version and other information about the item.
258
     */
259 5
    public function getItemArray(array $itemInfo)
260
    {
261 5
        return $this->newInterfaces[$itemInfo['nameLc']];
262
    }
263
264
265
    /**
266
     * Get the error message template for this sniff.
267
     *
268
     * @return string
269
     */
270 5
    protected function getErrorMsgTemplate()
271
    {
272 5
        return 'The built-in interface '.parent::getErrorMsgTemplate();
273
    }
274
275
276
}//end class
277