Completed
Pull Request — master (#291)
by Juliette
02:26
created

addError()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 8
nop 4
1
<?php
2
/**
3
 * PHPCompatibility_AbstractRemovedFeatureSniff.
4
 *
5
 * @category PHP
6
 * @package  PHPCompatibility
7
 * @author   Juliette Reinders Folmer <[email protected]>
8
 */
9
10
/**
11
 * PHPCompatibility_AbstractRemovedFeatureSniff.
12
 *
13
 * @category PHP
14
 * @package  PHPCompatibility
15
 * @author   Juliette Reinders Folmer <[email protected]>
16
 */
17
abstract class PHPCompatibility_AbstractRemovedFeatureSniff
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
    extends PHPCompatibility_AbstractComplexVersionSniff
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
19
{
20
21
22
    /**
23
     * Determine whether an error/warning should be thrown for an item based on collected information.
24
     *
25
     * @param array $errorInfo Detail information about an item.
26
     *
27
     * @return bool
28
     */
29
    protected function shouldThrowError(array $errorInfo)
30
    {
31
        return ($errorInfo['deprecated'] !== '' || $errorInfo['removed'] !== '');
32
    }
33
34
35
    /**
36
     * Get an array of the non-PHP-version array keys used in a sub-array.
37
     *
38
     * By default, removed feature version arrays, contain an additional 'alternative' array key.
39
     *
40
     * @return array
41
     */
42
    protected function getNonVersionArrayKeys()
43
    {
44
        return array('alternative');
45
    }
46
47
48
    /**
49
     * Retrieve the relevant detail (version) information for use in an error message.
50
     *
51
     * @param array $itemArray Version and other information about the item.
52
     * @param array $itemInfo  Base information about the item.
53
     *
54
     * @return array
55
     */
56
    public function getErrorInfo(array $itemArray, array $itemInfo)
57
    {
58
        $errorInfo = array(
59
            'deprecated'  => '',
60
            'removed'     => '',
61
            'alternative' => '',
62
            'error'       => false,
63
        );
64
65
        $versionArray = $this->getVersionArray($itemArray);
66
67
        if (empty($versionArray) === false) {
68
            foreach ($versionArray as $version => $removed) {
69
                if ($this->supportsAbove($version) === true) {
70
                    if ($removed === true && $errorInfo['removed'] === '') {
71
                        $errorInfo['removed'] = $version;
72
                        $errorInfo['error']   = true;
73
                    } else if ($errorInfo['deprecated'] === '') {
74
                        $errorInfo['deprecated'] = $version;
75
                    }
76
                }
77
            }
78
        }
79
80
        if (isset($itemArray['alternative']) === true) {
81
            $errorInfo['alternative'] = $itemArray['alternative'];
82
        }
83
84
        return $errorInfo;
85
    }
86
87
88
    /**
89
     * Get the error message template for suggesting an alternative for a specific sniff.
90
     *
91
     * @return string
92
     */
93
    protected function getAlternativeOptionTemplate()
94
    {
95
        return '; Use %s instead';
96
    }
97
98
99
    /**
100
     * Generates the error or warning for this item.
101
     *
102
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
103
     * @param int                  $stackPtr  The position of the relevant token in
104
     *                                        the stack.
105
     * @param array                $itemInfo  Base information about the item.
106
     * @param array                $errorInfo Array with detail (version) information
107
     *                                        relevant to the item.
108
     *
109
     * @return void
110
     */
111
    public function addError(PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo, array $errorInfo)
112
    {
113
        $itemName = $this->getItemName($itemInfo, $errorInfo);
114
        $error    = $this->getErrorMsgTemplate();
115
116
        $errorCode = $this->stringToErrorCode($itemName);
117
        $data      = array($itemName);
118
119
        if ($errorInfo['deprecated'] !== '') {
120
            $error     .= 'deprecated since PHP %s and ';
121
            $errorCode .= 'Deprecated';
122
            $data[]     = $errorInfo['deprecated'];
123
        }
124
125
        if ($errorInfo['removed'] !== '') {
126
            $error     .= 'removed since PHP %s and ';
127
            $errorCode .= 'Removed';
128
            $data[]     = $errorInfo['removed'];
129
        }
130
131
        // Remove the last 'and' from the message.
132
        $error = substr($error, 0, (strlen($error) - 5));
133
134
        if ($errorInfo['alternative'] !== '') {
135
            $error .= $this->getAlternativeOptionTemplate();
136
            $data[] = $errorInfo['alternative'];
137
        }
138
139
        $error = $this->filterErrorMsg($error, $itemInfo, $errorInfo);
140
        $data  = $this->filterErrorData($data, $itemInfo, $errorInfo);
141
142
        $this->addMessage($phpcsFile, $error, $stackPtr, $errorInfo['error'], $errorCode, $data);
143
144
    }//end addError()
145
146
147
}//end class
148