| Conditions | 20 |
| Paths | 168 |
| Total Lines | 71 |
| 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 |
||
| 90 | private function compile(string $name, bool $processLang, bool $processInclude, bool $processExtends): string |
||
| 91 | { |
||
| 92 | if ($this->isRouteView) { |
||
| 93 | $this->isRouteView = false; |
||
| 94 | $path = ''; |
||
| 95 | $stack = debug_backtrace(); |
||
| 96 | foreach ($stack as $item) { |
||
| 97 | if (false !== stripos($item['file'], DIRECTORY_SEPARATOR . 'Route' . DIRECTORY_SEPARATOR)) { |
||
| 98 | $path = pathinfo($item['file'], PATHINFO_DIRNAME) . '/view.html.php'; |
||
| 99 | if ($processLang) { |
||
| 100 | $storagePath = str_replace('view.html.php', '_lang/' . $this->language . '/data.php', $path); |
||
| 101 | } |
||
| 102 | break; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } else { |
||
| 106 | $path = $this->packageRoot . '/view/' . $name; |
||
| 107 | if ($processLang) { |
||
| 108 | $storagePath = str_replace('view.html.php', '', $path) . '_lang/' . $this->language . '/data.php'; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | if (file_exists($path)) { |
||
| 113 | ob_start(); |
||
| 114 | readfile($path); |
||
| 115 | $code = ob_get_clean(); |
||
| 116 | } else { |
||
| 117 | throw new FileNotFoundException($path); |
||
| 118 | } |
||
| 119 | |||
| 120 | if ($processLang && file_exists($storagePath)) { |
||
| 121 | $storage = include $storagePath; |
||
|
|
|||
| 122 | preg_match_all('/<!-- lang (.*) -->/', $code, $matchList); |
||
| 123 | if (isset($matchList[1])) { |
||
| 124 | foreach ($matchList[1] as $key => $index) { |
||
| 125 | $name = explode('>', $index); |
||
| 126 | $default = trim($name[1] ?? ''); |
||
| 127 | $name = trim($name[0]); |
||
| 128 | if (!empty($matchList[0][$key]) && false !== strpos($code, $matchList[0][$key])) { |
||
| 129 | $code = str_replace($matchList[0][$key], $storage[$name] ?? $default, $code); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | if ($processInclude) { |
||
| 136 | preg_match_all('/<!-- include (.*) -->/', $code, $matchList); |
||
| 137 | if (isset($matchList[1])) { |
||
| 138 | foreach ($matchList[1] as $key => $template) { |
||
| 139 | if (!empty($matchList[0][$key]) && false !== strpos($code, $matchList[0][$key])) { |
||
| 140 | $template = trim($template); |
||
| 141 | $code = str_replace($matchList[0][$key], $this->compile($template . '/view.html.php', true, true, false), $code); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($processExtends) { |
||
| 148 | preg_match_all('/<!-- extends (.*) -->/', $code, $matchList); |
||
| 149 | if (isset($matchList[1][0])) { |
||
| 150 | $template = trim($matchList[1][0]); |
||
| 151 | $parentHtml = $this->compile($template . '/view.html.php', true, true, false); |
||
| 152 | |||
| 153 | $code = str_replace($matchList[0][0], '', $code); |
||
| 154 | $parentHtml = str_replace('<!-- section -->', $code, $parentHtml); |
||
| 155 | $code = $parentHtml; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | return $code; |
||
| 160 | } |
||
| 161 | |||
| 183 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: