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:
Complex classes like LocalBusiness 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 LocalBusiness, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class LocalBusiness extends AbstractModel implements ModelInterface |
||
13 | { |
||
14 | protected static $DEFAULT_OPTIONS = array ( |
||
15 | 'businessType' => array( |
||
16 | // additional types as extension of schema:LocalBusiness |
||
17 | 'filter' => FILTER_DEFAULT, |
||
18 | 'flags' => FILTER_FORCE_ARRAY, |
||
19 | ), |
||
20 | 'taxID' => array( |
||
21 | 'filter' => FILTER_CALLBACK, |
||
22 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_TOKEN', |
||
23 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
24 | ), |
||
25 | 'vatID' => array( // italian rules |
||
26 | 'filter' => FILTER_VALIDATE_REGEXP, |
||
27 | 'options' => array('regexp'=>'/^[0-9]{11}$/'), |
||
28 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
29 | ), |
||
30 | 'legalName' => array( |
||
31 | 'filter' => FILTER_CALLBACK, |
||
32 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
33 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
34 | ), |
||
35 | 'businessName' => array( |
||
36 | // a schema:alternateName for schema:PostalAddress |
||
37 | 'filter' => FILTER_DEFAULT, |
||
38 | 'flags' => FILTER_FORCE_ARRAY, |
||
39 | ), |
||
40 | 'addressCountry' => array( |
||
41 | 'default' => 'IT', |
||
42 | 'filter' => FILTER_VALIDATE_REGEXP, |
||
43 | 'options' => array('regexp'=>'/^[A-Z]{2}$/'), |
||
44 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
45 | ), |
||
46 | 'addressLocality' => array( |
||
47 | 'filter' => FILTER_CALLBACK, |
||
48 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
49 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
50 | ), |
||
51 | 'addressRegion' => array( |
||
52 | 'filter' => FILTER_CALLBACK, |
||
53 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
54 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
55 | ), |
||
56 | 'streetAddress' => array( |
||
57 | 'filter' => FILTER_CALLBACK, |
||
58 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
59 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
60 | ), |
||
61 | 'postalCode' => array( // italian rules |
||
62 | 'filter' => FILTER_VALIDATE_REGEXP, |
||
63 | 'options' => array('regexp'=>'/^[0-9]{5}$/'), |
||
64 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
65 | ), |
||
66 | 'page' => array( |
||
67 | 'filter' => FILTER_SANITIZE_URL, |
||
68 | 'flags' => FILTER_FORCE_ARRAY, |
||
69 | ), |
||
70 | 'telephone' => array( |
||
71 | 'filter' => FILTER_CALLBACK, |
||
72 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_TELEPHONE', |
||
73 | 'flags' => FILTER_FORCE_ARRAY, |
||
74 | ), |
||
75 | 'faxNumber' => array( |
||
76 | 'filter' => FILTER_CALLBACK, |
||
77 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_TELEPHONE', |
||
78 | 'flags' => FILTER_FORCE_ARRAY, |
||
79 | ), |
||
80 | 'email' => array( |
||
81 | 'filter' => FILTER_CALLBACK, |
||
82 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_EMAIL', |
||
83 | 'flags' => FILTER_FORCE_ARRAY, |
||
84 | ), |
||
85 | 'geoDescription' => array( |
||
86 | // a schema:alternateName for schema:GeoCoordinates |
||
87 | 'filter' => FILTER_CALLBACK, |
||
88 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
89 | 'flags' => FILTER_FORCE_ARRAY, |
||
90 | ), |
||
91 | 'lat' => array( |
||
92 | 'filter' => FILTER_CALLBACK, |
||
93 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_LAT_LONG', |
||
94 | ), |
||
95 | 'long' => array( |
||
96 | 'filter' => FILTER_CALLBACK, |
||
97 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_LAT_LONG', |
||
98 | ), |
||
99 | ); |
||
100 | |||
101 | public function __construct(array $data = array(), array $customOptions = array()) |
||
106 | |||
107 | /** |
||
108 | * Create a normalized address from wollowing datam properties |
||
109 | * 'addressCountry', |
||
110 | * 'addressLocality', |
||
111 | * 'addressRegion', |
||
112 | * 'streetAddress', |
||
113 | * 'postalCode', |
||
114 | * If data is not sufficient to create a good addess, false is returned. |
||
115 | */ |
||
116 | public function buildNormalizedAddress() |
||
133 | |||
134 | |||
135 | public function asTurtle() |
||
227 | |||
228 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.