Conditions | 14 |
Paths | 12 |
Total Lines | 74 |
Code Lines | 44 |
Lines | 9 |
Ratio | 12.16 % |
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 |
||
84 | protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) |
||
85 | { |
||
86 | $methodName = $phpcsFile->getDeclarationName($stackPtr); |
||
87 | if ($methodName === null) { |
||
88 | // Ignore closures. |
||
89 | return; |
||
90 | } |
||
91 | |||
92 | $className = $phpcsFile->getDeclarationName($currScope); |
||
93 | |||
94 | // Is this a magic method i.e. is prefixed with "__". |
||
95 | if (0 === strcmp(substr($methodName, 0, 2), '__')) { |
||
96 | $magicPart = substr($methodName, 2); |
||
97 | if (in_array($magicPart, self::$magicMethods) === false) { |
||
98 | $error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore"; |
||
|
|||
99 | $phpcsFile->addError($error, $stackPtr); |
||
100 | } |
||
101 | |||
102 | return; |
||
103 | } |
||
104 | |||
105 | // PHP4 constructors are allowed to break our rules. |
||
106 | if ($methodName === $className) { |
||
107 | return; |
||
108 | } |
||
109 | |||
110 | // PHP4 destructors are allowed to break our rules. |
||
111 | if ($methodName === '_'.$className) { |
||
112 | return; |
||
113 | } |
||
114 | |||
115 | if (0 !== strcmp($methodName, strtolower($methodName))) { |
||
116 | $uscrdMethodName = preg_replace('/([A-Z])/', '_${1}', $methodName); |
||
117 | $expectedMethodName = strtolower($uscrdMethodName); |
||
118 | $error = "Class methods should be entirely lowercased. Please consider \"$expectedMethodName\" instead of \"$methodName\"."; |
||
119 | $phpcsFile->addError($error, $stackPtr); |
||
120 | return; |
||
121 | } |
||
122 | |||
123 | $methodProps = $phpcsFile->getMethodProperties($stackPtr); |
||
124 | $scope = $methodProps['scope']; |
||
125 | $scopeSpecified = $methodProps['scope_specified']; |
||
126 | |||
127 | // If it's a private method, it must have an underscore on the front. |
||
128 | if ($scope === 'private' && $methodName{0} !== '_') { |
||
129 | $error = "Private method name \"$className::$methodName\" must be prefixed with an underscore"; |
||
130 | $phpcsFile->addError($error, $stackPtr); |
||
131 | return; |
||
132 | } |
||
133 | |||
134 | // If it's not a private method, it must not have an underscore on the front. |
||
135 | View Code Duplication | if ($scope !== 'private' && $methodName{0} === '_') { |
|
136 | if (true === $scopeSpecified) { |
||
137 | $error = "Public method name \"$className::$methodName\" must not be prefixed with an underscore"; |
||
138 | } else { |
||
139 | $error = ucfirst($scope)." method name \"$className::$methodName\" must not be prefixed with an underscore"; |
||
140 | } |
||
141 | $phpcsFile->addError($error, $stackPtr); |
||
142 | return; |
||
143 | } |
||
144 | |||
145 | // If name is too verbose, |
||
146 | // then either an error or a warning is displayed. |
||
147 | $error_limit = 50; |
||
148 | $warning_limit = 35; |
||
149 | if (strlen($methodName) > $error_limit) { |
||
150 | $error = "Overly long and verbose names are prohibited. Please find a name shorter than $error_limit chars."; |
||
151 | $phpcsFile->addError($error, $stackPtr); |
||
152 | return; |
||
153 | } else if (strlen($methodName) > $warning_limit) { |
||
154 | $warning = "Try to avoid overly long and verbose names in finding a name shorter than $warning_limit chars."; |
||
155 | $phpcsFile->addWarning($warning, $stackPtr); |
||
156 | } |
||
157 | }//end processTokenWithinScope() |
||
158 | |||
162 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.