| Conditions | 12 |
| Paths | 225 |
| Total Lines | 102 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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 |
||
| 49 | public function process() |
||
| 50 | { |
||
| 51 | // prepares renderer |
||
| 52 | $this->builder->setRenderer(new Twig($this->getAllLayoutsPaths(), $this->builder)); |
||
| 53 | |||
| 54 | // adds global variables |
||
| 55 | $this->addGlobals(); |
||
| 56 | |||
| 57 | call_user_func_array($this->builder->getMessageCb(), ['RENDER', 'Rendering pages']); |
||
| 58 | |||
| 59 | // collects published pages |
||
| 60 | /** @var Page $page */ |
||
| 61 | $filteredPages = $this->builder->getPages()->filter(function (Page $page) { |
||
| 62 | return !empty($page->getVariable('published')); |
||
| 63 | }); |
||
| 64 | $max = count($filteredPages); |
||
| 65 | |||
| 66 | // renders each page |
||
| 67 | $count = 0; |
||
| 68 | /** @var Page $page */ |
||
| 69 | foreach ($filteredPages as $page) { |
||
| 70 | $count++; |
||
| 71 | $rendered = []; |
||
| 72 | |||
| 73 | // i18n |
||
| 74 | $pageLang = $page->getVariable('language'); |
||
| 75 | $locale = $this->config->getLanguageProperty('locale', $pageLang); |
||
| 76 | // the PHP Intl extension is needed to use localized date |
||
| 77 | if (extension_loaded('intl')) { |
||
| 78 | \Locale::setDefault($locale); |
||
| 79 | } |
||
| 80 | // the PHP Gettext extension is needed to use translation |
||
| 81 | if (extension_loaded('gettext')) { |
||
| 82 | $localePath = realpath(Util::joinFile($this->config->getSourceDir(), 'locale')); |
||
| 83 | $domain = 'messages'; |
||
| 84 | putenv("LC_ALL=$locale"); |
||
| 85 | putenv("LANGUAGE=$locale"); |
||
| 86 | setlocale(LC_ALL, "$locale.UTF-8"); |
||
| 87 | bindtextdomain($domain, $localePath); |
||
| 88 | } |
||
| 89 | |||
| 90 | // global site variables |
||
| 91 | $this->builder->getRenderer()->addGlobal('site', new Site($this->builder, $pageLang)); |
||
| 92 | |||
| 93 | // get Page's output formats |
||
| 94 | $formats = $this->getOutputFormats($page); |
||
| 95 | $page->setVariable('output', $formats); |
||
| 96 | |||
| 97 | // excluded format(s)? |
||
| 98 | foreach ($formats as $key => $format) { |
||
| 99 | if ($exclude = $this->config->getOutputFormatProperty($format, 'exclude')) { |
||
| 100 | // ie: |
||
| 101 | // formats: |
||
| 102 | // atom: |
||
| 103 | // [...] |
||
| 104 | // exclude: [paginated] |
||
| 105 | if (!is_array($exclude)) { |
||
| 106 | $exclude = [$exclude]; |
||
| 107 | } |
||
| 108 | foreach ($exclude as $variable) { |
||
| 109 | if ($page->hasVariable($variable)) { |
||
| 110 | unset($formats[$key]); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | // get and set alternates links |
||
| 117 | $page->setVariable('alternates', $this->getAlternates($formats)); |
||
| 118 | |||
| 119 | // renders each output format |
||
| 120 | foreach ($formats as $format) { |
||
| 121 | // search for the template |
||
| 122 | $layout = Layout::finder($page, $format, $this->config); |
||
| 123 | // renders with Twig |
||
| 124 | try { |
||
| 125 | $output = $this->builder->getRenderer()->render($layout['file'], ['page' => $page]); |
||
| 126 | $output = $this->postProcessOutput($output, $page, $format); |
||
| 127 | $rendered[$format]['output'] = $output; |
||
| 128 | $rendered[$format]['template']['scope'] = $layout['scope']; |
||
| 129 | $rendered[$format]['template']['file'] = $layout['file']; |
||
| 130 | } catch (\Twig\Error\Error $e) { |
||
| 131 | throw new Exception(sprintf( |
||
| 132 | "%s\nChecks template \"%s:%s\" line %s, used by page \"%s\".", |
||
| 133 | $e->getMessage(), |
||
| 134 | $layout['scope'], |
||
| 135 | $layout['file'], |
||
| 136 | $e->getLine(), |
||
| 137 | $page->getId() |
||
| 138 | )); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | $page->setVariable('rendered', $rendered); |
||
| 142 | $this->builder->getPages()->replace($page->getId(), $page); |
||
| 143 | |||
| 144 | $templates = array_column($rendered, 'template'); |
||
| 145 | $message = sprintf( |
||
| 146 | '%s [%s]', |
||
| 147 | ($page->getId() ?: 'index'), |
||
| 148 | Util::combineArrayToString($templates, 'scope', 'file') |
||
| 149 | ); |
||
| 150 | call_user_func_array($this->builder->getMessageCb(), ['RENDER_PROGRESS', $message, $count, $max]); |
||
| 151 | } |
||
| 297 |