Complex classes like TimberImage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TimberImage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class TimberImage extends TimberPost implements TimberCoreInterface { |
||
41 | |||
42 | protected $_can_edit; |
||
43 | protected $_dimensions; |
||
44 | public $abs_url; |
||
45 | /** |
||
46 | * @var string $object_type what does this class represent in WordPress terms? |
||
47 | */ |
||
48 | public $object_type = 'image'; |
||
49 | /** |
||
50 | * @var string $representation what does this class represent in WordPress terms? |
||
51 | */ |
||
52 | public static $representation = 'image'; |
||
53 | /** |
||
54 | * @var array of supported relative file types |
||
55 | */ |
||
56 | private $file_types = array('jpg', 'jpeg', 'png', 'svg', 'bmp', 'ico', 'gif', 'tiff', 'pdf'); |
||
57 | /** |
||
58 | * @api |
||
59 | * @var string $file_loc the location of the image file in the filesystem (ex: `/var/www/htdocs/wp-content/uploads/2015/08/my-pic.jpg`) |
||
60 | */ |
||
61 | public $file_loc; |
||
62 | public $file; |
||
63 | /** |
||
64 | * @api |
||
65 | * @var integer the ID of the image (which is a WP_Post) |
||
66 | */ |
||
67 | public $id; |
||
68 | public $sizes = array(); |
||
69 | /** |
||
70 | * @api |
||
71 | * @var string $caption the string stored in the WordPress database |
||
72 | */ |
||
73 | public $caption; |
||
74 | /** |
||
75 | * @var $_wp_attached_file the file as stored in the WordPress database |
||
76 | */ |
||
77 | protected $_wp_attached_file; |
||
78 | |||
79 | /** |
||
80 | * Creates a new TimberImage object |
||
81 | * @example |
||
82 | * ```php |
||
83 | * // You can pass it an ID number |
||
84 | * $myImage = new TimberImage(552); |
||
85 | * |
||
86 | * //Or send it a URL to an image |
||
87 | * $myImage = new TimberImage('http://google.com/logo.jpg'); |
||
88 | * ``` |
||
89 | * @param int|string $iid |
||
90 | */ |
||
91 | public function __construct($iid) { |
||
94 | |||
95 | /** |
||
96 | * @return string the src of the file |
||
|
|||
97 | */ |
||
98 | public function __toString() { |
||
104 | |||
105 | /** |
||
106 | * Get a PHP array with pathinfo() info from the file |
||
107 | * @return array |
||
108 | */ |
||
109 | function get_pathinfo() { |
||
112 | |||
113 | /** |
||
114 | * @internal |
||
115 | * @param string $dim |
||
116 | * @return array|int |
||
117 | */ |
||
118 | protected function get_dimensions($dim = null) { |
||
130 | |||
131 | /** |
||
132 | * @internal |
||
133 | * @param string|null $dim |
||
134 | * @return array|int |
||
135 | */ |
||
136 | protected function get_dimensions_loaded($dim) { |
||
148 | |||
149 | /** |
||
150 | * @internal |
||
151 | * @param int $iid the id number of the image in the WP database |
||
152 | */ |
||
153 | protected function get_image_info( $iid ) { |
||
178 | |||
179 | /** |
||
180 | * @internal |
||
181 | * @param string $url for evaluation |
||
182 | * @return string with http/https corrected depending on what's appropriate for server |
||
183 | */ |
||
184 | protected static function _maybe_secure_url($url) { |
||
190 | |||
191 | public static function wp_upload_dir() { |
||
200 | |||
201 | /** |
||
202 | * @internal |
||
203 | * @param int $iid |
||
204 | */ |
||
205 | function init( $iid = false ) { |
||
206 | if ( !is_numeric( $iid ) && is_string( $iid ) ) { |
||
207 | if (strstr($iid, '://')) { |
||
208 | $this->init_with_url($iid); |
||
209 | return; |
||
210 | } |
||
211 | if ( strstr($iid, ABSPATH) ) { |
||
212 | $this->init_with_file_path($iid); |
||
213 | return; |
||
214 | } |
||
215 | |||
216 | $relative = false; |
||
217 | $iid_lower = strtolower($iid); |
||
218 | foreach( $this->file_types as $type ) { if( strstr( $iid_lower, $type ) ) { $relative = true; break; } }; |
||
219 | if ( $relative ) { |
||
220 | $this->init_with_relative_path( $iid ); |
||
221 | return; |
||
222 | } |
||
223 | } else if ( $iid instanceof WP_Post ) { |
||
224 | $ref = new ReflectionClass($this); |
||
225 | $post = $ref->getParentClass()->newInstance($iid->ID); |
||
226 | if (isset($post->_thumbnail_id) && $post->_thumbnail_id) { |
||
227 | return $this->init((int) $post->_thumbnail_id); |
||
228 | } |
||
229 | return $this->init($iid->ID); |
||
230 | } else if ( $iid instanceof TimberPost ) { |
||
231 | /** |
||
232 | * This will catch TimberPost and any post classes that extend TimberPost, |
||
233 | * see http://php.net/manual/en/internals2.opcodes.instanceof.php#109108 |
||
234 | * and https://github.com/jarednova/timber/wiki/Extending-Timber |
||
235 | */ |
||
236 | $iid = (int) $iid->_thumbnail_id; |
||
237 | } |
||
238 | |||
239 | $image_info = $this->get_image_info($iid); |
||
240 | |||
241 | $this->import($image_info); |
||
242 | $basedir = self::wp_upload_dir(); |
||
243 | $basedir = $basedir['basedir']; |
||
244 | if ( isset($this->file) ) { |
||
245 | $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file; |
||
246 | } else if ( isset($this->_wp_attached_file) ) { |
||
247 | $this->file = reset($this->_wp_attached_file); |
||
248 | $this->file_loc = $basedir . DIRECTORY_SEPARATOR . $this->file; |
||
249 | } |
||
250 | if ( isset($image_info['id']) ) { |
||
251 | $this->ID = $image_info['id']; |
||
252 | } else if ( is_numeric($iid) ) { |
||
253 | $this->ID = $iid; |
||
254 | } |
||
255 | if ( isset($this->ID) ) { |
||
256 | $custom = get_post_custom($this->ID); |
||
257 | foreach ($custom as $key => $value) { |
||
258 | $this->$key = $value[0]; |
||
259 | } |
||
260 | $this->id = $this->ID; |
||
261 | } else { |
||
262 | if ( is_array($iid) || is_object($iid) ) { |
||
263 | TimberHelper::error_log('Not able to init in TimberImage with iid='); |
||
264 | TimberHelper::error_log($iid); |
||
265 | } else { |
||
266 | TimberHelper::error_log('Not able to init in TimberImage with iid=' . $iid); |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @internal |
||
273 | * @param string $relative_path |
||
274 | */ |
||
275 | protected function init_with_relative_path( $relative_path ) { |
||
281 | |||
282 | /** |
||
283 | * @internal |
||
284 | * @param string $file_path |
||
285 | */ |
||
286 | protected function init_with_file_path( $file_path ) { |
||
292 | |||
293 | /** |
||
294 | * @internal |
||
295 | * @param string $url |
||
296 | */ |
||
297 | protected function init_with_url($url) { |
||
304 | |||
305 | /** |
||
306 | * @api |
||
307 | * @example |
||
308 | * ```twig |
||
309 | * <img src="{{ image.src }}" alt="{{ image.alt }}" /> |
||
310 | * ``` |
||
311 | * ```html |
||
312 | * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" alt="W3 Checker told me to add alt text, so I am" /> |
||
313 | * ``` |
||
314 | * @return string alt text stored in WordPress |
||
315 | */ |
||
316 | public function alt() { |
||
320 | |||
321 | /** |
||
322 | * @api |
||
323 | * @example |
||
324 | * ```twig |
||
325 | * {% if post.thumbnail.aspect < 1 %} |
||
326 | * {# handle vertical image #} |
||
327 | * <img src="{{ post.thumbnail.src|resize(300, 500) }}" alt="A basketball player" /> |
||
328 | * {% else %} |
||
329 | * <img src="{{ post.thumbnail.src|resize(500) }}" alt="A sumo wrestler" /> |
||
330 | * {% endif %} |
||
331 | * ``` |
||
332 | * @return float |
||
333 | */ |
||
334 | public function aspect() { |
||
339 | |||
340 | /** |
||
341 | * @api |
||
342 | * @example |
||
343 | * ```twig |
||
344 | * <img src="{{ image.src }}" height="{{ image.height }}" /> |
||
345 | * ``` |
||
346 | * ```html |
||
347 | * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" height="900" /> |
||
348 | * ``` |
||
349 | * @return int |
||
350 | */ |
||
351 | public function height() { |
||
354 | |||
355 | /** |
||
356 | * Returns the link to an image attachment's Permalink page (NOT the link for the image itself!!) |
||
357 | * @api |
||
358 | * @example |
||
359 | * ```twig |
||
360 | * <a href="{{ image.link }}"><img src="{{ image.src }} "/></a> |
||
361 | * ``` |
||
362 | * ```html |
||
363 | * <a href="http://example.org/my-cool-picture"><img src="http://example.org/wp-content/uploads/2015/whatever.jpg"/></a> |
||
364 | * ``` |
||
365 | */ |
||
366 | public function link() { |
||
372 | |||
373 | /** |
||
374 | * @api |
||
375 | * @return bool|TimberPost |
||
376 | */ |
||
377 | public function parent() { |
||
383 | |||
384 | /** |
||
385 | * @api |
||
386 | * @example |
||
387 | * ```twig |
||
388 | * <img src="{{ image.path }}" /> |
||
389 | * ``` |
||
390 | * ```html |
||
391 | * <img src="/wp-content/uploads/2015/08/pic.jpg" /> |
||
392 | * ``` |
||
393 | * @return string the /relative/path/to/the/file |
||
394 | */ |
||
395 | public function path() { |
||
398 | |||
399 | /** |
||
400 | * @param string $size a size known to WordPress (like "medium") |
||
401 | * @api |
||
402 | * @example |
||
403 | * ```twig |
||
404 | * <h1>{{post.title}}</h1> |
||
405 | * <img src="{{post.thumbnail.src}}" /> |
||
406 | * ``` |
||
407 | * ```html |
||
408 | * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" /> |
||
409 | * ``` |
||
410 | * @return bool|string |
||
411 | */ |
||
412 | public function src($size = '') { |
||
437 | |||
438 | /** |
||
439 | * @deprecated use src() instead |
||
440 | * @return string |
||
441 | */ |
||
442 | function url() { |
||
445 | |||
446 | /** |
||
447 | * @api |
||
448 | * @example |
||
449 | * ```twig |
||
450 | * <img src="{{ image.src }}" width="{{ image.width }}" /> |
||
451 | * ``` |
||
452 | * ```html |
||
453 | * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" width="1600" /> |
||
454 | * ``` |
||
455 | * @return int |
||
456 | */ |
||
457 | public function width() { |
||
460 | |||
461 | |||
462 | /** |
||
463 | * @deprecated 0.21.9 use TimberImage::width() instead |
||
464 | * @internal |
||
465 | * @return int |
||
466 | */ |
||
467 | function get_width() { |
||
470 | |||
471 | /** |
||
472 | * @deprecated 0.21.9 use TimberImage::height() instead |
||
473 | * @internal |
||
474 | * @return int |
||
475 | */ |
||
476 | function get_height() { |
||
479 | |||
480 | /** |
||
481 | * @deprecated 0.21.9 use TimberImage::src |
||
482 | * @internal |
||
483 | * @param string $size |
||
484 | * @return bool|string |
||
485 | */ |
||
486 | function get_src( $size = '' ) { |
||
489 | |||
490 | /** |
||
491 | * @deprecated 0.21.9 use TimberImage::path() |
||
492 | * @internal |
||
493 | * @return string |
||
494 | */ |
||
495 | function get_path() { |
||
498 | |||
499 | /** |
||
500 | * @deprecated use src() instead |
||
501 | * @return string |
||
502 | */ |
||
503 | function get_url() { |
||
506 | |||
507 | /** |
||
508 | * @internal |
||
509 | * @deprecated 0.21.8 |
||
510 | * @return bool|TimberPost |
||
511 | */ |
||
512 | function get_parent() { |
||
515 | |||
516 | /** |
||
517 | * @internal |
||
518 | * @deprecated 0.21.9 |
||
519 | * @see TimberImage::alt |
||
520 | * @return string |
||
521 | */ |
||
522 | function get_alt() { |
||
525 | |||
526 | } |
||
527 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.