Completed
Push — php5.6/optionalrequired-add-re... ( 60a13b )
by Juliette
03:05 queued 01:32
created

getErrorInfo()   D

Complexity

Conditions 9
Paths 2

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 4.909
c 0
b 0
f 0
cc 9
eloc 20
nc 2
nop 2
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\OptionalRequiredFunctionParametersSniff.
4
 *
5
 * @category PHP
6
 * @package  PHPCompatibility
7
 * @author   Juliette Reinders Folmer <[email protected]>
8
 */
9
10
namespace PHPCompatibility\Sniffs\PHP;
11
12
use PHPCompatibility\Sniffs\PHP\RequiredOptionalFunctionParametersSniff;
13
14
/**
15
 * \PHPCompatibility\Sniffs\PHP\OptionalRequiredFunctionParametersSniff.
16
 *
17
 * @category PHP
18
 * @package  PHPCompatibility
19
 * @author   Juliette Reinders Folmer <[email protected]>
20
 */
21
class OptionalRequiredFunctionParametersSniff extends RequiredOptionalFunctionParametersSniff
22
{
23
24
    /**
25
     * A list of function parameters, which were optional in older versions and became required later on.
26
     *
27
     * The array lists : version number with true (required) and false (optional use deprecated).
28
     *
29
     * The index is the location of the parameter in the parameter list, starting at 0 !
30
     * If's sufficient to list the last version in which the parameter was not yet required.
31
     *
32
     * @var array
33
     */
34
    protected $functionParameters = array(
35
        // Special case, the optional nature is not deprecated, but usage is recommended
36
        // and leaving the parameter out will throw an E_NOTICE.
37
        'crypt' => array(
38
            1 => array(
39
                'name' => 'salt',
40
                '5.6'  => 'recommended',
41
            ),
42
        ),
43
        'parse_str' => array(
44
            1 => array(
45
                'name' => 'result',
46
                '7.2'  => false,
47
            ),
48
        ),
49
    );
50
51
52
    /**
53
     * Determine whether an error/warning should be thrown for an item based on collected information.
54
     *
55
     * @param array $errorInfo Detail information about an item.
56
     *
57
     * @return bool
58
     */
59
    protected function shouldThrowError(array $errorInfo)
60
    {
61
        return ($errorInfo['optionalDeprecated'] !== ''
62
            || $errorInfo['optionalRemoved'] !== ''
63
            || $errorInfo['optionalRecommended'] !== '');
64
    }
65
66
67
    /**
68
     * Retrieve the relevant detail (version) information for use in an error message.
69
     *
70
     * @param array $itemArray Version and other information about the item.
71
     * @param array $itemInfo  Base information about the item.
72
     *
73
     * @return array
74
     */
75
    public function getErrorInfo(array $itemArray, array $itemInfo)
76
    {
77
        $errorInfo = array(
78
            'paramName'           => '',
79
            'optionalRecommended' => '',
80
            'optionalDeprecated'  => '',
81
            'optionalRemoved'     => '',
82
            'error'               => false,
83
        );
84
85
        $versionArray = $this->getVersionArray($itemArray);
86
87
        if (empty($versionArray) === false) {
88
            foreach ($versionArray as $version => $required) {
89
                if ($this->supportsAbove($version) === true) {
90
                    if ($required === true && $errorInfo['optionalRemoved'] === '') {
91
                        $errorInfo['optionalRemoved'] = $version;
92
                        $errorInfo['error']           = true;
93
                    } elseif ($required === 'recommended' && $errorInfo['optionalRecommended'] === '') {
94
                        $errorInfo['optionalRecommended'] = $version;
95
                    } elseif ($errorInfo['optionalDeprecated'] === '') {
96
                        $errorInfo['optionalDeprecated'] = $version;
97
                    }
98
                }
99
            }
100
        }
101
102
        $errorInfo['paramName'] = $itemArray['name'];
103
104
        return $errorInfo;
105
106
    }//end getErrorInfo()
107
108
109
    /**
110
     * Generates the error or warning for this item.
111
     *
112
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
113
     * @param int                   $stackPtr  The position of the relevant token in
114
     *                                         the stack.
115
     * @param array                 $itemInfo  Base information about the item.
116
     * @param array                 $errorInfo Array with detail (version) information
117
     *                                         relevant to the item.
118
     *
119
     * @return void
120
     */
121
    public function addError(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo)
122
    {
123
        $error = 'The "%s" parameter for function %s() is missing. Passing this parameter is ';
124
        if ($errorInfo['optionalRecommended'] === '') {
125
            $error .= 'no longer optional. The optional nature of the parameter is ';
126
        } else {
127
            $error .= 'strongly recommended ';
128
        }
129
130
        $errorCode = $this->stringToErrorCode($itemInfo['name'].'_'.$errorInfo['paramName']);
131
        $data      = array(
132
            $errorInfo['paramName'],
133
            $itemInfo['name'],
134
        );
135
136
        if ($errorInfo['optionalRecommended'] !== '') {
137
            $error     .= 'since PHP %s ';
138
            $errorCode .= 'SoftRecommended';
139
            $data[]     = $errorInfo['optionalRecommended'];
140
        } else {
141
            if ($errorInfo['optionalDeprecated'] !== '') {
142
                $error     .= 'deprecated since PHP %s and ';
143
                $errorCode .= 'SoftRequired';
144
                $data[]     = $errorInfo['optionalDeprecated'];
145
            }
146
147
            if ($errorInfo['optionalRemoved'] !== '') {
148
                $error     .= 'removed since PHP %s and ';
149
                $errorCode .= 'HardRequired';
150
                $data[]     = $errorInfo['optionalRemoved'];
151
            }
152
153
            // Remove the last 'and' from the message.
154
            $error      = substr($error, 0, (strlen($error) - 5));
155
        }
156
157
        $this->addMessage($phpcsFile, $error, $stackPtr, $errorInfo['error'], $errorCode, $data);
158
159
    }//end addError()
160
161
162
}//end class
163