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:
1 | <?php |
||
20 | class EcSerializer extends SerializerAbstract { |
||
21 | protected $type = 'ecs'; |
||
22 | protected $link = ['ec_profile', 'most_recent_ec_state_rating']; |
||
23 | protected $include = NULL; |
||
24 | |||
25 | protected static $potentialDataTables; |
||
26 | protected static $potentialDataTableNames; |
||
27 | |||
28 | /** |
||
29 | * Removes linked info. |
||
30 | */ |
||
31 | View Code Duplication | protected function attributes($ec_term) { |
|
32 | self::$potentialDataTables = \Drupal\esdportal_api\EcDataUtils::getDataTablesWithProgramIds(); |
||
33 | self::$potentialDataTableNames = \Drupal\esdportal_api\EcDataUtils::extractDataTableNames(self::$potentialDataTables); |
||
34 | |||
35 | // These turn into linkages: |
||
36 | unset($ec_term->ec_profile); |
||
37 | unset($ec_term->ec_profile_id); |
||
38 | unset($ec_term->most_recent_ec_state_rating); |
||
39 | unset($ec_term->most_recent_ec_state_rating_id); |
||
40 | |||
41 | foreach (self::$potentialDataTableNames as $name) { |
||
42 | unset($ec_term->{$name}); |
||
43 | } |
||
44 | |||
45 | return $ec_term; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Returns the id. |
||
50 | */ |
||
51 | protected function id($ec_term) { |
||
54 | /** |
||
55 | * Same thing... backwards compatible with bnchdrff/json-api just in case. |
||
56 | */ |
||
57 | protected function getId($ec_term) { |
||
60 | |||
61 | /** |
||
62 | * Handles inclusion of ec_profiles. |
||
63 | */ |
||
64 | View Code Duplication | protected function ec_profile() { |
|
79 | |||
80 | /** |
||
81 | * Handles inclusion of most_recent_ec_state_ratings. |
||
82 | */ |
||
83 | protected function most_recent_ec_state_rating() { |
||
98 | |||
99 | /** |
||
100 | * Dynamically construct methods for data tables with bcodes. |
||
101 | * |
||
102 | * @param string $method |
||
103 | * Should be a table_name, and will be converted to camelCase. |
||
104 | */ |
||
105 | View Code Duplication | public function __call($method, $args) { |
|
140 | |||
141 | } |
||
142 |