Completed
Pull Request — master (#148)
by Juliette
04:48
created

NonStaticMagicMethodsSniff::process()   C

Complexity

Conditions 11
Paths 17

Size

Total Lines 68
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 68
rs 5.8371
cc 11
eloc 35
nc 17
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NonStaticMagicMethodsSniff.
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 * @copyright 2012 Cu.be Solutions bvba
11
 */
12
13
/**
14
 * PHPCompatibility_Sniffs_PHP_NonStaticMagicMethodsSniff.
15
 *
16
 * Verifies the use of the correct visibility and static properties of magic methods.
17
 *
18
 * @category  PHP
19
 * @package   PHPCompatibility
20
 * @author    Wim Godden <[email protected]>
21
 * @copyright 2012 Cu.be Solutions bvba
22
 */
23
class PHPCompatibility_Sniffs_PHP_NonStaticMagicMethodsSniff extends PHPCompatibility_Sniff
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...
24
{
25
26
    /**
27
     * A list of PHP magic methods and their visibility and static requirements.
28
     *
29
     * Method names in the array should be all *lowercase*.
30
     * Visibility can be either 'public', 'protected' or 'private'.
31
     * Static can be either 'true' - *must* be static, or 'false' - *must* be non-static.
32
     * When a method does not have a specific requirement for either visibility or static,
33
     * do *not* add the key.
34
     *
35
     * @var array(string)
36
     */
37
    protected $magicMethods = array(
38
        '__get' => array(
39
            'visibility' => 'public',
40
            'static'     => false,
41
        ),
42
        '__set' => array(
43
            'visibility' => 'public',
44
            'static'     => false,
45
        ),
46
        '__isset' => array(
47
            'visibility' => 'public',
48
            'static'     => false,
49
        ),
50
        '__unset' => array(
51
            'visibility' => 'public',
52
            'static'     => false,
53
        ),
54
        '__call' => array(
55
            'visibility' => 'public',
56
            'static'     => false,
57
        ),
58
        '__callstatic' => array(
59
            'visibility' => 'public',
60
            'static'     => true,
61
        ),
62
        '__sleep' => array(
63
            'visibility' => 'public',
64
        ),
65
        '__tostring' => array(
66
            'visibility' => 'public',
67
        ),
68
        '__set_state' => array(
69
            'static'     => true,
70
        ),
71
    );
72
73
74
    /**
75
     * Returns an array of tokens this test wants to listen for.
76
     *
77
     * @return array
78
     */
79
    public function register()
80
    {
81
        return array(T_CLASS, T_INTERFACE, T_TRAIT);
82
83
    }//end register()
84
85
86
    /**
87
     * Processes this test, when one of its tokens is encountered.
88
     *
89
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
90
     * @param int                  $stackPtr  The position of the current token in the
91
     *                                        stack passed in $tokens.
92
     *
93
     * @return void
94
     */
95
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
96
    {
97
        // Should be removed, the requirement was previously also there, 5.3 just started throwing a warning about it.
98
        if ($this->supportsAbove('5.3') === false) {
99
            return;
100
        }
101
102
        $tokens = $phpcsFile->getTokens();
103
104
        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
105
            return;
106
        }
107
108
        $classScopeCloser = $tokens[$stackPtr]['scope_closer'];
109
        $functionPtr      = $stackPtr;
110
111
        // Find all the functions in this class or interface.
112
        while ($functionToken = $phpcsFile->findNext(T_FUNCTION, $functionPtr, $classScopeCloser)) {
113
            /*
114
             * Get the scope closer for this function in order to know how
115
             * to advance to the next function.
116
             * If no body of function (e.g. for interfaces), there is
117
             * no closing curly brace; advance the pointer differently.
118
             */
119
            $scopeCloser = isset($tokens[$functionToken]['scope_closer'])
120
                ? $tokens[$functionToken]['scope_closer']
121
                : $functionToken + 1;
122
123
124
            $methodName   = $phpcsFile->getDeclarationName($functionToken);
125
            $methodNameLc = strtolower($methodName);
126
            if (isset($this->magicMethods[$methodNameLc]) === false) {
127
                $functionPtr = $scopeCloser;
128
                continue;
129
            }
130
131
            $methodProperties = $phpcsFile->getMethodProperties($functionToken);
132
133
            if (isset($this->magicMethods[$methodNameLc]['visibility']) && $this->magicMethods[$methodNameLc]['visibility'] !== $methodProperties['scope']) {
134
                $error = 'Visibility for magic method %s must be %s. Found: %s';
135
                $data  = array(
136
                    $methodName,
137
                    $this->magicMethods[$methodNameLc]['visibility'],
138
                    $methodProperties['scope'],
139
                );
140
141
                $phpcsFile->addError($error, $functionToken, 'MethodVisibility', $data);
142
            }
143
144
            if (isset($this->magicMethods[$methodNameLc]['static']) && $this->magicMethods[$methodNameLc]['static'] !== $methodProperties['is_static']) {
145
                $error     = 'Magic method %s cannot be defined as static.';
146
                $errorCode = 'MethodStatic';
147
                if ( $this->magicMethods[$methodNameLc]['static'] === true ) {
148
                    $error     = 'Magic method %s must be defined as static.';
149
                    $errorCode = 'MethodNonStatic';
150
                }
151
                $data = array(
152
                    $methodName,
153
                );
154
155
                $phpcsFile->addError($error, $functionToken, $errorCode, $data);
156
            }
157
158
            // Advance to next function.
159
            $functionPtr = $scopeCloser;
160
        }
161
162
    }//end process()
163
164
165
}//end class
166