| Conditions | 19 |
| Paths | 4 |
| Total Lines | 57 |
| Code Lines | 41 |
| 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 |
||
| 85 | public function findByFields(string $moduleName, array $fields, array $searchValue, bool $reload = false): array |
||
| 86 | { |
||
| 87 | $return = []; |
||
| 88 | $cache = $moduleName . ':' . implode('|', $fields); |
||
| 89 | if ($reload) { |
||
| 90 | unset(self::$cache[$cache]); |
||
| 91 | } else { |
||
| 92 | foreach ($searchValue as $i => $value) { |
||
| 93 | if (isset(self::$cache[$cache][$value])) { |
||
| 94 | $return[$value] = self::$cache[$cache][$value]; |
||
| 95 | $return = array_filter($return); |
||
| 96 | unset($searchValue[$i]); |
||
| 97 | } else { |
||
| 98 | self::$cache[$cache][$value] = []; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | $moduleModel = \Vtiger_Module_Model::getInstance($moduleName); |
||
| 104 | if ($searchValue && ($fields = array_filter($fields, fn ($name) => ($fieldsModel = $moduleModel->getFieldByName($name)) && $fieldsModel->isActiveField()))) { |
||
|
|
|||
| 105 | $dataReader = $this->getQueryForFields($moduleName, $fields, $searchValue)->createQuery()->createCommand()->query(); |
||
| 106 | while ($row = $dataReader->read()) { |
||
| 107 | foreach ($fields as $fieldName) { |
||
| 108 | $fieldModel = $moduleModel->getFieldByName($fieldName); |
||
| 109 | $rowValue = $row[$fieldName]; |
||
| 110 | $recordId = $row['id']; |
||
| 111 | switch ($fieldModel->getFieldDataType()) { |
||
| 112 | case 'multiDomain': |
||
| 113 | $rowDomains = $rowValue ? array_filter(explode(',', $rowValue)) : []; |
||
| 114 | foreach ($searchValue as $email) { |
||
| 115 | $domain = mb_strtolower(explode('@', $email)[1]); |
||
| 116 | if (\in_array($domain, $rowDomains)) { |
||
| 117 | self::$cache[$cache][$email][$recordId] = $return[$email][$recordId] = $recordId; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | break; |
||
| 121 | case 'multiEmail': |
||
| 122 | $rowEmails = $rowValue ? \App\Json::decode($rowValue) : []; |
||
| 123 | foreach ($rowEmails as $emailData) { |
||
| 124 | $email = $emailData['e']; |
||
| 125 | if (\in_array($email, $searchValue)) { |
||
| 126 | self::$cache[$cache][$email][$recordId] = $return[$email][$recordId] = $recordId; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | break; |
||
| 130 | case 'recordNumber': |
||
| 131 | default: |
||
| 132 | if (\in_array($rowValue, $searchValue)) { |
||
| 133 | self::$cache[$cache][$rowValue][$recordId] = $return[$rowValue][$recordId] = $recordId; |
||
| 134 | } |
||
| 135 | break; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | return $return; |
||
| 142 | } |
||
| 253 |
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.