Conditions | 9 |
Paths | 21 |
Total Lines | 78 |
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 |
||
43 | public function process() |
||
44 | { |
||
45 | // prepares renderer |
||
46 | $this->builder->setRenderer(new Twig($this->getAllLayoutsPaths(), $this->builder)); |
||
47 | |||
48 | // add globals variables |
||
49 | $this->addGlobals(); |
||
50 | |||
51 | call_user_func_array($this->builder->getMessageCb(), ['RENDER', 'Rendering pages']); |
||
52 | |||
53 | // collect published pages |
||
54 | /* @var $page Page */ |
||
55 | $filteredPages = $this->builder->getPages()->filter(function (Page $page) { |
||
56 | return !empty($page->getVariable('published')); |
||
57 | }); |
||
58 | $max = count($filteredPages); |
||
59 | |||
60 | // render each page |
||
61 | $count = 0; |
||
62 | /* @var $page Page */ |
||
63 | foreach ($filteredPages as $page) { |
||
64 | $count++; |
||
65 | $formats = ['html']; |
||
|
|||
66 | $rendered = null; |
||
67 | $alternates = []; |
||
68 | |||
69 | // get page's output formats |
||
70 | $formats = $this->getOutputFormats($page); |
||
71 | $page->setVariable('output', $formats); |
||
72 | |||
73 | // excluded format(s)? |
||
74 | foreach ($formats as $key => $format) { |
||
75 | if ($exclude = $this->config->get("site.output.formats.$format.exclude")) { |
||
76 | // ie: |
||
77 | // formats: |
||
78 | // atom: |
||
79 | // [...] |
||
80 | // exclude: [paginated] |
||
81 | foreach ($exclude as $variable) { |
||
82 | if ($page->hasVariable($variable)) { |
||
83 | unset($formats[$key]); |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | // get alternates links |
||
90 | $alternates = $this->getAlternates($formats); |
||
91 | $page->setVariable('alternates', $alternates); |
||
92 | |||
93 | // render each output format |
||
94 | foreach ($formats as $format) { |
||
95 | // search for the template |
||
96 | $layout = Layout::finder($page, $format, $this->config); |
||
97 | // render with Twig |
||
98 | try { |
||
99 | $rendered[$format]['output'] = $this->builder->getRenderer()->render( |
||
100 | $layout, |
||
101 | ['page' => $page] |
||
102 | ); |
||
103 | $rendered[$format]['template'] = $layout; |
||
104 | } catch (\Exception $e) { |
||
105 | throw new Exception(sprintf( |
||
106 | "Error in template \"%s\" for page \"%s\":\n%s", |
||
107 | $layout, |
||
108 | $page->getId(), |
||
109 | $e->getMessage() |
||
110 | )); |
||
111 | } |
||
112 | } |
||
113 | $page->setVariable('rendered', $rendered); |
||
114 | $this->builder->getPages()->replace($page->getId(), $page); |
||
115 | |||
116 | $layouts = implode(', ', array_column($rendered, 'template')); |
||
117 | $message = sprintf('%s [%s]', ($page->getId() ?: 'index'), $layouts); |
||
118 | call_user_func_array($this->builder->getMessageCb(), ['RENDER_PROGRESS', $message, $count, $max]); |
||
119 | } |
||
120 | } |
||
121 | |||
225 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.