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

PHPCompatibility_Sniffs_PHP_NewMagicMethodsSniff   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 176
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
A process() 0 20 3
A getItemArray() 0 4 1
A getNonVersionArrayKeys() 0 4 1
A getErrorInfo() 0 12 2
A getErrorMsgTemplate() 0 4 1
A filterErrorMsg() 0 8 2
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewMagicMethodsSniff.
4
 *
5
 * @category  PHP
6
 * @package   PHPCompatibility
7
 * @author    Juliette Reinders Folmer <[email protected]>
8
 */
9
10
/**
11
 * PHPCompatibility_Sniffs_PHP_NewMagicMethodsSniff.
12
 *
13
 * Warns for non-magic behaviour of magic methods prior to becoming magic.
14
 *
15
 * @category  PHP
16
 * @package   PHPCompatibility
17
 * @author    Juliette Reinders Folmer <[email protected]>
18
 */
19
class PHPCompatibility_Sniffs_PHP_NewMagicMethodsSniff
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...
20
    extends PHPCompatibility_AbstractNewFeatureSniff
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
21
{
22
23
    /**
24
     * A list of new magic methods, not considered magic in older versions.
25
     *
26
     * Method names in the array should be all *lowercase*.
27
     * The array lists : version number with false (not magic) or true (magic).
28
     * If's sufficient to list the first version where the method became magic.
29
     *
30
     * @var array(string => array(string => int|string|null))
31
     */
32
    protected $newMagicMethods = array(
33
                               '__get' => array( // verified
34
                                   '4.4' => false,
35
                                   '5.0' => true,
36
                               ),
37
38
                               '__isset' => array( // verified
39
                                   '5.0' => false,
40
                                   '5.1' => true,
41
                               ),
42
                               '__unset' => array( // verified
43
                                   '5.0' => false,
44
                                   '5.1' => true,
45
                               ),
46
                               '__set_state' => array( // verified
47
                                   '5.0' => false,
48
                                   '5.1' => true,
49
                               ),
50
51
                               '__callstatic' => array( // verified
52
                                   '5.2' => false,
53
                                   '5.3' => true,
54
                               ),
55
                               '__invoke' => array( // verified
56
                                   '5.2' => false,
57
                                   '5.3' => true,
58
                               ),
59
60
                               '__debuginfo' => array( // verified
61
                                   '5.5' => false,
62
                                   '5.6' => true,
63
                               ),
64
65
                               // Special case - only became properly magical in 5.2.0,
66
                               // before that it was only called for echo and print.
67
                               '__tostring' => array(
68
                                   '5.1' => false,
69
                                   '5.2' => true,
70
                                   'message' => 'The method %s() was not truly magical in PHP version %s and earlier. The associated magic functionality will only be called when directly combined with echo or print.',
71
                               ),
72
                              );
73
74
75
    /**
76
     * Returns an array of tokens this test wants to listen for.
77
     *
78
     * @return array
79
     */
80
    public function register()
81
    {
82
        return array(T_FUNCTION);
83
84
    }//end register()
85
86
87
    /**
88
     * Processes this test, when one of its tokens is encountered.
89
     *
90
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
91
     * @param int                  $stackPtr  The position of the current token in the
92
     *                                        stack passed in $tokens.
93
     *
94
     * @return void
95
     */
96
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
97
    {
98
        $functionName   = $phpcsFile->getDeclarationName($stackPtr);
99
        $functionNameLc = strtolower($functionName);
100
101
        if (isset($this->newMagicMethods[$functionNameLc]) === false) {
102
            return;
103
        }
104
105
        if ($this->inClassScope($phpcsFile, $stackPtr, false) === false) {
106
            return;
107
        }
108
109
        $itemInfo = array(
110
            'name'   => $functionName,
111
            'nameLc' => $functionNameLc,
112
        );
113
        $this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
114
115
    }//end process()
116
117
118
    /**
119
     * Get the relevant sub-array for a specific item from a multi-dimensional array.
120
     *
121
     * @param array $itemInfo Base information about the item.
122
     *
123
     * @return array Version and other information about the item.
124
     */
125
    public function getItemArray(array $itemInfo)
126
    {
127
        return $this->newMagicMethods[$itemInfo['nameLc']];
128
    }
129
130
131
    /**
132
     * Get an array of the non-PHP-version array keys used in a sub-array.
133
     *
134
     * @return array
135
     */
136
    protected function getNonVersionArrayKeys()
137
    {
138
        return array('message');
139
    }
140
141
142
    /**
143
     * Retrieve the relevant detail (version) information for use in an error message.
144
     *
145
     * @param array $itemArray Version and other information about the item.
146
     * @param array $itemInfo  Base information about the item.
147
     *
148
     * @return array
149
     */
150
    public function getErrorInfo(array $itemArray, array $itemInfo)
151
    {
152
        $errorInfo            = parent::getErrorInfo($itemArray, $itemInfo);
153
        $errorInfo['error']   = false; // Warning, not error.
154
        $errorInfo['message'] = '';
155
156
        if (empty($itemArray['message']) === false) {
157
            $errorInfo['message'] = $itemArray['message'];
158
        }
159
160
        return $errorInfo;
161
    }
162
163
164
    /**
165
     * Get the error message template for this sniff.
166
     *
167
     * @return string
168
     */
169
    protected function getErrorMsgTemplate()
170
    {
171
        return 'The method %s() was not magical in PHP version %s and earlier. The associated magic functionality will not be invoked.';
172
    }
173
174
175
    /**
176
     * Allow for concrete child classes to filter the error message before it's passed to PHPCS.
177
     *
178
     * @param string $error     The error message which was created.
179
     * @param array  $itemInfo  Base information about the item this error message applied to.
180
     * @param array  $errorInfo Detail information about an item this error message applied to.
181
     *
182
     * @return string
183
     */
184
    protected function filterErrorMsg($error, array $itemInfo, array $errorInfo)
185
    {
186
        if ($errorInfo['message'] !== '') {
187
            $error = $errorInfo['message'];
188
        }
189
190
        return $error;
191
    }
192
193
194
}//end class
195