| Conditions | 14 |
| Paths | 21 |
| Total Lines | 85 |
| Lines | 34 |
| Ratio | 40 % |
| 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 |
||
| 102 | protected function _getAssetParentId(JTable $table = null, $id = null) |
||
| 103 | { |
||
| 104 | // Initialise variables. |
||
| 105 | $db = $this->getDbo(); |
||
|
|
|||
| 106 | |||
| 107 | // Build the query to get the asset id for the parent category. |
||
| 108 | $asset = JTable::getInstance('asset'); |
||
| 109 | $name = basename($this->path); |
||
| 110 | $relativePath = substr($this->path, strlen(JPATH_ROOT)); |
||
| 111 | |||
| 112 | if (preg_match('/^([^.]*)\..*\.ini$/', $name, $matches) || preg_match('/^([^.]*)\.ini$/', $name, $matches)) |
||
| 113 | { |
||
| 114 | $params = JComponentHelper::getParams('com_localise'); |
||
| 115 | $installation_folder = $params->get('installation', 'installation'); |
||
| 116 | $tag = $matches[1]; |
||
| 117 | |||
| 118 | if (preg_match('#^/(administrator|plugins)#', $relativePath)) |
||
| 119 | { |
||
| 120 | $id = LocaliseHelper::getFileId(JPATH_ROOT . "/administrator/language/$tag/$tag.xml"); |
||
| 121 | } |
||
| 122 | elseif (preg_match('#^/' . $installation_folder . '#', $relativePath)) |
||
| 123 | { |
||
| 124 | $id = LocaliseHelper::getFileId(LOCALISEPATH_INSTALLATION . "/language/$tag/$tag.xml"); |
||
| 125 | } |
||
| 126 | else |
||
| 127 | { |
||
| 128 | $id = LocaliseHelper::getFileId(JPATH_ROOT . "/language/$tag/$tag.xml"); |
||
| 129 | } |
||
| 130 | |||
| 131 | $assetName = "com_localise.$id"; |
||
| 132 | |||
| 133 | if (!$asset->loadByName($assetName)) |
||
| 134 | { |
||
| 135 | $component = JTable::getInstance('asset'); |
||
| 136 | |||
| 137 | View Code Duplication | if (!$component->loadByName('com_localise')) |
|
| 138 | { |
||
| 139 | $root = JTable::getInstance('asset'); |
||
| 140 | $root->rebuild(); |
||
| 141 | $root->loadByName('root.1'); |
||
| 142 | $component->name = 'com_localise'; |
||
| 143 | $component->title = 'com_localise'; |
||
| 144 | $component->setLocation($root->id, 'last-child'); |
||
| 145 | |||
| 146 | if (!$component->check() || !$component->store()) |
||
| 147 | { |
||
| 148 | $this->setError($component->getError()); |
||
| 149 | |||
| 150 | return false; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | $asset->name = "com_localise.$id"; |
||
| 155 | $asset->title = $name; |
||
| 156 | $asset->setLocation($component->id, 'last-child'); |
||
| 157 | |||
| 158 | if (!$asset->check() || !$asset->store()) |
||
| 159 | { |
||
| 160 | $this->setError($asset->getError()); |
||
| 161 | |||
| 162 | return false; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | View Code Duplication | else |
|
| 167 | { |
||
| 168 | if (!$asset->loadByName('com_localise')) |
||
| 169 | { |
||
| 170 | $root = JTable::getInstance('asset'); |
||
| 171 | $root->loadByName('root.1'); |
||
| 172 | $asset->name = 'com_localise'; |
||
| 173 | $asset->title = 'com_localise'; |
||
| 174 | $asset->setLocation($root->id, 'last-child'); |
||
| 175 | |||
| 176 | if (!$asset->check() || !$asset->store()) |
||
| 177 | { |
||
| 178 | $this->setError($asset->getError()); |
||
| 179 | |||
| 180 | return false; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | return $asset->id; |
||
| 186 | } |
||
| 187 | |||
| 250 |
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.