| Conditions | 38 |
| Paths | 7360 |
| Total Lines | 101 |
| Lines | 30 |
| Ratio | 29.7 % |
| 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 |
||
| 88 | public static function updateZCRMRecordToBean(AbstractZohoDao $dao, ZohoBeanInterface $bean, ZCRMRecord $record) |
||
| 89 | { |
||
| 90 | $bean->setZCRMRecord($record); |
||
| 91 | $id = $record->getEntityId(); |
||
| 92 | $bean->setZohoId($id); |
||
| 93 | $bean->setCreatedTime(!empty($record->getCreatedTime()) ? \DateTimeImmutable::createFromFormat(\DateTime::ATOM, $record->getCreatedTime()) : null); |
||
| 94 | $bean->setModifiedTime(!empty($record->getModifiedTime()) ? \DateTimeImmutable::createFromFormat(\DateTime::ATOM, $record->getModifiedTime()) : null); |
||
| 95 | $bean->setLastActivityTime(!empty($record->getLastActivityTime()) ? \DateTimeImmutable::createFromFormat(\DateTime::ATOM, $record->getLastActivityTime()) : null); |
||
| 96 | |||
| 97 | if($record->getOwner()) { $bean->setOwner_OwnerID($record->getOwner()->getId()); |
||
| 98 | } |
||
| 99 | if($record->getOwner()) { $bean->setOwner_OwnerName($record->getOwner()->getName()); |
||
| 100 | } |
||
| 101 | if($record->getModifiedBy()) { $bean->setModifiedBy_OwnerID($record->getModifiedBy()->getId()); |
||
| 102 | } |
||
| 103 | if($record->getModifiedBy()) { $bean->setModifiedBy_OwnerName($record->getModifiedBy()->getName()); |
||
| 104 | } |
||
| 105 | if($record->getCreatedBy()) { $bean->setCreatedBy_OwnerID($record->getCreatedBy()->getId()); |
||
| 106 | } |
||
| 107 | if($record->getCreatedBy()) { $bean->setCreatedBy_OwnerName($record->getCreatedBy()->getName()); |
||
| 108 | } |
||
| 109 | |||
| 110 | $fields = $dao->getFields(); |
||
| 111 | foreach ($fields as $field) { |
||
| 112 | if(!$field->isSystem() && array_key_exists($field->getApiName(), $record->getData())) { |
||
| 113 | $value = $record->getFieldValue($field->getApiName()); |
||
| 114 | $setter = $field->getSetter(); |
||
| 115 | switch ($field->getType()) { |
||
| 116 | case 'date': |
||
| 117 | if ($value && $dateObj = \DateTime::createFromFormat('M/d/Y', $value)) { |
||
| 118 | $value = $dateObj; |
||
| 119 | } elseif ($value && $dateObj = \DateTime::createFromFormat('Y-m-d', $value)) { |
||
| 120 | $value = $dateObj; |
||
| 121 | } elseif ($value && $dateObj = \DateTime::createFromFormat(\DateTime::ATOM, $value)) { |
||
| 122 | $value = $dateObj; |
||
| 123 | } elseif($value !== null) { |
||
| 124 | throw new ZohoCRMORMException('Unable to convert the Date field "' . $field->getName() . "\" into a DateTime PHP object from the the record $id of the module " . $dao->getModule() . '.'); |
||
| 125 | } |
||
| 126 | break; |
||
| 127 | case 'datetime': |
||
| 128 | $value = \DateTime::createFromFormat(\DateTime::ATOM, $value); |
||
| 129 | break; |
||
| 130 | case 'userlookup': |
||
| 131 | case 'lookup': |
||
| 132 | View Code Duplication | if ($value) { |
|
| 133 | if (is_a($value, 'zcrmsdk\crm\crud\ZCRMRecord')) { |
||
| 134 | /** |
||
| 135 | * @var $ZCRMRecord ZCRMRecord |
||
| 136 | */ |
||
| 137 | $ZCRMRecord = $value; |
||
| 138 | if (UtilsHelper::endsWith($field->getName(), 'ID')) { |
||
| 139 | $value = $ZCRMRecord->getEntityId(); |
||
| 140 | } else { |
||
| 141 | $value = $ZCRMRecord->getLookupLabel(); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } else { |
||
| 145 | $value = null; |
||
| 146 | } |
||
| 147 | break; |
||
| 148 | |||
| 149 | case 'ownerlookup': |
||
| 150 | View Code Duplication | if ($value) { |
|
| 151 | if (is_a($value, 'zcrmsdk\crm\setup\users\ZCRMUser')) { |
||
| 152 | /** |
||
| 153 | * @var $ZCRMUser ZCRMUser |
||
| 154 | */ |
||
| 155 | $ZCRMUser = $value; |
||
| 156 | if (UtilsHelper::endsWith($field->getName(), 'ID')) { |
||
| 157 | $value = $ZCRMUser->getId(); |
||
| 158 | } else { |
||
| 159 | $value = $ZCRMUser->getFullName(); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } else { |
||
| 163 | $value = null; |
||
| 164 | } |
||
| 165 | break; |
||
| 166 | default: |
||
| 167 | break; |
||
| 168 | } |
||
| 169 | if (($value === false || $value === null) && in_array($field->getType(), ['date', 'datetime', ''])) { |
||
| 170 | $value = null; |
||
| 171 | } |
||
| 172 | |||
| 173 | $bean->$setter($value); |
||
| 174 | $bean->setDirty($field->getName(), false); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | if (method_exists($bean, 'setTag')) { |
||
| 179 | $tags = []; |
||
| 180 | if (count($record->getTags())) { |
||
| 181 | foreach ($record->getTags() as $tag) { |
||
| 182 | $tags[] = $tag->getId(); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | $bean->setTag(count($tags) ? implode(';', $tags) : null); |
||
| 186 | } |
||
| 187 | |||
| 188 | } |
||
| 189 | } |
||
| 190 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: