Conditions | 25 |
Paths | 330 |
Total Lines | 86 |
Lines | 23 |
Ratio | 26.74 % |
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 |
||
99 | private function compile(string $name, bool $processLang, bool $processInclude, bool $processExtends): string |
||
100 | { |
||
101 | if ($this->isRouteView) { |
||
102 | $this->isRouteView = false; |
||
103 | $path = ''; |
||
104 | $stack = debug_backtrace(); |
||
105 | foreach ($stack as $item) { |
||
106 | if (false !== stripos($item['file'], DIRECTORY_SEPARATOR . 'Route' . DIRECTORY_SEPARATOR)) { |
||
107 | $path = pathinfo($item['file'], PATHINFO_DIRNAME) . '/view.html.php'; |
||
108 | if ($processLang) { |
||
109 | $storagePath = str_replace('view.html.php', '_lang/' . $this->language . '.php', $path); |
||
110 | } |
||
111 | break; |
||
112 | } |
||
113 | } |
||
114 | } else { |
||
115 | $path = $this->packageRoot . '/view/' . $name; |
||
116 | if ($processLang) { |
||
117 | $storagePath = str_replace('view.html.php', '', $path) . '_lang/' . $this->language . '.php'; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | if (file_exists($path)) { |
||
122 | ob_start(); |
||
123 | readfile($path); |
||
124 | $code = ob_get_clean(); |
||
125 | } else { |
||
126 | throw new FileNotFoundException($path); |
||
127 | } |
||
128 | |||
129 | if ($processLang && file_exists($storagePath)) { |
||
130 | $storage = include $storagePath; |
||
|
|||
131 | if ('' == $this->title) { |
||
132 | $this->title = $storage['title'] ?? ''; |
||
133 | } |
||
134 | preg_match_all('/<!-- lang (.*) -->/', $code, $matchList); |
||
135 | View Code Duplication | if (isset($matchList[1])) { |
|
136 | foreach ($matchList[1] as $key => $index) { |
||
137 | $name = explode('>', $index); |
||
138 | $default = trim($name[1] ?? ''); |
||
139 | $name = trim($name[0]); |
||
140 | if (!empty($matchList[0][$key]) && false !== strpos($code, $matchList[0][$key])) { |
||
141 | $code = str_replace($matchList[0][$key], $storage[$name] ?? $default, $code); |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | View Code Duplication | } else { |
|
146 | preg_match_all('/<!-- lang (.*) -->/', $code, $matchList); |
||
147 | if (isset($matchList[1])) { |
||
148 | foreach ($matchList[1] as $key => $index) { |
||
149 | $name = explode('>', $index); |
||
150 | $default = trim($name[1] ?? ''); |
||
151 | $name = trim($name[0]); |
||
152 | if (!empty($matchList[0][$key]) && false !== strpos($code, $matchList[0][$key])) { |
||
153 | $code = str_replace($matchList[0][$key], $this->$name ?? $default, $code); |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | if ($processInclude) { |
||
160 | preg_match_all('/<!-- include (.*) -->/', $code, $matchList); |
||
161 | if (isset($matchList[1])) { |
||
162 | foreach ($matchList[1] as $key => $template) { |
||
163 | if (!empty($matchList[0][$key]) && false !== strpos($code, $matchList[0][$key])) { |
||
164 | $template = trim($template); |
||
165 | $code = str_replace($matchList[0][$key], $this->compile($template . '/view.html.php', true, true, false), $code); |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | if ($processExtends) { |
||
172 | preg_match_all('/<!-- extends (.*) -->/', $code, $matchList); |
||
173 | if (isset($matchList[1][0])) { |
||
174 | $template = trim($matchList[1][0]); |
||
175 | $parentHtml = $this->compile($template . '/view.html.php', true, true, false); |
||
176 | |||
177 | $code = str_replace($matchList[0][0], '', $code); |
||
178 | $parentHtml = str_replace('<!-- section -->', $code, $parentHtml); |
||
179 | $code = $parentHtml; |
||
180 | } |
||
181 | } |
||
182 | |||
183 | return $code; |
||
184 | } |
||
185 | |||
207 | } |
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: