Completed
Push — feature/minimal-codestyle-chec... ( aed179 )
by Juliette
02:09
created

AbstractComplexVersionSniff.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * PHPCompatibility_AbstractComplexVersionSniff.
4
 *
5
 * @category PHP
6
 * @package  PHPCompatibility
7
 * @author   Juliette Reinders Folmer <[email protected]>
8
 */
9
10
/**
11
 * PHPCompatibility_AbstractComplexVersionSniff.
12
 *
13
 * @category PHP
14
 * @package  PHPCompatibility
15
 * @author   Juliette Reinders Folmer <[email protected]>
16
 */
17
abstract class PHPCompatibility_AbstractComplexVersionSniff extends PHPCompatibility_Sniff implements PHPCompatibility_ComplexVersionInterface
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
{
19
20
21
    /**
22
     * Handle the retrieval of relevant information and - if necessary - throwing of an
23
     * error/warning for an item.
24
     *
25
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
26
     * @param int                  $stackPtr  The position of the relevant token in
27
     *                                        the stack.
28
     * @param array                $itemInfo  Base information about the item.
29
     *
30
     * @return void
31
     */
32
    public function handleFeature(PHP_CodeSniffer_File $phpcsFile, $stackPtr, array $itemInfo)
33
    {
34
        $itemArray = $this->getItemArray($itemInfo);
35
        $errorInfo = $this->getErrorInfo($itemArray, $itemInfo);
36
37
        if ($this->shouldThrowError($errorInfo) === true) {
38
            $this->addError($phpcsFile, $stackPtr, $itemInfo, $errorInfo);
39
        }
40
    }
41
42
43
    /**
44
     * Determine whether an error/warning should be thrown for an item based on collected information.
45
     *
46
     * @param array $errorInfo Detail information about an item.
47
     *
48
     * @return bool
49
     */
50
    abstract protected function shouldThrowError(array $errorInfo);
51
52
53
    /**
54
     * Get an array of the non-PHP-version array keys used in a sub-array.
55
     *
56
     * @return array
57
     */
58
    protected function getNonVersionArrayKeys()
59
    {
60
        return array();
61
    }
62
63
64
    /**
65
     * Retrieve a subset of an item array containing only the array keys which
66
     * contain PHP version information.
67
     *
68
     * @param array $itemArray Version and other information about an item.
69
     *
70
     * @return array Array with only the version information.
71
     */
72
    protected function getVersionArray(array $itemArray)
73
    {
74
        return array_diff_key($itemArray, array_flip($this->getNonVersionArrayKeys()));
75
    }
76
77
78
    /**
79
     * Get the item name to be used for the creation of the error code and in the error message.
80
     *
81
     * @param array $itemInfo  Base information about the item.
82
     * @param array $errorInfo Detail information about an item.
83
     *
84
     * @return string
85
     */
86
    protected function getItemName(array $itemInfo, array $errorInfo)
87
    {
88
        return $itemInfo['name'];
89
    }
90
91
92
    /**
93
     * Get the error message template for a specific sniff.
94
     *
95
     * @return string
96
     */
97
    abstract protected function getErrorMsgTemplate();
98
99
100
    /**
101
     * Allow for concrete child classes to filter the error message before it's passed to PHPCS.
102
     *
103
     * @param string $error     The error message which was created.
104
     * @param array  $itemInfo  Base information about the item this error message applied to.
105
     * @param array  $errorInfo Detail information about an item this error message applied to.
106
     *
107
     * @return string
108
     */
109
    protected function filterErrorMsg($error, array $itemInfo, array $errorInfo)
0 ignored issues
show
The parameter $itemInfo is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        return $error;
112
    }
113
114
115
    /**
116
     * Allow for concrete child classes to filter the error data before it's passed to PHPCS.
117
     *
118
     * @param array $data      The error data array which was created.
119
     * @param array $itemInfo  Base information about the item this error message applied to.
120
     * @param array $errorInfo Detail information about an item this error message applied to.
121
     *
122
     * @return array
123
     */
124
    protected function filterErrorData(array $data, array $itemInfo, array $errorInfo)
125
    {
126
        return $data;
127
    }
128
129
130
}//end class
131