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 StatementObject 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 StatementObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class StatementObject |
||
34 | { |
||
35 | const TYPE_ACTIVITY = 'activity'; |
||
36 | const TYPE_AGENT = 'agent'; |
||
37 | const TYPE_GROUP = 'group'; |
||
38 | const TYPE_STATEMENT_REFERENCE = 'statement_reference'; |
||
39 | const TYPE_SUB_STATEMENT = 'sub_statement'; |
||
40 | |||
41 | public $identifier; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | public $type; |
||
47 | |||
48 | /** |
||
49 | * @var string|null |
||
50 | */ |
||
51 | public $activityId; |
||
52 | |||
53 | /** |
||
54 | * @var bool|null |
||
55 | */ |
||
56 | public $hasActivityDefinition; |
||
57 | |||
58 | /** |
||
59 | * @var bool|null |
||
60 | */ |
||
61 | public $hasActivityName; |
||
62 | |||
63 | /** |
||
64 | * @var array|null |
||
65 | */ |
||
66 | public $activityName; |
||
67 | |||
68 | /** |
||
69 | * @var bool|null |
||
70 | */ |
||
71 | public $hasActivityDescription; |
||
72 | |||
73 | /** |
||
74 | * @var array|null |
||
75 | */ |
||
76 | public $activityDescription; |
||
77 | |||
78 | /** |
||
79 | * @var string|null |
||
80 | */ |
||
81 | public $activityType; |
||
82 | |||
83 | /** |
||
84 | * @var string|null |
||
85 | */ |
||
86 | public $activityMoreInfo; |
||
87 | |||
88 | /** |
||
89 | * @var Extensions|null |
||
90 | */ |
||
91 | public $activityExtensions; |
||
92 | |||
93 | /** |
||
94 | * @var string|null |
||
95 | */ |
||
96 | public $mbox; |
||
97 | |||
98 | /** |
||
99 | * @var string|null |
||
100 | */ |
||
101 | public $mboxSha1Sum; |
||
102 | |||
103 | /** |
||
104 | * @var string|null |
||
105 | */ |
||
106 | public $openId; |
||
107 | |||
108 | /** |
||
109 | * @var string|null |
||
110 | */ |
||
111 | public $accountName; |
||
112 | |||
113 | /** |
||
114 | * @var string|null |
||
115 | */ |
||
116 | public $accountHomePage; |
||
117 | |||
118 | /** |
||
119 | * @var string|null |
||
120 | */ |
||
121 | public $name; |
||
122 | |||
123 | /** |
||
124 | * @var StatementObject[]|null |
||
125 | */ |
||
126 | public $members; |
||
127 | |||
128 | /** |
||
129 | * @var StatementObject|null |
||
130 | */ |
||
131 | public $group; |
||
132 | |||
133 | /** |
||
134 | * @var string|null |
||
135 | */ |
||
136 | public $referencedStatementId; |
||
137 | |||
138 | /** |
||
139 | * @var StatementObject|null |
||
140 | */ |
||
141 | public $actor; |
||
142 | |||
143 | /** |
||
144 | * @var Verb|null |
||
145 | */ |
||
146 | public $verb; |
||
147 | |||
148 | /** |
||
149 | * @var StatementObject|null |
||
150 | */ |
||
151 | public $object; |
||
152 | |||
153 | /** |
||
154 | * @var Result|null |
||
155 | */ |
||
156 | public $result; |
||
157 | |||
158 | /** |
||
159 | * @var Context|null |
||
160 | */ |
||
161 | public $context; |
||
162 | |||
163 | /** |
||
164 | * @var Statement|null |
||
165 | */ |
||
166 | public $parentContext; |
||
167 | |||
168 | /** |
||
169 | * @var Statement|null |
||
170 | */ |
||
171 | public $groupingContext; |
||
172 | |||
173 | /** |
||
174 | * @var Statement|null |
||
175 | */ |
||
176 | public $categoryContext; |
||
177 | |||
178 | /** |
||
179 | * @var Statement|null |
||
180 | */ |
||
181 | public $otherContext; |
||
182 | |||
183 | public static function fromModel($model) |
||
207 | |||
208 | public function getModel() |
||
224 | |||
225 | private static function fromActivity(Activity $model) |
||
272 | |||
273 | private static function fromActor(ActorModel $model) |
||
303 | |||
304 | private static function fromSubStatement(SubStatement $model) |
||
314 | |||
315 | private function getActivityModel() |
||
351 | |||
352 | View Code Duplication | private function getActorModel() |
|
378 | |||
379 | private function getSubStatementModel() |
||
392 | } |
||
393 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.