Completed
Pull Request — master (#388)
by Juliette
02:05
created

NewArrayStringDereferencingSniff::process()   C

Complexity

Conditions 11
Paths 15

Size

Total Lines 55
Code Lines 34

Duplication

Lines 18
Ratio 32.73 %

Importance

Changes 0
Metric Value
dl 18
loc 55
rs 6.6153
c 0
b 0
f 0
cc 11
eloc 34
nc 15
nop 2

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_NewArrayStringDereferencingSniff.
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_NewArrayStringDereferencingSniff.
14
 *
15
 * Array and string literals can now be dereferenced directly to access individual elements and characters.
16
 *
17
 * PHP version 5.5
18
 *
19
 * @category PHP
20
 * @package  PHPCompatibility
21
 * @author   Juliette Reinders Folmer <[email protected]>
22
 */
23
class PHPCompatibility_Sniffs_PHP_NewArrayStringDereferencingSniff 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...
24
{
25
    /**
26
     * Returns an array of tokens this test wants to listen for.
27
     *
28
     * @return array
29
     */
30
    public function register()
31
    {
32
        return array(
33
            T_ARRAY,
34
            T_OPEN_SHORT_ARRAY,
35
            T_CONSTANT_ENCAPSED_STRING,
36
        );
37
    }//end register()
38
39
    /**
40
     * Processes this test, when one of its tokens is encountered.
41
     *
42
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
43
     * @param int                  $stackPtr  The position of the current token in
44
     *                                        the stack passed in $tokens.
45
     *
46
     * @return void
47
     */
48
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
49
    {
50
        if ($this->supportsBelow('5.4') === false) {
51
            return;
52
        }
53
54
        $tokens = $phpcsFile->getTokens();
55
56
        switch($tokens[$stackPtr]['code']) {
57
            case T_CONSTANT_ENCAPSED_STRING:
58
                $type = 'string literals';
59
                $end  = $stackPtr;
60
                break;
61
62 View Code Duplication
            case T_ARRAY:
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
63
                if (isset($tokens[$stackPtr]['parenthesis_closer']) === false) {
64
                    // Live coding.
65
                    return;
66
                } else {
67
                    $type = 'arrays';
68
                    $end  = $tokens[$stackPtr]['parenthesis_closer'];
69
                }
70
                break;
71
72 View Code Duplication
            case T_OPEN_SHORT_ARRAY:
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
73
                if (isset($tokens[$stackPtr]['bracket_closer']) === false) {
74
                    // Live coding.
75
                    return;
76
                } else {
77
                    $type = 'arrays';
78
                    $end  = $tokens[$stackPtr]['bracket_closer'];
79
                }
80
                break;
81
        }
82
83
        if (isset($type, $end) === false) {
84
            // Shouldn't happen, but for some reason did.
85
            return;
86
        }
87
88
        $nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($end + 1), null, true, null, true);
0 ignored issues
show
Bug introduced by
The variable $end 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...
89
90
        if ($nextNonEmpty !== false &&
91
            ($tokens[$nextNonEmpty]['type'] === 'T_OPEN_SQUARE_BRACKET' ||
92
                $tokens[$nextNonEmpty]['type'] === 'T_OPEN_SHORT_ARRAY') // Work around bug #1381 in PHPCS 2.8.1 and lower.
93
        ) {
94
            $phpcsFile->addError(
95
                'Direct array dereferencing of %s is not present in PHP version 5.4 or earlier',
96
                $nextNonEmpty,
97
                'Found',
98
                array($type)
0 ignored issues
show
Bug introduced by
The variable $type 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...
99
            );
100
        }
101
102
    }//end process()
103
104
}//end class
105