| Conditions | 41 |
| Paths | > 20000 |
| Total Lines | 198 |
| Code Lines | 125 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 1722 |
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 |
||
| 83 | function parse($filePath, $vardefs = array(), $moduleDir = '', $merge=false, $masterCopy=null) { |
||
| 84 | |||
| 85 | global $app_strings; |
||
| 86 | $contents = file_get_contents($filePath); |
||
| 87 | |||
| 88 | // The contents are not well formed so we add this section to make it easier to parse |
||
| 89 | $contents = $this->trimHTML($contents) . '</td></tr></table>'; |
||
| 90 | $moduleName = ''; |
||
| 91 | |||
| 92 | $forms = $this->getElementsByType("form", $contents); |
||
| 93 | $tables = $this->getElementsByType("table", $forms[0] . "</td></tr></table>"); |
||
| 94 | $mainrow = $this->getElementsByType("tr", $tables[1]); |
||
| 95 | $rows = substr($mainrow[0], strpos($mainrow[0], "</tr>")); |
||
| 96 | $tablerows = $this->getElementsByType("tr", $rows); |
||
| 97 | |||
| 98 | foreach($tablerows as $trow) { |
||
| 99 | |||
| 100 | $emptyCount = 0; |
||
| 101 | $tablecolumns = $this->getElementsByType("td", $trow); |
||
| 102 | $col = array(); |
||
| 103 | $slot = 0; |
||
| 104 | |||
| 105 | foreach($tablecolumns as $tcols) { |
||
| 106 | |||
| 107 | $sugarAttrLabel = $this->getTagAttribute("sugar", $tcols, "'^slot[^b]+$'"); |
||
| 108 | $sugarAttrValue = $this->getTagAttribute("sugar", $tcols, "'slot[0-9]+b$'"); |
||
| 109 | |||
| 110 | // If there wasn't any slot numbering/lettering then just default to expect label->vallue pairs |
||
| 111 | $sugarAttrLabel = count($sugarAttrLabel) != 0 ? $sugarAttrLabel : ($slot % 2 == 0) ? true : false; |
||
| 112 | $sugarAttrValue = count($sugarAttrValue) != 0 ? $sugarAttrValue : ($slot % 2 == 1) ? true : false; |
||
| 113 | |||
| 114 | $slot++; |
||
| 115 | |||
| 116 | if($sugarAttrValue) { |
||
| 117 | |||
| 118 | $spanValue = strtolower($this->getElementValue("span", $tcols)); |
||
| 119 | if(empty($spanValue)) { |
||
| 120 | $spanValue = strtolower($this->getElementValue("slot", $tcols)); |
||
| 121 | } |
||
| 122 | if(empty($spanValue)) { |
||
| 123 | $spanValue = strtolower($this->getElementValue("td", $tcols)); |
||
| 124 | } |
||
| 125 | |||
| 126 | //Get all the editable form elements' names |
||
| 127 | $formElementNames = $this->getFormElementsNames($spanValue); |
||
| 128 | $customField = $this->getCustomField($formElementNames); |
||
| 129 | |||
| 130 | $name = ''; |
||
| 131 | $readOnly = false; |
||
| 132 | $fields = null; |
||
| 133 | $customCode = null; |
||
| 134 | |||
| 135 | if(!empty($customField)) { |
||
| 136 | // If it's a custom field we just set the name |
||
| 137 | $name = $customField; |
||
| 138 | |||
| 139 | } else if(empty($formElementNames) && preg_match_all('/[\{]([^\}]*?)[\}]/s', $spanValue, $matches, PREG_SET_ORDER)) { |
||
| 140 | // We are here if the $spanValue did not contain a form element for editing. |
||
| 141 | // We will assume that it is read only (since there were no edit form elements) |
||
| 142 | |||
| 143 | |||
| 144 | // If there is more than one matching {} value then try to find the right one to key off |
||
| 145 | // based on vardefs.php file. Also, use the entire spanValue as customCode |
||
| 146 | if(count($matches) > 1) { |
||
| 147 | $name = $matches[0][1]; |
||
| 148 | $customCode = $spanValue; |
||
| 149 | foreach($matches as $pair) { |
||
| 150 | if(preg_match("/^(mod[\.]|app[\.]).*?/s", $pair[1])) { |
||
| 151 | $customCode = str_replace($pair[1], '$'.strtoupper($pair[1]), $customCode); |
||
| 152 | } else if(!empty($vardefs[$pair[1]])) { |
||
| 153 | $name = $pair[1]; |
||
| 154 | $customCode = str_replace($pair[1], '$fields.'.$pair[1].'.value', $customCode); |
||
| 155 | } |
||
| 156 | } //foreach |
||
| 157 | } else { |
||
| 158 | //If it is only a label, skip |
||
| 159 | if(preg_match("/^(mod[\.]|app[\.]).*?/s", $matches[0][1])) { |
||
| 160 | continue; |
||
| 161 | } else if(preg_match("/^[\$].*?/s", $matches[0][1])) { |
||
| 162 | $name = '{' . strtoupper($matches[0][1]) . '}'; |
||
| 163 | } else { |
||
| 164 | $name = $matches[0][1]; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | $readOnly = true; |
||
| 169 | } else if(is_array($formElementNames)) { |
||
| 170 | |||
| 171 | if(count($formElementNames) == 1) { |
||
| 172 | if(!empty($vardefs[$formElementNames[0]])) { |
||
| 173 | $name = $formElementNames[0]; |
||
| 174 | } |
||
| 175 | } else { |
||
| 176 | $fields = array(); |
||
| 177 | foreach($formElementNames as $elementName) { |
||
| 178 | // What we are doing here is saying that we will add all your fields assuming |
||
| 179 | // there are none that are of type relate or link. However, if we find such a type |
||
| 180 | // we'll take the first one found and assume that is the field you want (the SugarFields |
||
| 181 | // library will handle rendering the popup and select and clear buttons for you). |
||
| 182 | if(isset($vardefs[$elementName])) { |
||
| 183 | $type = $vardefs[$elementName]['type']; |
||
| 184 | if($type != 'relate' && $type != 'link') { |
||
| 185 | $fields[] = $elementName; |
||
| 186 | $name = $elementName; |
||
| 187 | } else { |
||
| 188 | unset($fields); |
||
| 189 | $name = $elementName; |
||
| 190 | break; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } //if-else |
||
| 195 | } |
||
| 196 | |||
| 197 | // Build the entry |
||
| 198 | if(preg_match("/<textarea/si", $spanValue)) { |
||
| 199 | //special case for textarea form elements (add the displayParams) |
||
| 200 | $displayParams = array(); |
||
| 201 | $displayParams['rows'] = $this->getTagAttribute("rows", $spanValue); |
||
| 202 | $displayParams['cols'] = $this->getTagAttribute("cols", $spanValue); |
||
| 203 | |||
| 204 | if(!empty($displayParams['rows']) && !empty($displayParams['cols'])) { |
||
| 205 | $field = array(); |
||
| 206 | $field['name'] = $name; |
||
| 207 | $field['displayParams'] = $displayParams; |
||
| 208 | } else { |
||
| 209 | $field = $name; |
||
| 210 | } |
||
| 211 | $col[] = $field; |
||
| 212 | } else if($readOnly) { |
||
| 213 | $field = array(); |
||
| 214 | $field['name'] = $name; |
||
| 215 | $field['type'] = 'readonly'; |
||
| 216 | if(isset($customCode)) { |
||
| 217 | $field['customCode'] = $customCode; |
||
| 218 | } //if |
||
| 219 | $col[] = $field; |
||
| 220 | } else { |
||
| 221 | |||
| 222 | if(isset($fields) || isset($customCode)) { |
||
| 223 | $field = array(); |
||
| 224 | $field['name'] = $name; |
||
| 225 | if(isset($fields)) { |
||
| 226 | $field['fields'] = $fields; |
||
| 227 | } |
||
| 228 | if(isset($customCode)) { |
||
| 229 | $field['customCode'] = $customCode; |
||
| 230 | } |
||
| 231 | |||
| 232 | $col[] = $field; |
||
| 233 | } else { |
||
| 234 | $emptyCount = $name == '' ? $emptyCount + 1 : $emptyCount; |
||
| 235 | $col[] = $name; |
||
| 236 | } |
||
| 237 | } //if-else if-else block |
||
| 238 | } //if($sugarAttrValue) |
||
| 239 | } //foreach |
||
| 240 | |||
| 241 | // One last final check. If $emptyCount does not equal Array $col count, don't add |
||
| 242 | if($emptyCount != count($col)) { |
||
| 243 | $metarow[] = $col; |
||
| 244 | } //if |
||
| 245 | } //foreach |
||
| 246 | |||
| 247 | $templateMeta = array(); |
||
| 248 | $templateMeta['form']['buttons'] = 'button'; |
||
| 249 | |||
| 250 | preg_match_all("/(<input[^>]*?)>/si", $tables[0], $matches); |
||
| 251 | $buttons = array(); |
||
| 252 | foreach($matches[0] as $button) { |
||
| 253 | $buttons[] = array('customCode'=>$button); |
||
| 254 | } |
||
| 255 | $templateMeta['form']['buttons'] = $buttons; |
||
| 256 | |||
| 257 | $formElements = $this->getFormElements($contents); |
||
| 258 | $hiddenInputs = array(); |
||
| 259 | foreach($formElements as $elem) { |
||
| 260 | $type = $this->getTagAttribute("type", $elem); |
||
| 261 | if(preg_match('/hidden/si',$type, $matches)) { |
||
| 262 | $name = $this->getTagAttribute("name", $elem); |
||
| 263 | $value = $this->getTagAttribute("value", $elem); |
||
| 264 | $index = stripos($value, '$REQUEST'); |
||
| 265 | $value = !empty($index) ? '$smarty.request.' . substr($value, 10) : $value; |
||
| 266 | $hiddenInputs[] = '<input id="' . $name . '" name="' . $name . '" value="' . $value . '">'; |
||
| 267 | } |
||
| 268 | } //foreach |
||
| 269 | |||
| 270 | $templateMeta['form']['hidden'] = $hiddenInputs; |
||
| 271 | $templateMeta['widths'] = array(array('label' => '10', 'field' => '30'), array('label' => '10', 'field' => '30')); |
||
| 272 | $templateMeta['maxColumns'] = 2; |
||
| 273 | |||
| 274 | $panels = array(); |
||
| 275 | $panels['default'] = $metarow; |
||
| 276 | $panels = $this->appplyRules($moduleDir, $panels); |
||
|
|
|||
| 277 | return $this->createFileContents($moduleDir, $panels, $templateMeta, $filePath); |
||
| 278 | |||
| 279 | |||
| 280 | } |
||
| 281 | |||
| 285 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.