Conditions | 21 |
Paths | 139 |
Total Lines | 100 |
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 |
||
148 | public function loadClass($className) |
||
149 | { |
||
150 | |||
151 | // Might the class be a omitted one? If so we can require the original. |
||
152 | if ($this->config->hasValue('autoloader/omit')) { |
||
153 | $omittedNamespaces = $this->config->getValue('autoloader/omit'); |
||
154 | |||
155 | foreach ($omittedNamespaces as $omitted) { |
||
156 | // If our class name begins with the omitted part e.g. it's namespace |
||
157 | if (strpos($className, str_replace('\\\\', '\\', $omitted)) === 0) { |
||
158 | return false; |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | |||
163 | // Do we have the file in our cache dir? If we are in development mode we have to ignore this. |
||
164 | if ($this->config->getValue('environment') !== 'development') { |
||
165 | $cachePath = $this->config->getValue('cache/dir') . DIRECTORY_SEPARATOR . str_replace('\\', '_', $className) . '.php'; |
||
166 | |||
167 | if (is_readable($cachePath)) { |
||
168 | $res = fopen($cachePath, 'r'); |
||
169 | $str = fread($res, 384); |
||
170 | |||
171 | $success = preg_match( |
||
172 | '/' . Dictionaries\Placeholders::ORIGINAL_PATH_HINT . '(.+)' . |
||
173 | Dictionaries\Placeholders::ORIGINAL_PATH_HINT . '/', |
||
174 | $str, |
||
175 | $tmp |
||
176 | ); |
||
177 | |||
178 | if ($success > 0) { |
||
179 | $tmp = explode('#', $tmp[1]); |
||
180 | |||
181 | $path = $tmp[0]; |
||
182 | $mTime = $tmp[1]; |
||
183 | |||
184 | if (filemtime($path) == $mTime) { |
||
185 | // the cached file is recent, load it |
||
186 | require $cachePath; |
||
187 | return true; |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | |||
193 | // If we are loading something that the autoloader needs to function, then we have to skip to composer |
||
194 | if ((strpos($className, 'AppserverIo\Doppelgaenger') === 0 && strpos($className, 'AppserverIo\Doppelgaenger\Tests') === false) || |
||
195 | strpos($className, 'PHP') === 0 || strpos($className, 'AppserverIo\Psr\MetaobjectProtocol') === 0 || |
||
196 | strpos($className, 'AppserverIo\Lang\\') === 0 |
||
197 | ) { |
||
198 | return false; |
||
199 | } |
||
200 | |||
201 | // If the structure map did not get filled by now we will do so here |
||
202 | if ($this->structureMap->isEmpty()) { |
||
203 | $this->structureMap->fill(); |
||
204 | } |
||
205 | |||
206 | // Get the file from the map |
||
207 | $file = $this->structureMap->getEntry($className); |
||
208 | |||
209 | // Did we get something? If not return false. |
||
210 | if ($file === false) { |
||
211 | return false; |
||
212 | } |
||
213 | |||
214 | // We are still here, so we know the class and it is not omitted. Does it contain annotations then? |
||
215 | if (!$file->hasAnnotations() || !$file->isEnforced()) { |
||
216 | // on un-enforced classes we will require the original |
||
217 | require $file->getPath(); |
||
218 | |||
219 | return true; |
||
220 | } |
||
221 | |||
222 | // So we have to create a new class definition for this original class. |
||
223 | // Get a current cache instance if we do not have one already. |
||
224 | if ($this->cache === null) { |
||
225 | // We also require the classes of our maps as we do not have proper autoloading in place |
||
226 | $this->cache = new CacheMap($this->getConfig()->getValue('cache/dir'), array(), $this->config); |
||
227 | } |
||
228 | $this->generator = new Generator($this->structureMap, $this->cache, $this->config, $this->aspectRegister); |
||
229 | |||
230 | // Create the new class definition |
||
231 | if ($this->generator->create($file, $this->config->getValue('enforcement/contract-inheritance')) === true) { |
||
232 | // Require the new class, it should have been created now |
||
233 | $file = $this->generator->getFileName($className); |
||
234 | |||
235 | if ($file !== false && is_readable($file) === true) { |
||
236 | require $file; |
||
237 | |||
238 | return true; |
||
239 | } |
||
240 | |||
241 | } else { |
||
242 | return false; |
||
243 | } |
||
244 | |||
245 | // Still here? That sounds like bad news! |
||
246 | return false; |
||
247 | } |
||
248 | |||
278 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.