| Conditions | 15 |
| Paths | 385 |
| Total Lines | 92 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 92 | public function loadTemplate($tpl = null) |
||
| 93 | { |
||
| 94 | // Uses the parent function if no framework suffix is present |
||
| 95 | if (RHtmlMedia::$frameworkSuffix == '') |
||
| 96 | { |
||
| 97 | return parent::loadTemplate($tpl); |
||
| 98 | } |
||
| 99 | |||
| 100 | // Clear prior output |
||
| 101 | $this->_output = null; |
||
| 102 | |||
| 103 | $template = JFactory::getApplication()->getTemplate(); |
||
| 104 | $layout = $this->getLayout(); |
||
| 105 | $layoutTemplate = $this->getLayoutTemplate(); |
||
| 106 | |||
| 107 | // Create the template file name based on the layout |
||
| 108 | $file = isset($tpl) ? $layout . '_' . $tpl : $layout; |
||
| 109 | |||
| 110 | // Clean the file name |
||
| 111 | $file = preg_replace('/[^A-Z0-9_\.-]/i', '', $file); |
||
| 112 | $tpl = isset($tpl) ? preg_replace('/[^A-Z0-9_\.-]/i', '', $tpl) : $tpl; |
||
| 113 | |||
| 114 | // Load the language file for the template |
||
| 115 | $lang = JFactory::getLanguage(); |
||
| 116 | $lang->load('tpl_' . $template, JPATH_BASE, null, false, true) |
||
| 117 | || $lang->load('tpl_' . $template, JPATH_THEMES . "/$template", null, false, true); |
||
| 118 | |||
| 119 | // Change the template folder if alternative layout is in different template |
||
| 120 | if (isset($layoutTemplate) && $layoutTemplate != '_' && $layoutTemplate != $template) |
||
| 121 | { |
||
| 122 | $this->_path['template'] = str_replace($template, $layoutTemplate, $this->_path['template']); |
||
| 123 | } |
||
| 124 | |||
| 125 | // Load the template script |
||
| 126 | jimport('joomla.filesystem.path'); |
||
| 127 | |||
| 128 | // Tries first with the framework suffix |
||
| 129 | $filetofind = $this->_createFileName('template', array('name' => $file . '.' . RHtmlMedia::$frameworkSuffix)); |
||
| 130 | $this->_template = JPath::find($this->_path['template'], $filetofind); |
||
| 131 | |||
| 132 | // If no framework-speficic is found, tries with the normal alternate layout |
||
| 133 | if ($this->_template == false) |
||
| 134 | { |
||
| 135 | $filetofind = $this->_createFileName('template', array('name' => $file)); |
||
| 136 | $this->_template = JPath::find($this->_path['template'], $filetofind); |
||
| 137 | } |
||
| 138 | |||
| 139 | // If alternate layout can't be found, fall back to default layout, first with framework suffix |
||
| 140 | if ($this->_template == false) |
||
| 141 | { |
||
| 142 | $filetofind = $this->_createFileName( |
||
| 143 | '', array('name' => 'default' . (isset($tpl) ? '_' . $tpl : $tpl) . '.' . RHtmlMedia::$frameworkSuffix) |
||
| 144 | ); |
||
| 145 | $this->_template = JPath::find($this->_path['template'], $filetofind); |
||
| 146 | } |
||
| 147 | |||
| 148 | // If no default with suffix is found, fall back to default layout without framework suffix |
||
| 149 | if ($this->_template == false) |
||
| 150 | { |
||
| 151 | $filetofind = $this->_createFileName('', array('name' => 'default' . (isset($tpl) ? '_' . $tpl : $tpl))); |
||
| 152 | $this->_template = JPath::find($this->_path['template'], $filetofind); |
||
| 153 | } |
||
| 154 | |||
| 155 | if ($this->_template != false) |
||
| 156 | { |
||
| 157 | // Unset so as not to introduce into template scope |
||
| 158 | unset($tpl); |
||
| 159 | unset($file); |
||
| 160 | |||
| 161 | // Never allow a 'this' property |
||
| 162 | if (isset($this->this)) |
||
| 163 | { |
||
| 164 | unset($this->this); |
||
| 165 | } |
||
| 166 | |||
| 167 | // Start capturing output into a buffer |
||
| 168 | ob_start(); |
||
| 169 | |||
| 170 | // Include the requested template filename in the local scope |
||
| 171 | // (this will execute the view logic). |
||
| 172 | include $this->_template; |
||
| 173 | |||
| 174 | // Done with the requested template; get the buffer and |
||
| 175 | // clear it. |
||
| 176 | $this->_output = ob_get_contents(); |
||
| 177 | ob_end_clean(); |
||
| 178 | |||
| 179 | return $this->_output; |
||
| 180 | } |
||
| 181 | else |
||
| 182 | { |
||
| 183 | throw new Exception(JText::sprintf('JLIB_APPLICATION_ERROR_LAYOUTFILE_NOT_FOUND', $file), 500); |
||
| 184 | } |
||
| 187 |