Conditions | 8 |
Paths | 21 |
Total Lines | 58 |
Code Lines | 35 |
Lines | 28 |
Ratio | 48.28 % |
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 |
||
41 | public static function compile(Compiler $compiler, $file) |
||
42 | { |
||
43 | $file = substr($file, 1, - 1); |
||
44 | |||
45 | if ($file === '') { |
||
46 | return ''; |
||
47 | } |
||
48 | |||
49 | View Code Duplication | if (preg_match('#^([a-z]{2,}):(.*)$#i', $file, $m)) { |
|
|
|||
50 | // resource:identifier given, extract them |
||
51 | $resource = $m[1]; |
||
52 | $identifier = $m[2]; |
||
53 | } else { |
||
54 | // get the current template's resource |
||
55 | $resource = $compiler->getDwoo()->getTemplate()->getResourceName(); |
||
56 | $identifier = $file; |
||
57 | } |
||
58 | |||
59 | $tpl = $compiler->getDwoo()->templateFactory($resource, $identifier); |
||
60 | |||
61 | View Code Duplication | if ($tpl === null) { |
|
62 | throw new CompilationException($compiler, |
||
63 | 'Load Templates : Resource "' . $resource . ':' . $identifier . '" not found.'); |
||
64 | } elseif ($tpl === false) { |
||
65 | throw new CompilationException($compiler, |
||
66 | 'Load Templates : Resource "' . $resource . '" does not support includes.'); |
||
67 | } |
||
68 | |||
69 | $cmp = clone $compiler; |
||
70 | $cmp->compile($compiler->getDwoo(), $tpl); |
||
71 | foreach ($cmp->getTemplatePlugins() as $template => $args) { |
||
72 | $compiler->addTemplatePlugin($template, $args['params'], $args['uuid'], $args['body']); |
||
73 | } |
||
74 | foreach ($cmp->getUsedPlugins() as $plugin => $type) { |
||
75 | $compiler->addUsedPlugin($plugin, $type); |
||
76 | } |
||
77 | |||
78 | $out = '\'\';// checking for modification in ' . $resource . ':' . $identifier . "\r\n"; |
||
79 | |||
80 | $modCheck = $tpl->getIsModifiedCode(); |
||
81 | |||
82 | View Code Duplication | if ($modCheck) { |
|
83 | $out .= 'if (!(' . $modCheck . ')) { ob_end_clean(); return false; }'; |
||
84 | } else { |
||
85 | $out .= 'try { |
||
86 | $tpl = $this->templateFactory("' . $resource . '", "' . $identifier . '"); |
||
87 | } catch (Dwoo\Exception $e) { |
||
88 | $this->triggerError(\'Load Templates : Resource <em>' . $resource . '</em> was not added to Dwoo, can not extend <em>' . $identifier . '</em>\', E_USER_WARNING); |
||
89 | } |
||
90 | if ($tpl === null) |
||
91 | $this->triggerError(\'Load Templates : Resource "' . $resource . ':' . $identifier . '" was not found.\', E_USER_WARNING); |
||
92 | elseif ($tpl === false) |
||
93 | $this->triggerError(\'Load Templates : Resource "' . $resource . '" does not support extends.\', E_USER_WARNING); |
||
94 | if ($tpl->getUid() != "' . $tpl->getUid() . '") { ob_end_clean(); return false; }'; |
||
95 | } |
||
96 | |||
97 | return $out; |
||
98 | } |
||
99 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.