Conditions | 26 |
Paths | 225 |
Total Lines | 181 |
Lines | 14 |
Ratio | 7.73 % |
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 |
||
70 | public function getAttachments( |
||
71 | $group = null, |
||
72 | $type = null, |
||
73 | callable $before = null, |
||
74 | callable $after = null |
||
75 | ) { |
||
76 | if (is_array($group)) { |
||
77 | $options = $group; |
||
78 | } else { |
||
79 | if ($group !== null) { |
||
80 | $this->logger->warning( |
||
|
|||
81 | 'AttachmentAwareTrait::attachments() parameters are deprecated. '. |
||
82 | 'An array of parameters should be used.', |
||
83 | [ 'package' => 'locomotivemtl/charcoal-attachment' ] |
||
84 | ); |
||
85 | } |
||
86 | $options = [ |
||
87 | 'group' => $group, |
||
88 | 'type' => $type, |
||
89 | 'before' => $before, |
||
90 | 'after' => $after, |
||
91 | ]; |
||
92 | } |
||
93 | |||
94 | $options = $this->parseAttachmentOptions($options); |
||
95 | extract($options); |
||
96 | |||
97 | View Code Duplication | if ($group !== 0) { |
|
98 | if (!is_string($group)) { |
||
99 | throw new InvalidArgumentException(sprintf( |
||
100 | 'The "group" must be a string, received %s', |
||
101 | is_object($group) ? get_class($group) : gettype($group) |
||
102 | )); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | if ($type !== 0) { |
||
107 | View Code Duplication | if (!is_string($type)) { |
|
108 | throw new InvalidArgumentException(sprintf( |
||
109 | 'The "type" must be a string, received %s', |
||
110 | is_object($type) ? get_class($type) : gettype($type) |
||
111 | )); |
||
112 | } |
||
113 | |||
114 | $type = preg_replace('/([a-z])([A-Z])/', '$1-$2', $type); |
||
115 | $type = strtolower(str_replace('\\', '/', $type)); |
||
116 | } |
||
117 | |||
118 | if (isset($this->attachments[$group][$type])) { |
||
119 | return $this->attachments[$group][$type]; |
||
120 | } |
||
121 | |||
122 | $objType = $this->objType(); |
||
123 | $objId = $this->id(); |
||
124 | |||
125 | $joinProto = $this->modelFactory()->get(Join::class); |
||
126 | $joinTable = $joinProto->source()->table(); |
||
127 | |||
128 | $attProto = $this->modelFactory()->get(Attachment::class); |
||
129 | $attTable = $attProto->source()->table(); |
||
130 | |||
131 | if (!$attProto->source()->tableExists() || !$joinProto->source()->tableExists()) { |
||
132 | return []; |
||
133 | } |
||
134 | |||
135 | $widget = $this->attachmentWidget(); |
||
136 | |||
137 | $query = sprintf(' |
||
138 | SELECT |
||
139 | attachment.*, |
||
140 | joined.attachment_id AS attachment_id, |
||
141 | joined.position AS position |
||
142 | FROM |
||
143 | `%s` AS attachment |
||
144 | LEFT JOIN |
||
145 | `%s` AS joined |
||
146 | ON |
||
147 | joined.attachment_id = attachment.id |
||
148 | WHERE |
||
149 | 1 = 1', $attTable, $joinTable); |
||
150 | |||
151 | /** Disable `active` check in admin, or according to $isActive value */ |
||
152 | if (!$widget instanceof AttachmentWidget && $isActive === true) { |
||
153 | $query .= ' |
||
154 | AND |
||
155 | attachment.active = 1'; |
||
156 | } |
||
157 | |||
158 | if ($type) { |
||
159 | $query .= sprintf(' |
||
160 | AND |
||
161 | attachment.type = "%s"', $type); |
||
162 | } |
||
163 | |||
164 | $query .= sprintf(' |
||
165 | AND |
||
166 | joined.object_type = "%s" |
||
167 | AND |
||
168 | joined.object_id = "%s"', $objType, $objId); |
||
169 | |||
170 | if ($group) { |
||
171 | $query .= sprintf(' |
||
172 | AND |
||
173 | joined.group = "%s"', $group); |
||
174 | } |
||
175 | |||
176 | $query .= ' |
||
177 | ORDER BY joined.position'; |
||
178 | |||
179 | $loader = $this->collectionLoader(); |
||
180 | $loader->setModel($attProto); |
||
181 | $loader->setDynamicTypeField('type'); |
||
182 | |||
183 | if ($widget instanceof AttachmentWidget) { |
||
184 | $callable = function (&$att) use ($widget, $before) { |
||
185 | if ($this instanceof AttachableInterface) { |
||
186 | $att->setContainerObj($this); |
||
187 | } |
||
188 | |||
189 | if ($att instanceof AttachmentAwareInterface) { |
||
190 | $att['attachment_widget'] = $widget; |
||
191 | } |
||
192 | |||
193 | $kind = $att->type(); |
||
194 | $attachables = $widget->attachableObjects(); |
||
195 | |||
196 | if (isset($attachables[$kind]['data'])) { |
||
197 | $att->setData($attachables[$kind]['data']); |
||
198 | } |
||
199 | |||
200 | if (!$att->rawHeading()) { |
||
201 | $att->setHeading($widget->attachmentHeading()); |
||
202 | } |
||
203 | |||
204 | if (!$att->rawPreview()) { |
||
205 | $att->setPreview($widget->attachmentPreview()); |
||
206 | } |
||
207 | |||
208 | $att->isPresentable(true); |
||
209 | |||
210 | /** Not Sure if we want to present the attachment for backend preview. |
||
211 | * Might want to have a second presenter key on attachment model |
||
212 | * so we can supply either the same presenter, |
||
213 | * another one or none at all. |
||
214 | */ |
||
215 | // if ($att->presenter() !== null) { |
||
216 | // $att = $this->modelFactory() |
||
217 | // ->create($att->presenterClass()) |
||
218 | // ->setData($att->flatData()); |
||
219 | // } |
||
220 | |||
221 | if ($before !== null) { |
||
222 | call_user_func_array($before, [ &$att ]); |
||
223 | } |
||
224 | }; |
||
225 | } else { |
||
226 | $callable = function (&$att) use ($before) { |
||
227 | if ($this instanceof AttachableInterface) { |
||
228 | $att->setContainerObj($this); |
||
229 | } |
||
230 | |||
231 | $att->isPresentable(true); |
||
232 | |||
233 | if ($att->presenter() !== null) { |
||
234 | $att = $this->modelFactory() |
||
235 | ->create($att->presenter()) |
||
236 | ->setData($att->flatData()); |
||
237 | } |
||
238 | |||
239 | if ($before !== null) { |
||
240 | call_user_func_array($before, [ &$att ]); |
||
241 | } |
||
242 | }; |
||
243 | } |
||
244 | |||
245 | $collection = $loader->loadFromQuery($query, $after, $callable->bindTo($this)); |
||
246 | |||
247 | $this->attachments[$group][$type] = $collection; |
||
248 | |||
249 | return $this->attachments[$group][$type]; |
||
250 | } |
||
251 | |||
530 |
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: