| Conditions | 13 |
| Paths | 161 |
| Total Lines | 102 |
| 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 |
||
| 44 | public function process() |
||
| 45 | { |
||
| 46 | // prepares renderer |
||
| 47 | $this->builder->setRenderer(new Twig($this->getAllLayoutsPaths(), $this->builder)); |
||
| 48 | |||
| 49 | // add globals variables |
||
| 50 | $this->addGlobals(); |
||
| 51 | |||
| 52 | call_user_func_array($this->builder->getMessageCb(), ['RENDER', 'Rendering pages']); |
||
| 53 | |||
| 54 | // collect published pages |
||
| 55 | /* @var $page Page */ |
||
| 56 | $filteredPages = $this->builder->getPages()->filter(function (Page $page) { |
||
| 57 | return !empty($page->getVariable('published')); |
||
| 58 | }); |
||
| 59 | $max = count($filteredPages); |
||
| 60 | |||
| 61 | // render each page |
||
| 62 | $count = 0; |
||
| 63 | /* @var $page Page */ |
||
| 64 | foreach ($filteredPages as $page) { |
||
| 65 | $count++; |
||
| 66 | $formats = ['html']; |
||
|
|
|||
| 67 | $rendered = []; |
||
| 68 | |||
| 69 | // i18n |
||
| 70 | $pageLang = $page->getVariable('language'); |
||
| 71 | $locale = $this->config->getLanguageProperty('locale', $pageLang); |
||
| 72 | // The PHP Intl extension is needed to use localized date |
||
| 73 | if (extension_loaded('intl')) { |
||
| 74 | \Locale::setDefault($locale); |
||
| 75 | } |
||
| 76 | // The PHP Gettext extension is needed to use translation |
||
| 77 | if (extension_loaded('gettext')) { |
||
| 78 | $localePath = realpath($this->config->getSourceDir().'/locale'); |
||
| 79 | $domain = 'messages'; |
||
| 80 | putenv("LC_ALL=$locale"); |
||
| 81 | putenv("LANGUAGE=$locale"); |
||
| 82 | setlocale(LC_ALL, "$locale.UTF-8"); |
||
| 83 | bindtextdomain($domain, $localePath); |
||
| 84 | } |
||
| 85 | |||
| 86 | // global site variables |
||
| 87 | $this->builder->getRenderer() |
||
| 88 | ->addGlobal('site', new Site($this->builder)); |
||
| 89 | // specific language: replace global site variables |
||
| 90 | if (!empty($pageLang) && $pageLang != $this->config->get('language')) { |
||
| 91 | $this->builder->getRenderer() |
||
| 92 | ->addGlobal('site', new Site($this->builder, $pageLang)); |
||
| 93 | } |
||
| 94 | |||
| 95 | // get page's output formats |
||
| 96 | $formats = $this->getOutputFormats($page); |
||
| 97 | $page->setVariable('output', $formats); |
||
| 98 | |||
| 99 | // excluded format(s)? |
||
| 100 | foreach ($formats as $key => $format) { |
||
| 101 | if ($exclude = $this->config->get("site.output.formats.$format.exclude")) { |
||
| 102 | // ie: |
||
| 103 | // formats: |
||
| 104 | // atom: |
||
| 105 | // [...] |
||
| 106 | // exclude: [paginated] |
||
| 107 | foreach ($exclude as $variable) { |
||
| 108 | if ($page->hasVariable($variable)) { |
||
| 109 | unset($formats[$key]); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | // get and set alternates links |
||
| 116 | $page->setVariable('alternates', $this->getAlternates($formats)); |
||
| 117 | |||
| 118 | // render each output format |
||
| 119 | foreach ($formats as $format) { |
||
| 120 | // search for the template |
||
| 121 | $layout = Layout::finder($page, $format, $this->config); |
||
| 122 | // render with Twig |
||
| 123 | try { |
||
| 124 | $rendered[$format]['output'] = $this->builder->getRenderer()->render( |
||
| 125 | $layout, |
||
| 126 | ['page' => $page] |
||
| 127 | ); |
||
| 128 | $rendered[$format]['template'] = $layout; |
||
| 129 | } catch (\Exception $e) { |
||
| 130 | throw new Exception(sprintf( |
||
| 131 | "Error in template \"%s\" for page \"%s\":\n%s", |
||
| 132 | $layout, |
||
| 133 | $page->getId(), |
||
| 134 | $e->getMessage() |
||
| 135 | )); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | $page->setVariable('rendered', $rendered); |
||
| 139 | $this->builder->getPages()->replace($page->getId(), $page); |
||
| 140 | |||
| 141 | $layouts = implode(', ', array_column($rendered, 'template')); |
||
| 142 | $message = sprintf('%s [%s]', ($page->getId() ?: 'index'), $layouts); |
||
| 143 | call_user_func_array($this->builder->getMessageCb(), ['RENDER_PROGRESS', $message, $count, $max]); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 243 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.