Completed
Push — feature/new-optionalrequiredfu... ( 670d7b )
by Juliette
01:49
created

getErrorInfo()   C

Complexity

Conditions 7
Paths 2

Size

Total Lines 29
Code Lines 17

Duplication

Lines 29
Ratio 100 %

Importance

Changes 0
Metric Value
dl 29
loc 29
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
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
        'parse_str' => array(
36
            1 => array(
37
                'name' => 'result',
38
                '7.2'  => false,
39
            ),
40
        ),
41
    );
42
43
44
    /**
45
     * Determine whether an error/warning should be thrown for an item based on collected information.
46
     *
47
     * @param array $errorInfo Detail information about an item.
48
     *
49
     * @return bool
50
     */
51
    protected function shouldThrowError(array $errorInfo)
52
    {
53
        return ($errorInfo['optionalDeprecated'] !== '' || $errorInfo['optionalRemoved'] !== '');
54
    }
55
56
57
    /**
58
     * Retrieve the relevant detail (version) information for use in an error message.
59
     *
60
     * @param array $itemArray Version and other information about the item.
61
     * @param array $itemInfo  Base information about the item.
62
     *
63
     * @return array
64
     */
65 View Code Duplication
    public function getErrorInfo(array $itemArray, array $itemInfo)
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...
66
    {
67
        $errorInfo = array(
68
            'paramName'          => '',
69
            'optionalDeprecated' => '',
70
            'optionalRemoved'    => '',
71
            'error'              => false,
72
        );
73
74
        $versionArray = $this->getVersionArray($itemArray);
75
76
        if (empty($versionArray) === false) {
77
            foreach ($versionArray as $version => $required) {
78
                if ($this->supportsAbove($version) === true) {
79
                    if ($required === true && $errorInfo['optionalRemoved'] === '') {
80
                        $errorInfo['optionalRemoved'] = $version;
81
                        $errorInfo['error']           = true;
82
                    } elseif ($errorInfo['optionalDeprecated'] === '') {
83
                        $errorInfo['optionalDeprecated'] = $version;
84
                    }
85
                }
86
            }
87
        }
88
89
        $errorInfo['paramName'] = $itemArray['name'];
90
91
        return $errorInfo;
92
93
    }//end getErrorInfo()
94
95
96
    /**
97
     * Get the error message template for this sniff.
98
     *
99
     * @return string
100
     */
101
    protected function getErrorMsgTemplate()
102
    {
103
        return 'The "%s" parameter for function %s() is missing. Passing this parameter is no longer optional. The optional nature of the parameter is ';
104
    }
105
106
107
    /**
108
     * Generates the error or warning for this item.
109
     *
110
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
111
     * @param int                   $stackPtr  The position of the relevant token in
112
     *                                         the stack.
113
     * @param array                 $itemInfo  Base information about the item.
114
     * @param array                 $errorInfo Array with detail (version) information
115
     *                                         relevant to the item.
116
     *
117
     * @return void
118
     */
119
    public function addError(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo)
120
    {
121
        $error     = $this->getErrorMsgTemplate();
122
        $errorCode = $this->stringToErrorCode($itemInfo['name'].'_'.$errorInfo['paramName']);
123
        $data      = array(
124
            $errorInfo['paramName'],
125
            $itemInfo['name'],
126
        );
127
128
        if ($errorInfo['optionalDeprecated'] !== '') {
129
            $error     .= 'deprecated since PHP %s and ';
130
            $errorCode .= 'Soft';
131
            $data[]     = $errorInfo['optionalDeprecated'];
132
        }
133
134
        if ($errorInfo['optionalRemoved'] !== '') {
135
            $error     .= 'removed since PHP %s and ';
136
            $errorCode .= 'Hard';
137
            $data[]     = $errorInfo['optionalRemoved'];
138
        }
139
140
        // Remove the last 'and' from the message.
141
        $error      = substr($error, 0, (strlen($error) - 5));
142
        $errorCode .= 'Required';
143
144
        $this->addMessage($phpcsFile, $error, $stackPtr, $errorInfo['error'], $errorCode, $data);
145
146
    }//end addError()
147
148
149
}//end class
150