| Conditions | 6 |
| Paths | 7 |
| Total Lines | 56 |
| Code Lines | 31 |
| 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 |
||
| 46 | public static function run(string $module, string $controller, string $method): bool |
||
| 47 | { |
||
| 48 | Debug::initialize(); |
||
| 49 | |||
| 50 | $routes = Routes::getAllRoutes(); |
||
| 51 | $endpoint = $routes['Controller'][$module][$controller] ?? null; |
||
| 52 | if ($endpoint === null) { |
||
| 53 | return false; |
||
| 54 | } |
||
| 55 | |||
| 56 | Debug::message("Dispatcher::runWeb executing $module::$controller ($endpoint)"); |
||
| 57 | $route_array = explode('|', $endpoint); |
||
| 58 | $className = $route_array[0]; |
||
| 59 | $filename = $route_array[1]; |
||
| 60 | |||
| 61 | if (!file_exists($filename)) { |
||
| 62 | Debug::message("Dispatcher::runWeb error: $filename does not exists"); |
||
| 63 | new Error404Controller(); |
||
| 64 | return false; |
||
| 65 | } |
||
| 66 | |||
| 67 | require_once $filename; |
||
| 68 | |||
| 69 | $controller = new $className(); |
||
| 70 | if ($controller === null) { |
||
| 71 | Debug::message("Dispatcher::runApi error: $className not found"); |
||
| 72 | new Error404Controller(); |
||
| 73 | return false; |
||
| 74 | } |
||
| 75 | |||
| 76 | $templates_path = [ |
||
| 77 | constant('ALX_PATH') . '/src/Modules/' . $module . '/Templates/', |
||
| 78 | constant('BASE_PATH') . '/../Modules/' . $module . '/Templates/', |
||
| 79 | ]; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * If the class exists and is successfully instantiated, the module blade templates folder |
||
| 83 | * is added, if they exist. |
||
| 84 | */ |
||
| 85 | if (method_exists($controller, 'setTemplatesPath')) { |
||
| 86 | Debug::message('Templates: ' . $templates_path[0]); |
||
| 87 | Debug::message('Templates: ' . $templates_path[1]); |
||
| 88 | $controller->setTemplatesPath($templates_path); |
||
| 89 | } |
||
| 90 | |||
| 91 | if (!method_exists($controller, $method)) { |
||
| 92 | Debug::message('Method ' . $method . ' not found in controller ' . $className); |
||
| 93 | $method = 'index'; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Runs the index method to launch the controller. |
||
| 98 | */ |
||
| 99 | $controller->{$method}(); |
||
| 100 | |||
| 101 | return true; |
||
| 102 | } |
||
| 104 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.