| Conditions | 10 |
| Paths | 3 |
| Total Lines | 29 |
| Code Lines | 21 |
| 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 |
||
| 99 | public static function getByRecordModel(string $view, \Vtiger_Record_Model $recordModel, bool $cache = true): array |
||
| 100 | { |
||
| 101 | $cacheKey = $view . $recordModel->getId(); |
||
| 102 | if ($cache && isset(self::$recordModelCache[$cacheKey])) { |
||
| 103 | return self::$recordModelCache[$cacheKey]; |
||
| 104 | } |
||
| 105 | $return = [ |
||
| 106 | 'show' => ['backend' => [], 'frontend' => [], 'mandatory' => []], |
||
| 107 | 'hide' => ['backend' => [], 'frontend' => [], 'mandatory' => []], |
||
| 108 | 'mandatory' => [], |
||
| 109 | 'conditionsFields' => [], |
||
| 110 | ]; |
||
| 111 | $fields = self::getByModule($recordModel->getModule()->getId()); |
||
| 112 | if ($fields && isset($fields[$view])) { |
||
|
|
|||
| 113 | foreach ($fields[$view] as $row) { |
||
| 114 | $status = (!$row['conditions'] || Condition::checkConditions($row['conditions'], $recordModel)) ? 'show' : 'hide'; |
||
| 115 | if (self::GUI_FRONTEND === $row['gui']) { |
||
| 116 | $return[$status]['frontend'] = array_merge($return[$status]['frontend'], $row['fields']); |
||
| 117 | } else { |
||
| 118 | $return[$status]['backend'] = array_merge($return[$status]['backend'], $row['fields']); |
||
| 119 | } |
||
| 120 | if (1 === $row['mandatory']) { |
||
| 121 | $return[$status]['mandatory'] = array_merge($return[$status]['mandatory'], $row['fields']); |
||
| 122 | $return['mandatory'] = array_merge($return['mandatory'], $row['fields']); |
||
| 123 | } |
||
| 124 | $return['conditionsFields'] = array_merge($return['conditionsFields'], $row['conditionsFields']); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | return self::$recordModelCache[$cacheKey] = $return; |
||
| 128 | } |
||
| 130 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.