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 Object 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 Object, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Object |
||
| 33 | { |
||
| 34 | const TYPE_ACTIVITY = 'activity'; |
||
| 35 | const TYPE_AGENT = 'agent'; |
||
| 36 | const TYPE_GROUP = 'group'; |
||
| 37 | const TYPE_STATEMENT_REFERENCE = 'statement_reference'; |
||
| 38 | const TYPE_SUB_STATEMENT = 'sub_statement'; |
||
| 39 | |||
| 40 | public $identifier; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | public $type; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string|null |
||
| 49 | */ |
||
| 50 | public $activityId; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var bool|null |
||
| 54 | */ |
||
| 55 | public $hasActivityDefinition; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var bool|null |
||
| 59 | */ |
||
| 60 | public $hasActivityName; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array|null |
||
| 64 | */ |
||
| 65 | public $activityName; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var bool|null |
||
| 69 | */ |
||
| 70 | public $hasActivityDescription; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array|null |
||
| 74 | */ |
||
| 75 | public $activityDescription; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string|null |
||
| 79 | */ |
||
| 80 | public $activityType; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string|null |
||
| 84 | */ |
||
| 85 | public $activityMoreInfo; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var Extensions|null |
||
| 89 | */ |
||
| 90 | public $activityExtensions; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var string|null |
||
| 94 | */ |
||
| 95 | public $mbox; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var string|null |
||
| 99 | */ |
||
| 100 | public $mboxSha1Sum; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string|null |
||
| 104 | */ |
||
| 105 | public $openId; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var string|null |
||
| 109 | */ |
||
| 110 | public $accountName; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var string|null |
||
| 114 | */ |
||
| 115 | public $accountHomePage; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var string|null |
||
| 119 | */ |
||
| 120 | public $name; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var Object[]|null |
||
| 124 | */ |
||
| 125 | public $members; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var Object|null |
||
| 129 | */ |
||
| 130 | public $group; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var string|null |
||
| 134 | */ |
||
| 135 | public $referencedStatementId; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var Object|null |
||
| 139 | */ |
||
| 140 | public $actor; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @var Verb|null |
||
| 144 | */ |
||
| 145 | public $verb; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @var Object|null |
||
| 149 | */ |
||
| 150 | public $object; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @var Result|null |
||
| 154 | */ |
||
| 155 | public $result; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @var Context|null |
||
| 159 | */ |
||
| 160 | public $context; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @var Statement|null |
||
| 164 | */ |
||
| 165 | public $parentContext; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @var Statement|null |
||
| 169 | */ |
||
| 170 | public $groupingContext; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @var Statement|null |
||
| 174 | */ |
||
| 175 | public $categoryContext; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @var Statement|null |
||
| 179 | */ |
||
| 180 | public $otherContext; |
||
| 181 | |||
| 182 | public static function fromModel(ObjectModel $model) |
||
| 202 | |||
| 203 | public function getModel() |
||
| 219 | |||
| 220 | private static function fromActivity(Activity $model) |
||
| 267 | |||
| 268 | private static function fromActor(Actor $model) |
||
| 298 | |||
| 299 | private static function fromSubStatement(SubStatement $model) |
||
| 309 | |||
| 310 | private function getActivityModel() |
||
| 346 | |||
| 347 | View Code Duplication | private function getActorModel() |
|
| 373 | |||
| 374 | private function getSubStatementModel() |
||
| 387 | } |
||
| 388 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: