Conditions | 11 |
Paths | 40 |
Total Lines | 82 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
45 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
46 | { |
||
47 | $tokens = $phpcsFile->getTokens(); |
||
48 | |||
49 | if ($this->supportsBelow('5.2')) { |
||
50 | $phpcsFile->addError( |
||
51 | 'Closures / anonymous functions are not available in PHP 5.2 or earlier', |
||
52 | $stackPtr, |
||
53 | 'Found' |
||
54 | ); |
||
55 | } |
||
56 | |||
57 | $isStatic = $this->isClosureStatic($phpcsFile, $stackPtr); |
||
58 | $usesThis = $this->findThisUsageInClosure($phpcsFile, $stackPtr); |
||
59 | |||
60 | if ($this->supportsBelow('5.3')) { |
||
61 | |||
62 | /* |
||
63 | * Closures can only be declared as static since PHP 5.4. |
||
64 | */ |
||
65 | if ($isStatic === true) { |
||
66 | $phpcsFile->addError( |
||
67 | 'Closures / anonymous functions could not be declared as static in PHP 5.3 or earlier', |
||
68 | $stackPtr, |
||
69 | 'StaticFound' |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | /* |
||
74 | * Closures declared within classes only have access to $this since PHP 5.4. |
||
75 | */ |
||
76 | if ($usesThis !== false) { |
||
77 | $thisFound = $usesThis; |
||
78 | do { |
||
79 | $phpcsFile->addError( |
||
80 | 'Closures / anonymous functions did not have access to $this in PHP 5.3 or earlier', |
||
81 | $thisFound, |
||
82 | 'ThisFound' |
||
83 | ); |
||
84 | |||
85 | $thisFound = $this->findThisUsageInClosure($phpcsFile, $stackPtr, ($thisFound + 1)); |
||
86 | |||
87 | } while($thisFound !== false); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /* |
||
92 | * Check for correct usage. |
||
93 | */ |
||
94 | if ($this->supportsAbove('5.4') && $usesThis !== false) { |
||
95 | |||
96 | $thisFound = $usesThis; |
||
97 | |||
98 | do { |
||
99 | /* |
||
100 | * Closures only have access to $this if not declared as static. |
||
101 | */ |
||
102 | if ($isStatic === true) { |
||
103 | $phpcsFile->addError( |
||
104 | 'Closures / anonymous functions declared as static do not have access to $this', |
||
105 | $thisFound, |
||
106 | 'ThisFoundInStatic' |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | /* |
||
111 | * Closures only have access to $this if used within a class context. |
||
112 | */ |
||
113 | else if ($this->inClassScope($phpcsFile, $stackPtr, false) === false) { |
||
114 | $phpcsFile->addError( |
||
115 | 'Closures / anonymous functions only have access to $this if used within a class', |
||
116 | $thisFound, |
||
117 | 'ThisFoundOutsideClass' |
||
118 | ); |
||
119 | } |
||
120 | |||
121 | $thisFound = $this->findThisUsageInClosure($phpcsFile, $stackPtr, ($thisFound + 1)); |
||
122 | |||
123 | } while($thisFound !== false); |
||
124 | } |
||
125 | |||
126 | }//end process() |
||
127 | |||
186 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.