Complex classes like Base often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | abstract class Base |
||
22 | extends \Aimeos\MW\Common\Item\Base |
||
|
|||
23 | implements \Aimeos\MShop\Common\Item\Iface |
||
24 | { |
||
25 | private $prefix; |
||
26 | private $values; |
||
27 | private $modified = false; |
||
28 | |||
29 | /** |
||
30 | * Initializes the class properties. |
||
31 | * |
||
32 | * @param string $prefix Prefix for the keys returned by toArray() |
||
33 | * @param array $values Associative list of key/value pairs of the item properties |
||
34 | */ |
||
35 | public function __construct( $prefix, array $values ) |
||
40 | |||
41 | |||
42 | /** |
||
43 | * Returns the ID of the item if available. |
||
44 | * |
||
45 | * @return string|null ID of the item |
||
46 | */ |
||
47 | public function getId() |
||
56 | |||
57 | |||
58 | /** |
||
59 | * Sets the new ID of the item. |
||
60 | * |
||
61 | * @param string|null $id ID of the item |
||
62 | * @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls |
||
63 | */ |
||
64 | public function setId( $id ) |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Returns the site ID of the item. |
||
81 | * |
||
82 | * @return integer|null Site ID or null if no site id is available |
||
83 | */ |
||
84 | public function getSiteId() |
||
93 | |||
94 | |||
95 | /** |
||
96 | * Returns modification time of the order coupon. |
||
97 | * |
||
98 | * @return string|null Modification time (YYYY-MM-DD HH:mm:ss) |
||
99 | */ |
||
100 | public function getTimeModified() |
||
109 | |||
110 | |||
111 | /** |
||
112 | * Returns the create date of the item. |
||
113 | * |
||
114 | * @return string|null ISO date in YYYY-MM-DD hh:mm:ss format |
||
115 | */ |
||
116 | public function getTimeCreated() |
||
125 | |||
126 | |||
127 | /** |
||
128 | * Returns the name of editor who created/modified the item at last. |
||
129 | * |
||
130 | * @return string Name of editor who created/modified the item at last |
||
131 | */ |
||
132 | public function getEditor() |
||
141 | |||
142 | |||
143 | /** |
||
144 | * Tests if this Item object was modified. |
||
145 | * |
||
146 | * @return boolean True if modified, false if not |
||
147 | */ |
||
148 | public function isModified() |
||
152 | |||
153 | |||
154 | /** |
||
155 | * Sets the modified flag of the object. |
||
156 | * |
||
157 | * @return \Aimeos\MShop\Common\Item\Iface Item for chaining method calls |
||
158 | */ |
||
159 | public function setModified() |
||
164 | |||
165 | |||
166 | /** |
||
167 | * Sets the item values from the given array. |
||
168 | * |
||
169 | * @param array Associative list of item keys and their values |
||
170 | * @return array Associative list of keys and their values that are unknown |
||
171 | */ |
||
172 | public function fromArray( array $list ) |
||
187 | |||
188 | |||
189 | /** |
||
190 | * Returns the item values as array. |
||
191 | * |
||
192 | * @param boolean True to return private properties, false for public only |
||
193 | * @return array Associative list of item properties and their values |
||
194 | */ |
||
195 | public function toArray( $private = false ) |
||
209 | |||
210 | |||
211 | /** |
||
212 | * Checks if the new ID is valid for the item. |
||
213 | * |
||
214 | * @param string $old Current ID of the item |
||
215 | * @param string $new New ID which should be set in the item |
||
216 | * @return string Value of the new ID |
||
217 | * @throws \Aimeos\MShop\Common\Exception if the ID is not null or not the same as the old one |
||
218 | */ |
||
219 | public static function checkId( $old, $new ) |
||
227 | |||
228 | |||
229 | /** |
||
230 | * Tests if the date parameter represents an ISO format. |
||
231 | * |
||
232 | * @param string|null $date ISO date in yyyy-mm-dd HH:ii:ss format or null |
||
233 | * @return string|null Clean date or null for no date |
||
234 | * @throws \Aimeos\MShop\Exception If the date is invalid |
||
235 | */ |
||
236 | protected function checkDateFormat( $date ) |
||
249 | |||
250 | |||
251 | /** |
||
252 | * Tests if the code is valid. |
||
253 | * |
||
254 | * @param string $code New code for an item |
||
255 | * @return string Item code |
||
256 | * @throws \Aimeos\MShop\Exception If the code is invalid |
||
257 | */ |
||
258 | protected function checkCode( $code ) |
||
266 | |||
267 | |||
268 | /** |
||
269 | * Tests if the currency ID parameter represents an ISO currency format. |
||
270 | * |
||
271 | * @param string|null $currencyid Three letter ISO currency format, e.g. EUR |
||
272 | * @param boolean $null True if null is allowed, false if not |
||
273 | * @return string|null Three letter ISO currency ID or null for no currency |
||
274 | * @throws \Aimeos\MShop\Exception If the currency ID is invalid |
||
275 | */ |
||
276 | protected function checkCurrencyId( $currencyid, $null = true ) |
||
291 | |||
292 | |||
293 | /** |
||
294 | * Tests if the language ID parameter represents an ISO language format. |
||
295 | * |
||
296 | * @param string|null $langid ISO language format, e.g. de or de_DE |
||
297 | * @param boolean $null True if null is allowed, false if not |
||
298 | * @return string|null ISO language ID or null for no language |
||
299 | * @throws \Aimeos\MShop\Exception If the language ID is invalid |
||
300 | */ |
||
301 | protected function checkLanguageId( $langid, $null = true ) |
||
327 | |||
328 | |||
329 | /** |
||
330 | * Returns the raw value list. |
||
331 | * |
||
332 | * @return array Associative list of key/value pairs |
||
333 | */ |
||
334 | protected function getRawValues() |
||
338 | } |
||
339 |