Conditions | 10 |
Paths | 72 |
Total Lines | 73 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
118 | public function load($class) |
||
119 | { |
||
120 | // Separate the class name and its namespace |
||
121 | $parts = explode('\\', $class); |
||
122 | $className = array_pop($parts); |
||
123 | $dir = implode('/', $parts); |
||
124 | $paths = array(); |
||
125 | |||
126 | // Test for potential registered namespace to directory mappings |
||
127 | foreach ($this->namespaces as $registered) { |
||
128 | list($ns, $nsPaths) = $registered; |
||
129 | |||
130 | foreach ((array) $nsPaths as $nsPath) { |
||
131 | // Try without and with the autoloader's base path |
||
132 | $nsBasePaths = array(''); |
||
133 | |||
134 | if ($this->basePath) { |
||
135 | $nsBasePaths[] = $this->basePath . '/'; |
||
136 | } |
||
137 | |||
138 | foreach ($nsBasePaths as $nsBasePath) { |
||
139 | if ($class === $ns) { |
||
140 | array_push($paths, "$nsBasePath$nsPath"); |
||
141 | array_push($paths, "$nsBasePath$nsPath/$className.php"); |
||
142 | array_push($paths, "$nsBasePath" . strtolower($nsPath) . "/$className.php"); |
||
143 | } |
||
144 | |||
145 | if (strpos($class, $ns) === 0) { |
||
146 | array_push($paths, "$nsBasePath$nsPath/$dir/$className.php"); |
||
147 | array_push($paths, "$nsBasePath" . strtolower("$nsPath/$dir") . "/$className.php"); |
||
148 | |||
149 | $nsRemain = str_replace('\\', '/', substr($class, strlen($ns))); |
||
150 | array_push($paths, "$nsBasePath$nsPath/$nsRemain.php"); |
||
151 | array_push($paths, "$nsPath/$nsRemain.php"); |
||
152 | |||
153 | $nsRemainDir = dirname($nsRemain); |
||
154 | $nsRemainFile = basename($nsRemain); |
||
155 | array_push($paths, "$nsBasePath$nsPath/" . strtolower($nsRemainDir) . "/$nsRemainFile.php"); |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | // Try using the namespace as an exact directory mapping |
||
162 | array_push($paths, $this->basePath . "/$dir/$className.php"); |
||
163 | |||
164 | // Try using the namespace in lowercase as a directory mapping, with |
||
165 | // only the class name in its original case |
||
166 | $dirLowercase = strtolower($dir); |
||
167 | array_push($paths, $this->basePath . "/$dirLowercase/$className.php"); |
||
168 | |||
169 | // Last try using the last part of the namespace as a subdirectory, with |
||
170 | // and without a trailing 's', as well as any common subdirectory names |
||
171 | $subdirs = array_merge($this->commonSubdirs, array( |
||
172 | $className, |
||
173 | $className . 's', |
||
174 | )); |
||
175 | |||
176 | foreach ($subdirs as $subdir) { |
||
177 | array_push($paths, $this->basePath . "/$dir/$subdir/$className.php"); |
||
178 | |||
179 | $subdirLowercase = strtolower($subdir); |
||
180 | array_push($paths, $this->basePath . "/$dirLowercase/$subdirLowercase/$className.php"); |
||
181 | } |
||
182 | |||
183 | // Finally, attempt to find the class |
||
184 | foreach ($paths as $path) { |
||
185 | if ($this->attempt($path)) { |
||
186 | return true; |
||
187 | } |
||
188 | } |
||
189 | |||
190 | return false; |
||
191 | } |
||
194 |