Completed
Push — feature/issue-367-phpcs-3.x-co... ( e185aa )
by Juliette
01:53
created

AbstractRemovedFeatureSniff::getErrorInfo()   C

Complexity

Conditions 8
Paths 4

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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