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 | trait Persistence { |
||
14 | |||
15 | /** |
||
16 | * [Internal] : Retrieve/Set persistence options |
||
17 | * This function can be used to get all options passing null, setting options passing an associative |
||
18 | * array or retrieve a single value passing a string |
||
19 | * |
||
20 | * @param mixed $options The options passed to the persistence layer. |
||
21 | * @return mixed All options array or a single value |
||
22 | */ |
||
23 | public static function persistenceOptions($options=null){ |
||
58 | |||
59 | /** |
||
60 | * [Internal] : Assigns or retrieve the Save callback |
||
61 | * The save callback interface is |
||
62 | * function($table, array $options) |
||
63 | * |
||
64 | * @param callable $callback The callback to use on model save |
||
65 | * @return callable Current save callback |
||
66 | */ |
||
67 | protected static function persistenceSave(callable $callback=null){ |
||
71 | |||
72 | /** |
||
73 | * [Internal] : Assigns or load the Load callback |
||
74 | * The load callback interface is |
||
75 | * function($table, array $options) |
||
76 | * |
||
77 | * @param callable $callback The callback to use on model load |
||
78 | * @return callable Current load callback |
||
79 | */ |
||
80 | protected static function persistenceLoad(callable $callback=null){ |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Enable peristence on `$table` with `$options` |
||
88 | * Avaiable options: |
||
89 | * `key` : The column name of the primary key, default to `id`. |
||
90 | * |
||
91 | * @param string $table The table name |
||
92 | * @param array $options An associative array with options for the persistance layer. |
||
93 | * @return void |
||
94 | */ |
||
95 | public static function persistOn($table, array $options=[]){ |
||
99 | |||
100 | |||
101 | /** |
||
102 | * Override standard save function with a new callback |
||
103 | * @param callable $callback The callback to use on model save |
||
104 | * @return void |
||
105 | */ |
||
106 | public static function onSave(callable $callback){ |
||
109 | |||
110 | /** |
||
111 | * Override standard load function with a new callback |
||
112 | * @param callable $callback The callback to use on model load |
||
113 | * @return void |
||
114 | */ |
||
115 | public static function onLoad(callable $callback){ |
||
118 | |||
119 | /** |
||
120 | * Load the model from the persistence layer |
||
121 | * @return mixed The retrieved object |
||
122 | */ |
||
123 | public static function load($pk){ |
||
132 | |||
133 | /** |
||
134 | * Private Standard Load Method |
||
135 | */ |
||
136 | private static function persistenceLoadDefault($pk, $table, $options){ |
||
148 | |||
149 | /** |
||
150 | * Save the model to the persistence layer |
||
151 | * @return mixed The results from the save callback. (default: lastInsertID) |
||
152 | */ |
||
153 | public function save(){ |
||
162 | |||
163 | /** |
||
164 | * Private Standard Save Method |
||
165 | */ |
||
166 | private function persistenceSaveDefault($table,$options){ |
||
175 | |||
176 | |||
177 | } |
||
178 |