Conditions | 10 |
Paths | 19 |
Total Lines | 42 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
113 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
114 | { |
||
115 | $interfaces = $this->findImplementedInterfaceNames($phpcsFile, $stackPtr); |
||
116 | |||
117 | if (is_array($interfaces) === false || $interfaces === array()) { |
||
118 | return; |
||
119 | } |
||
120 | |||
121 | $tokens = $phpcsFile->getTokens(); |
||
122 | $checkMethods = false; |
||
123 | |||
124 | if(isset($tokens[$stackPtr]['scope_closer'])) { |
||
125 | $checkMethods = true; |
||
126 | $scopeCloser = $tokens[$stackPtr]['scope_closer']; |
||
127 | } |
||
128 | |||
129 | foreach ($interfaces as $interface) { |
||
130 | $lcInterface = strtolower($interface); |
||
131 | if (isset($this->newInterfaces[$lcInterface]) === true) { |
||
132 | $this->addError($phpcsFile, $stackPtr, $interface); |
||
133 | } |
||
134 | |||
135 | if ($checkMethods === true && isset($this->unsupportedMethods[$lcInterface]) === true) { |
||
136 | $nextFunc = $stackPtr; |
||
137 | while (($nextFunc = $phpcsFile->findNext(T_FUNCTION, ($nextFunc + 1), $scopeCloser)) !== false) { |
||
|
|||
138 | $funcNamePos = $phpcsFile->findNext(T_STRING, $nextFunc); |
||
139 | $funcName = strtolower($tokens[$funcNamePos]['content']); |
||
140 | |||
141 | if (isset($this->unsupportedMethods[$lcInterface][$funcName]) === true) { |
||
142 | $error = 'Classes that implement interface %s do not support the method %s(). See %s'; |
||
143 | $data = array( |
||
144 | $interface, |
||
145 | $tokens[$funcNamePos]['content'], |
||
146 | $this->unsupportedMethods[$lcInterface][$funcName], |
||
147 | ); |
||
148 | $phpcsFile->addError($error, $funcNamePos, 'UnsupportedMethod', $data); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | |||
154 | }//end process() |
||
155 | |||
199 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: