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 PrivateSettingsTable 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 PrivateSettingsTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class PrivateSettingsTable { |
||
19 | |||
20 | /** @var Database */ |
||
21 | private $db; |
||
22 | |||
23 | /** @var EntityTable */ |
||
24 | private $entities; |
||
25 | |||
26 | /** @var Name of the database table */ |
||
27 | private $table; |
||
28 | |||
29 | /** |
||
30 | * Constructor |
||
31 | * |
||
32 | * @param Database $db The database |
||
33 | * @param EntityTable $entities Entities table |
||
34 | */ |
||
35 | public function __construct(Database $db, EntityTable $entities) { |
||
40 | |||
41 | /** |
||
42 | * Returns entities based upon private settings |
||
43 | * |
||
44 | * Also accepts all options available to elgg_get_entities(). Supports |
||
45 | * the singular option shortcut. |
||
46 | * |
||
47 | * @param array $options Array in format: |
||
48 | * |
||
49 | * private_setting_names => null|ARR private setting names |
||
50 | * |
||
51 | * private_setting_values => null|ARR metadata values |
||
52 | * |
||
53 | * private_setting_name_value_pairs => null|ARR ( |
||
54 | * name => 'name', |
||
55 | * value => 'value', |
||
56 | * 'operand' => '=', |
||
57 | * ) |
||
58 | * Currently if multiple values are sent via |
||
59 | * an array (value => array('value1', 'value2') |
||
60 | * the pair's operand will be forced to "IN". |
||
61 | * |
||
62 | * private_setting_name_value_pairs_operator => null|STR The operator to |
||
63 | * use for combining |
||
64 | * (name = value) OPERATOR (name = value); |
||
65 | * default AND |
||
66 | * |
||
67 | * private_setting_name_prefix => STR A prefix to apply to all private |
||
68 | * settings. Used to namespace plugin user |
||
69 | * settings or by plugins to namespace their |
||
70 | * own settings. |
||
71 | * |
||
72 | * @return mixed int If count, int. If not count, array. false on errors. |
||
73 | */ |
||
74 | public function getEntities(array $options = array()) { |
||
122 | |||
123 | /** |
||
124 | * Returns private setting name and value SQL where/join clauses for entities |
||
125 | * |
||
126 | * @param string $table Entities table name |
||
127 | * @param array|null $names Array of names |
||
128 | * @param array|null $values Array of values |
||
129 | * @param array|null $pairs Array of names / values / operands |
||
130 | * @param string $pair_operator Operator for joining pairs where clauses |
||
131 | * @param string $name_prefix A string to prefix all names with |
||
132 | * @return array |
||
133 | */ |
||
134 | private function getWhereSql($table, $names = null, $values = null, |
||
288 | |||
289 | /** |
||
290 | * Gets a private setting for an entity |
||
291 | * |
||
292 | * Plugin authors can set private data on entities. By default private |
||
293 | * data will not be searched or exported. |
||
294 | * |
||
295 | * @param int $entity_guid The entity GUID |
||
296 | * @param string $name The name of the setting |
||
297 | * |
||
298 | * @return mixed The setting value, or null if does not exist |
||
299 | */ |
||
300 | public function get($entity_guid, $name) { |
||
320 | |||
321 | /** |
||
322 | * Return an array of all private settings. |
||
323 | * |
||
324 | * @param int $entity_guid The entity GUID |
||
325 | * |
||
326 | * @return string[] empty array if no settings |
||
327 | */ |
||
328 | function getAll($entity_guid) { |
||
350 | |||
351 | /** |
||
352 | * Sets a private setting for an entity. |
||
353 | * |
||
354 | * @param int $entity_guid The entity GUID |
||
355 | * @param string $name The name of the setting |
||
356 | * @param string $value The value of the setting |
||
357 | * @return bool |
||
358 | */ |
||
359 | public function set($entity_guid, $name, $value) { |
||
371 | |||
372 | /** |
||
373 | * Deletes a private setting for an entity. |
||
374 | * |
||
375 | * @param int $entity_guid The Entity GUID |
||
376 | * @param string $name The name of the setting |
||
377 | * @return bool |
||
378 | */ |
||
379 | function remove($entity_guid, $name) { |
||
394 | |||
395 | /** |
||
396 | * Deletes all private settings for an entity |
||
397 | * |
||
398 | * @param int $entity_guid The Entity GUID |
||
399 | * @return bool |
||
400 | */ |
||
401 | function removeAllForEntity($entity_guid) { |
||
413 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..