| Conditions | 13 |
| Paths | 25 |
| Total Lines | 38 |
| Code Lines | 19 |
| Lines | 12 |
| Ratio | 31.58 % |
| 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 |
||
| 39 | public function executeFilter(HttpRequestInterface $request, HttpResponseInterface $response) |
||
| 40 | { |
||
| 41 | /* |
||
| 42 | * Deny service, if the system load is too high. |
||
| 43 | */ |
||
| 44 | if (defined('DEBUG') and DEBUG === false) { |
||
|
|
|||
| 45 | $maxServerLoad = isset(self::$config['load']['max']) ? (float) self::$config['load']['max'] : 80; |
||
| 46 | |||
| 47 | if (\Koch\Functions\Functions::getServerLoad() > $maxServerLoad) { |
||
| 48 | $response->addHeader('Retry-After', mt_rand(45, 90)); |
||
| 49 | $response->setStatusCode('503'); |
||
| 50 | $response->addContent('HTTP/1.1 503 Server too busy. Please try again later.'); |
||
| 51 | $response->sendResponse(); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | // ensure smarty "tpl_compile" folder exists |
||
| 56 | View Code Duplication | if (false === is_dir(APPLICATION_CACHE_PATH . 'tpl_compile') and |
|
| 57 | (false === @mkdir(APPLICATION_CACHE_PATH . 'tpl_compile', 0755, true))) { |
||
| 58 | throw new Exception('Smarty Template Directories not existant.', 9); |
||
| 59 | } |
||
| 60 | |||
| 61 | // ensure smarty "cache" folder exists |
||
| 62 | View Code Duplication | if (false === is_dir(APPLICATION_CACHE_PATH . 'tpl_cache') and |
|
| 63 | (false === @mkdir(APPLICATION_CACHE_PATH . 'tpl_cache', 0755, true))) { |
||
| 64 | throw new Exception('Smarty Template Directories not existant.', 9); |
||
| 65 | } |
||
| 66 | |||
| 67 | // ensure smarty folders are writable |
||
| 68 | if (false === is_writable(APPLICATION_CACHE_PATH . 'tpl_compile') or |
||
| 69 | (false === is_writable(APPLICATION_CACHE_PATH . 'tpl_cache'))) { |
||
| 70 | // if not, try to set writeable permission on the folders |
||
| 71 | View Code Duplication | if ((false === chmod(APPLICATION_CACHE_PATH . 'tpl_compile', 0755)) and |
|
| 72 | (false === chmod(APPLICATION_CACHE_PATH . 'tpl_cache', 0755))) { |
||
| 73 | throw new Exception('Smarty Template Directories not writable.', 10); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.