Completed
Push — php70 ( dafa05...f7da2a )
by Wim
02:47
created

process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 2
dl 8
loc 8
rs 9.4285
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewOperatorsSniff.
4
 *
5
 * @category  PHP
6
 * @package   PHPCompatibility
7
 * @author    Wim Godden <[email protected]>
8
 */
9
10
/**
11
 * PHPCompatibility_Sniffs_PHP_NewOperatorsSniff.
12
 *
13
 * @category  PHP
14
 * @package   PHPCompatibility
15
 * @author    Wim Godden <[email protected]>
16
 */
17 View Code Duplication
class PHPCompatibility_Sniffs_PHP_NewOperatorsSniff extends PHPCompatibility_Sniff
0 ignored issues
show
Duplication introduced by
This class 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...
18
{
19
20
    /**
21
     * A list of new operators
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 keyword appears.
25
     *
26
     * @var array(string => array(string => int|string|null))
27
     */
28
    protected $newOperators = array (
29
                                        'T_SPACESHIP' => array(
30
                                            '5.6' => false,
31
                                            '7.0' => true,
32
                                            'description' => 'Spaceship operator'
33
                                        ),
34
                                        'float' => array(
35
                                            '5.6' => false,
36
                                            '7.0' => true,
37
                                            'description' => 'float return type'
38
                                        ),
39
                                        'bool' => array(
40
                                            '5.6' => false,
41
                                            '7.0' => true,
42
                                            'description' => 'bool return type'
43
                                        ),
44
                                        'string' => array(
45
                                            '5.6' => false,
46
                                            '7.0' => true,
47
                                            'description' => 'string return type'
48
                                        ),
49
                                    );
50
51
52
    /**
53
     * If true, an error will be thrown; otherwise a warning.
54
     *
55
     * @var bool
56
     */
57
    protected $error = false;
58
59
60
    /**
61
     * Returns an array of tokens this test wants to listen for.
62
     *
63
     * @return array
64
     */
65
    public function register()
66
    {
67
        return array(T_RETURN_TYPE);
68
    }//end register()
69
70
71
    /**
72
     * Processes this test, when one of its tokens is encountered.
73
     *
74
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
75
     * @param int                  $stackPtr  The position of the current token in
76
     *                                        the stack passed in $tokens.
77
     *
78
     * @return void
79
     */
80
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
81
    {
82
        $tokens = $phpcsFile->getTokens();
83
84
        if (in_array($tokens[$stackPtr]['content'], array_keys($this->newTypes))) {
0 ignored issues
show
Bug introduced by
The property newTypes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
85
            $this->addError($phpcsFile, $stackPtr, $tokens[$stackPtr]['content']);
86
        }
87
    }//end process()
88
89
90
    /**
91
     * Generates the error or wanrning for this sniff.
92
     *
93
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
94
     * @param int                  $stackPtr  The position of the function
95
     *                                        in the token array.
96
     * @param string               $typeName  The type.
97
     * @param string               $pattern   The pattern used for the match.
98
     *
99
     * @return void
100
     */
101
    protected function addError($phpcsFile, $stackPtr, $typeName, $pattern=null)
102
    {
103
        if ($pattern === null) {
104
            $pattern = $typeName;
105
        }
106
107
        $error = '';
108
109
        $this->error = false;
110
        foreach ($this->newTypes[$pattern] as $version => $present) {
111
            if ($this->supportsBelow($version)) {
112
                if ($present === false) {
113
                    $this->error = true;
114
                    $error .= 'not present in PHP version ' . $version . ' or earlier';
115
                }
116
            }
117
        }
118
        if (strlen($error) > 0) {
119
            $error = $this->newTypes[$typeName]['description'] . ' is ' . $error;
120
121
            if ($this->error === true) {
122
                $phpcsFile->addError($error, $stackPtr);
123
            } else {
124
                $phpcsFile->addWarning($error, $stackPtr);
125
            }
126
        }
127
128
    }//end addError()
129
130
}//end class
131