| Conditions | 18 |
| Paths | 129 |
| Total Lines | 134 |
| Lines | 14 |
| Ratio | 10.45 % |
| 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 |
||
| 60 | public function getAttachments( |
||
| 61 | $group = null, |
||
| 62 | $type = null, |
||
| 63 | callable $before = null, |
||
| 64 | callable $after = null |
||
| 65 | ) { |
||
| 66 | if (is_array($group)) { |
||
| 67 | $options = $group; |
||
| 68 | } else { |
||
| 69 | if ($group !== null) { |
||
| 70 | $this->logger->warning( |
||
|
|
|||
| 71 | 'AttachmentAwareTrait::getAttachments() parameters are deprecated. '. |
||
| 72 | 'An array of parameters should be used.', |
||
| 73 | [ 'package' => 'locomotivemtl/charcoal-attachment' ] |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | $options = [ |
||
| 77 | 'group' => $group, |
||
| 78 | 'type' => $type, |
||
| 79 | 'before' => $before, |
||
| 80 | 'after' => $after, |
||
| 81 | ]; |
||
| 82 | } |
||
| 83 | |||
| 84 | $options = $this->parseAttachmentOptions($options); |
||
| 85 | extract($options); |
||
| 86 | |||
| 87 | View Code Duplication | if ($group !== 0) { |
|
| 88 | if (!is_string($group)) { |
||
| 89 | throw new InvalidArgumentException(sprintf( |
||
| 90 | 'The "group" must be a string, received %s', |
||
| 91 | is_object($group) ? get_class($group) : gettype($group) |
||
| 92 | )); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | if ($type !== 0) { |
||
| 97 | View Code Duplication | if (!is_string($type)) { |
|
| 98 | throw new InvalidArgumentException(sprintf( |
||
| 99 | 'The "type" must be a string, received %s', |
||
| 100 | is_object($type) ? get_class($type) : gettype($type) |
||
| 101 | )); |
||
| 102 | } |
||
| 103 | |||
| 104 | $type = preg_replace('/([a-z])([A-Z])/', '$1-$2', $type); |
||
| 105 | $type = strtolower(str_replace('\\', '/', $type)); |
||
| 106 | } |
||
| 107 | |||
| 108 | if (isset($this->attachments[$group][$type])) { |
||
| 109 | return $this->attachments[$group][$type]; |
||
| 110 | } |
||
| 111 | |||
| 112 | $objType = $this->objType(); |
||
| 113 | $objId = $this->id(); |
||
| 114 | |||
| 115 | $joinProto = $this->modelFactory()->get(Join::class); |
||
| 116 | $joinTable = $joinProto->source()->table(); |
||
| 117 | |||
| 118 | $attProto = $this->modelFactory()->get(Attachment::class); |
||
| 119 | $attTable = $attProto->source()->table(); |
||
| 120 | |||
| 121 | if (!$attProto->source()->tableExists() || !$joinProto->source()->tableExists()) { |
||
| 122 | return []; |
||
| 123 | } |
||
| 124 | |||
| 125 | $query = sprintf(' |
||
| 126 | SELECT |
||
| 127 | attachment.*, |
||
| 128 | joined.attachment_id AS attachment_id, |
||
| 129 | joined.position AS position |
||
| 130 | FROM |
||
| 131 | `%s` AS attachment |
||
| 132 | LEFT JOIN |
||
| 133 | `%s` AS joined |
||
| 134 | ON |
||
| 135 | joined.attachment_id = attachment.id |
||
| 136 | WHERE |
||
| 137 | 1 = 1', $attTable, $joinTable); |
||
| 138 | |||
| 139 | /** Disable `active` check in admin, or according to $isActive value */ |
||
| 140 | if ($isActive === true) { |
||
| 141 | $query .= ' |
||
| 142 | AND |
||
| 143 | attachment.active = 1'; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($type) { |
||
| 147 | $query .= sprintf(' |
||
| 148 | AND |
||
| 149 | attachment.type = "%s"', $type); |
||
| 150 | } |
||
| 151 | |||
| 152 | $query .= sprintf(' |
||
| 153 | AND |
||
| 154 | joined.object_type = "%s" |
||
| 155 | AND |
||
| 156 | joined.object_id = "%s"', $objType, $objId); |
||
| 157 | |||
| 158 | if ($group) { |
||
| 159 | $query .= sprintf(' |
||
| 160 | AND |
||
| 161 | joined.group = "%s"', $group); |
||
| 162 | } |
||
| 163 | |||
| 164 | $query .= ' |
||
| 165 | ORDER BY joined.position'; |
||
| 166 | |||
| 167 | $loader = $this->collectionLoader(); |
||
| 168 | $loader->setModel($attProto); |
||
| 169 | $loader->setDynamicTypeField('type'); |
||
| 170 | |||
| 171 | $callable = function (&$att) use ($before) { |
||
| 172 | if ($this instanceof AttachableInterface) { |
||
| 173 | $att->setContainerObj($this); |
||
| 174 | } |
||
| 175 | |||
| 176 | $att->isPresentable(true); |
||
| 177 | |||
| 178 | if ($att->presenter() !== null) { |
||
| 179 | $att = $this->modelFactory() |
||
| 180 | ->create($att->presenter()) |
||
| 181 | ->setData($att->flatData()); |
||
| 182 | } |
||
| 183 | |||
| 184 | if ($before !== null) { |
||
| 185 | call_user_func_array($before, [ &$att ]); |
||
| 186 | } |
||
| 187 | }; |
||
| 188 | $collection = $loader->loadFromQuery($query, $after, $callable->bindTo($this)); |
||
| 189 | |||
| 190 | $this->attachments[$group][$type] = $collection; |
||
| 191 | |||
| 192 | return $this->attachments[$group][$type]; |
||
| 193 | } |
||
| 194 | |||
| 450 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: