Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ObjectBase 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 ObjectBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class ObjectBase |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * An array of audio resources attached to the object. |
||
| 18 | * |
||
| 19 | * @var array|Audio[] |
||
| 20 | */ |
||
| 21 | public $audios = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * A short description of the object. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | public $description; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The word that appears before the object's title in a sentence. This is an list of words from 'a', 'an', 'the', |
||
| 32 | * ' "" ', or 'auto'. If 'auto' is chosen, the consumer of the object will chose between 'a' or 'an'. The default is |
||
| 33 | * the blank, "". |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | public $determiner; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * An array of images attached to the object. |
||
| 41 | * |
||
| 42 | * @var array|Image[] |
||
| 43 | */ |
||
| 44 | public $images = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The locale that the object's tags are marked up in, in the format language_TERRITORY. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | public $locale; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * An array of alternate locales in which the resource is available. |
||
| 55 | * |
||
| 56 | * @var array|string[] |
||
| 57 | */ |
||
| 58 | public $localeAlternate = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | public $richAttachment; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * An array of URLs of related resources. |
||
| 67 | * |
||
| 68 | * @var array|string[] |
||
| 69 | */ |
||
| 70 | public $seeAlso = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The name of the web site upon which the object resides. |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | public $siteName; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The title of the object as it should appear in the graph. |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | public $title; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The type of the object, such as 'article'. |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | public $type; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The time when the object was last updated. |
||
| 95 | * |
||
| 96 | * @var \DateTime |
||
| 97 | */ |
||
| 98 | public $updatedTime; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The canonical URL of the object, used as its ID in the graph. |
||
| 102 | * |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | public $url; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * An array of videos attached to the object. |
||
| 109 | * |
||
| 110 | * @var array|Video[] |
||
| 111 | */ |
||
| 112 | public $videos = []; |
||
| 113 | |||
| 114 | 27 | public function __construct() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Assigns all properties given to the this Object instance. |
||
| 120 | * |
||
| 121 | * @param array|Property[] $properties Array of all properties to assign. |
||
| 122 | * @param bool $debug Throw exceptions when parsing or not. |
||
| 123 | * |
||
| 124 | * @throws \UnexpectedValueException |
||
| 125 | */ |
||
| 126 | 15 | public function assignProperties(array $properties, $debug = false) |
|
| 235 | |||
| 236 | 2 | private function handleImageAttribute(Image $element, $name, $value) |
|
| 257 | |||
| 258 | 1 | private function handleVideoAttribute(Video $element, $name, $value) |
|
| 276 | |||
| 277 | 1 | private function handleAudioAttribute(Audio $element, $name, $value) |
|
| 289 | |||
| 290 | 1 | protected function convertToDateTime($value) |
|
| 294 | |||
| 295 | 1 | protected function convertToBoolean($value) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Gets all properties set on this object. |
||
| 309 | * |
||
| 310 | * @return array|Property[] |
||
| 311 | */ |
||
| 312 | public function getProperties() |
||
| 374 | } |
||
| 375 |