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 |
||
28 | class Manager |
||
29 | { |
||
30 | /** |
||
31 | * Service Mode constant - Exclusive mode where only one located service |
||
32 | * will be used. |
||
33 | */ |
||
34 | const MODE_EXCLUSIVE = 1; |
||
35 | |||
36 | /** |
||
37 | * Service Mode constant - Choice mode where one service from potentially |
||
38 | * many located services will be used. The service dispatched will be selected |
||
39 | * by system default. |
||
40 | */ |
||
41 | const MODE_CHOICE = 2; |
||
42 | |||
43 | /** |
||
44 | * Service Mode constant - Choice mode where one service from potentially many |
||
45 | * located services will be used. The service dispatched will be selected by user |
||
46 | * preference, or system default if no user valid preference is available. |
||
47 | */ |
||
48 | const MODE_PREFERENCE = 4; |
||
49 | |||
50 | /** |
||
51 | * Service Mode constant - Multiple mode where all located services will be |
||
52 | * dispatched in priority order. |
||
53 | */ |
||
54 | const MODE_MULTIPLE = 8; |
||
55 | |||
56 | /** |
||
57 | * Provider priorities |
||
58 | */ |
||
59 | const PRIORITY_SELECTED = 0; |
||
60 | const PRIORITY_HIGH = 1; |
||
61 | const PRIORITY_MEDIUM = 5; |
||
62 | const PRIORITY_LOW = 9; |
||
63 | |||
64 | /** |
||
65 | * Services registry - array keyed on service name, with provider object as value |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $services = array(); |
||
70 | |||
71 | /** |
||
72 | * Provider Preferences - array keyed on service name, where each element is |
||
73 | * an array of provider name => priority entries |
||
74 | * |
||
75 | * @var array|null |
||
76 | */ |
||
77 | protected $providerPrefs = null; |
||
78 | |||
79 | /** |
||
80 | * @var string config file with provider prefs |
||
81 | */ |
||
82 | private $providerPrefsFilename = 'var/configs/system_provider_prefs.yml'; |
||
83 | |||
84 | /** |
||
85 | * @var string config cache key |
||
86 | */ |
||
87 | private $providerPrefsCacheKey = 'system/provider/prefs'; |
||
88 | |||
89 | /** |
||
90 | * __construct |
||
91 | */ |
||
92 | 1 | protected function __construct() |
|
96 | |||
97 | /** |
||
98 | * Allow one instance only! |
||
99 | * |
||
100 | * @return Manager instance |
||
101 | */ |
||
102 | 12 | public static function getInstance() |
|
112 | |||
113 | /** |
||
114 | * readYamlProviderPrefs - read configured provider preferences from file |
||
115 | * |
||
116 | * @return array of configured provider preferences |
||
117 | */ |
||
118 | 1 | public function readYamlProviderPrefs() |
|
138 | |||
139 | /** |
||
140 | * readProviderPrefs - read configured provider preferences from cache |
||
141 | * |
||
142 | * @return array of configured provider preferences |
||
143 | */ |
||
144 | 1 | protected function readProviderPrefs() |
|
153 | |||
154 | /** |
||
155 | * saveProviderPrefs - record array of provider preferences in config file, and |
||
156 | * update cache |
||
157 | * |
||
158 | * @param array $providerPrefs array of provider preferences to save |
||
159 | * |
||
160 | * @return void |
||
161 | */ |
||
162 | View Code Duplication | protected function saveProviderPrefs($providerPrefs) |
|
174 | |||
175 | /** |
||
176 | * saveChoice - record priority choices for service providers |
||
177 | * |
||
178 | * This registers a permanent choice (i.e. setting system default) that will |
||
179 | * persist after the lifetime of this service manager. |
||
180 | * |
||
181 | * @param string $service the service name being set |
||
182 | * @param array $choices array of priorities for each of the named service providers |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | public function saveChoice($service, $choices) |
||
197 | |||
198 | /** |
||
199 | * registerChoice - record priority choices for service providers |
||
200 | * |
||
201 | * This registers a temporary choice (i.e. applying user preferences) for the |
||
202 | * lifetime of this service manager only. |
||
203 | * |
||
204 | * @param string $service the service name being set |
||
205 | * @param array $choices array of priorities for each of the named service providers |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | public function registerChoice($service, $choices) |
||
221 | |||
222 | /** |
||
223 | * listChoices - list choices availabe for a named service |
||
224 | * |
||
225 | * For MODE_CHOICE services, this can supply an array containing the |
||
226 | * available choices. This array can be used to construct a user form |
||
227 | * to make a choice. |
||
228 | * |
||
229 | * @param string $service the service name being set |
||
230 | * |
||
231 | * @return array of available service provider objects for this service. |
||
232 | */ |
||
233 | public function listChoices($service) |
||
238 | |||
239 | /** |
||
240 | * locate - create a provider object for a named service, locating all contract implementors |
||
241 | * |
||
242 | * @param string $service the service name being set |
||
243 | * |
||
244 | * @return Provider object for the requested service |
||
245 | */ |
||
246 | 4 | public function locate($service) |
|
279 | } |
||
280 |