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 |
||
13 | class CategoryTerm extends HierarchicalTerm |
||
14 | { |
||
15 | public $templateFile = '@nkostadinov/taxonomy/migrations/template/category.php'; |
||
16 | |||
17 | /** |
||
18 | * Assigns terms to an object. |
||
19 | * |
||
20 | * @param integer $object_id |
||
21 | * @param integer|array $params The ID/IDs of the term/terms that need to be assigned. Can be integer or array of integers. |
||
22 | * @return An array with the currently assigned TaxonomyTerms. |
||
23 | */ |
||
24 | public function addTerm($object_id, $params) |
||
46 | |||
47 | /** |
||
48 | * Removes terms from an object. |
||
49 | * |
||
50 | * @param integer $object_id The id of the object. |
||
51 | * @param array|integer $params An array of term IDs or term ID. |
||
52 | * @return An array with the TaxonomyTerms objects that were removed. |
||
53 | */ |
||
54 | public function removeTerm($object_id, $params = []) |
||
77 | |||
78 | /** |
||
79 | * Overwrites the existing terms of the object, but only with the children of the given parents. |
||
80 | * |
||
81 | * $params is an array in the form [$parent_id => [$children], $parent_id2 => [$children2]]. |
||
82 | * The children of those parents will replace the existing terms where the given object is assigned. |
||
83 | * |
||
84 | * @param integer $object_id The object id whose terms are changed. |
||
85 | * @param array $params The replacement |
||
86 | * @return An array with the TaxonomyTerms set |
||
87 | */ |
||
88 | public function setTerms($object_id, $params = []) |
||
100 | |||
101 | /** |
||
102 | * Creates a new category with the following specifics: |
||
103 | * - If $parent is string - creates a root category without any children. |
||
104 | * - If parent is integer (meaning an id of a term) and: |
||
105 | * - $children is string - creates new category and assigns it to that parent; |
||
106 | * - $children is array of strings - creates new categories and assigns them to that parent; |
||
107 | * |
||
108 | * @param string|integer $parent |
||
109 | * @param string|array $children |
||
110 | * @return TaxonomyTerms|array Returns the objects created |
||
111 | * @throws InvalidParamException If a parameter of a wrong type is given |
||
112 | * @throws NotFoundHttpException If a parent with a given id is not found |
||
113 | */ |
||
114 | public function createCategory($parent, $children = []) |
||
150 | |||
151 | /** |
||
152 | * @param integer $termId |
||
153 | * @return TaxonomyTerms |
||
154 | */ |
||
155 | public function getParent($termId) |
||
164 | |||
165 | /** |
||
166 | * @param integer $termId |
||
167 | * @return boolean |
||
168 | */ |
||
169 | public function hasParent($termId) |
||
173 | |||
174 | /** |
||
175 | * @param integer $termId |
||
176 | * @return array An array of TaxonomyTerms |
||
177 | */ |
||
178 | public function getChildren($termId) |
||
182 | |||
183 | /** |
||
184 | * @param integer $termId |
||
185 | * @return boolean |
||
186 | */ |
||
187 | public function hasChildren($termId) |
||
193 | } |
||
194 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.