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 | public $sizes = array(); |
||
64 | /** |
||
65 | * @api |
||
66 | * @var string $caption the string stored in the WordPress database |
||
67 | */ |
||
68 | public $caption; |
||
69 | /** |
||
70 | * @var $_wp_attached_file the file as stored in the WordPress database |
||
71 | */ |
||
72 | protected $_wp_attached_file; |
||
73 | |||
74 | /** |
||
75 | * Creates a new TimberImage object |
||
76 | * @example |
||
77 | * ```php |
||
78 | * // You can pass it an ID number |
||
79 | * $myImage = new TimberImage(552); |
||
80 | * |
||
81 | * //Or send it a URL to an image |
||
82 | * $myImage = new TimberImage('http://google.com/logo.jpg'); |
||
83 | * ``` |
||
84 | * @param int|string $iid |
||
85 | */ |
||
86 | public function __construct($iid) { |
||
89 | |||
90 | /** |
||
91 | * @return string the src of the file |
||
92 | */ |
||
93 | public function __toString() { |
||
99 | |||
100 | /** |
||
101 | * Get a PHP array with pathinfo() info from the file |
||
102 | * @return array |
||
103 | */ |
||
104 | function get_pathinfo() { |
||
107 | |||
108 | /** |
||
109 | * @internal |
||
110 | * @param string $dim |
||
111 | * @return array|int |
||
112 | */ |
||
113 | protected function get_dimensions($dim = null) { |
||
125 | |||
126 | /** |
||
127 | * @internal |
||
128 | * @param string|null $dim |
||
129 | * @return array|int |
||
130 | */ |
||
131 | protected function get_dimensions_loaded($dim) { |
||
143 | |||
144 | /** |
||
145 | * @internal |
||
146 | * @param int $iid the id number of the image in the WP database |
||
147 | */ |
||
148 | protected function get_image_info( $iid ) { |
||
173 | |||
174 | /** |
||
175 | * @internal |
||
176 | * @param string $url for evaluation |
||
177 | * @return string with http/https corrected depending on what's appropriate for server |
||
178 | */ |
||
179 | protected static function _maybe_secure_url($url) { |
||
185 | |||
186 | public static function wp_upload_dir() { |
||
195 | |||
196 | /** |
||
197 | * @internal |
||
198 | * @param int $iid |
||
199 | */ |
||
200 | function init( $iid = false ) { |
||
250 | |||
251 | /** |
||
252 | * @internal |
||
253 | * @param string $relative_path |
||
254 | */ |
||
255 | protected function init_with_relative_path( $relative_path ) { |
||
261 | |||
262 | /** |
||
263 | * @internal |
||
264 | * @param string $file_path |
||
265 | */ |
||
266 | protected function init_with_file_path( $file_path ) { |
||
272 | |||
273 | /** |
||
274 | * @internal |
||
275 | * @param string $url |
||
276 | */ |
||
277 | protected function init_with_url($url) { |
||
284 | |||
285 | /** |
||
286 | * @api |
||
287 | * @example |
||
288 | * ```twig |
||
289 | * <img src="{{ image.src }}" alt="{{ image.alt }}" /> |
||
290 | * ``` |
||
291 | * ```html |
||
292 | * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" alt="W3 Checker told me to add alt text, so I am" /> |
||
293 | * ``` |
||
294 | * @return string alt text stored in WordPress |
||
295 | */ |
||
296 | public function alt() { |
||
300 | |||
301 | /** |
||
302 | * @api |
||
303 | * @example |
||
304 | * ```twig |
||
305 | * {% if post.thumbnail.aspect < 1 %} |
||
306 | * {# handle vertical image #} |
||
307 | * <img src="{{ post.thumbnail.src|resize(300, 500) }}" alt="A basketball player" /> |
||
308 | * {% else %} |
||
309 | * <img src="{{ post.thumbnail.src|resize(500) }}" alt="A sumo wrestler" /> |
||
310 | * {% endif %} |
||
311 | * ``` |
||
312 | * @return float |
||
313 | */ |
||
314 | public function aspect() { |
||
319 | |||
320 | /** |
||
321 | * @api |
||
322 | * @example |
||
323 | * ```twig |
||
324 | * <img src="{{ image.src }}" height="{{ image.height }}" /> |
||
325 | * ``` |
||
326 | * ```html |
||
327 | * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" height="900" /> |
||
328 | * ``` |
||
329 | * @return int |
||
330 | */ |
||
331 | public function height() { |
||
334 | |||
335 | /** |
||
336 | * Returns the link to an image attachment's Permalink page (NOT the link for the image itself!!) |
||
337 | * @api |
||
338 | * @example |
||
339 | * ```twig |
||
340 | * <a href="{{ image.link }}"><img src="{{ image.src }} "/></a> |
||
341 | * ``` |
||
342 | * ```html |
||
343 | * <a href="http://example.org/my-cool-picture"><img src="http://example.org/wp-content/uploads/2015/whatever.jpg"/></a> |
||
344 | * ``` |
||
345 | */ |
||
346 | public function link() { |
||
352 | |||
353 | /** |
||
354 | * @api |
||
355 | * @return bool|TimberPost |
||
356 | */ |
||
357 | public function parent() { |
||
363 | |||
364 | /** |
||
365 | * @api |
||
366 | * @example |
||
367 | * ```twig |
||
368 | * <img src="{{ image.path }}" /> |
||
369 | * ``` |
||
370 | * ```html |
||
371 | * <img src="/wp-content/uploads/2015/08/pic.jpg" /> |
||
372 | * ``` |
||
373 | * @return string the /relative/path/to/the/file |
||
374 | */ |
||
375 | public function path() { |
||
378 | |||
379 | /** |
||
380 | * @param string $size a size known to WordPress (like "medium") |
||
381 | * @api |
||
382 | * @example |
||
383 | * ```twig |
||
384 | * <h1>{{post.title}}</h1> |
||
385 | * <img src="{{post.thumbnail.src}}" /> |
||
386 | * ``` |
||
387 | * ```html |
||
388 | * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" /> |
||
389 | * ``` |
||
390 | * @return bool|string |
||
391 | */ |
||
392 | public function src($size = '') { |
||
417 | |||
418 | /** |
||
419 | * @deprecated use src() instead |
||
420 | * @return string |
||
421 | */ |
||
422 | function url() { |
||
425 | |||
426 | /** |
||
427 | * @api |
||
428 | * @example |
||
429 | * ```twig |
||
430 | * <img src="{{ image.src }}" width="{{ image.width }}" /> |
||
431 | * ``` |
||
432 | * ```html |
||
433 | * <img src="http://example.org/wp-content/uploads/2015/08/pic.jpg" width="1600" /> |
||
434 | * ``` |
||
435 | * @return int |
||
436 | */ |
||
437 | public function width() { |
||
440 | |||
441 | |||
442 | /** |
||
443 | * @deprecated 0.21.9 use TimberImage::width() instead |
||
444 | * @internal |
||
445 | * @return int |
||
446 | */ |
||
447 | function get_width() { |
||
450 | |||
451 | /** |
||
452 | * @deprecated 0.21.9 use TimberImage::height() instead |
||
453 | * @internal |
||
454 | * @return int |
||
455 | */ |
||
456 | function get_height() { |
||
459 | |||
460 | /** |
||
461 | * @deprecated 0.21.9 use TimberImage::src |
||
462 | * @internal |
||
463 | * @param string $size |
||
464 | * @return bool|string |
||
465 | */ |
||
466 | function get_src( $size = '' ) { |
||
469 | |||
470 | /** |
||
471 | * @deprecated 0.21.9 use TimberImage::path() |
||
472 | * @internal |
||
473 | * @return string |
||
474 | */ |
||
475 | function get_path() { |
||
478 | |||
479 | /** |
||
480 | * @deprecated use src() instead |
||
481 | * @return string |
||
482 | */ |
||
483 | function get_url() { |
||
486 | |||
487 | /** |
||
488 | * @internal |
||
489 | * @deprecated 0.21.8 |
||
490 | * @return bool|TimberPost |
||
491 | */ |
||
492 | function get_parent() { |
||
495 | |||
496 | /** |
||
497 | * @internal |
||
498 | * @deprecated 0.21.9 |
||
499 | * @see TimberImage::alt |
||
500 | * @return string |
||
501 | */ |
||
502 | function get_alt() { |
||
505 | |||
506 | } |
||
507 |