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 |
||
40 | class SchoolSerializer extends SerializerAbstract { |
||
41 | protected $type = 'schools'; |
||
42 | protected $link = ['school_profile']; |
||
43 | protected $include = NULL; |
||
44 | |||
45 | protected static $potentialDataTables; |
||
46 | protected static $potentialDataTableNames; |
||
47 | |||
48 | /** |
||
49 | * Unsets any included data. |
||
50 | */ |
||
51 | View Code Duplication | protected function attributes($school_term) { |
|
52 | self::$potentialDataTables = \Drupal\esdportal_api\EcDataUtils::getDataTablesWithBcodes(); |
||
53 | self::$potentialDataTableNames = \Drupal\esdportal_api\EcDataUtils::extractDataTableNames(self::$potentialDataTables); |
||
54 | |||
55 | // These turn into linkages: |
||
56 | unset($school_term->school_profile); |
||
57 | unset($school_term->school_profile_id); |
||
58 | |||
59 | // These fields are private: |
||
60 | unset($school_term->field_files); |
||
61 | |||
62 | foreach (self::$potentialDataTableNames as $name) { |
||
63 | unset($school_term->{$name}); |
||
64 | } |
||
65 | |||
66 | return $school_term; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Provides taxonomy term id as id. |
||
71 | */ |
||
72 | protected function id($school_term) { |
||
75 | |||
76 | /** |
||
77 | * Backwards compatibility with bnchdrff/json-api fork. |
||
78 | */ |
||
79 | protected function getId($school_term) { |
||
82 | |||
83 | /** |
||
84 | * Handles inclusion of school_profiles. |
||
85 | */ |
||
86 | View Code Duplication | protected function school_profile() { |
|
101 | |||
102 | /** |
||
103 | * Dynamically construct methods for data tables with bcodes. |
||
104 | * |
||
105 | * @param string $method |
||
106 | * Should be a table_name, and will be converted to camelCase. |
||
107 | */ |
||
108 | View Code Duplication | public function __call($method, $args) { |
|
143 | |||
144 | } |
||
145 |