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 |
||
| 10 | class LocalBusiness extends AbstractModel implements \BOTK\ModelInterface |
||
| 11 | { |
||
| 12 | |||
| 13 | protected static $DEFAULT_OPTIONS = array ( |
||
| 14 | 'businessType' => array( |
||
| 15 | // additional types as extension of schema:LocalBusiness |
||
| 16 | 'filter' => FILTER_DEFAULT, |
||
| 17 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 18 | ), |
||
| 19 | 'taxID' => array( |
||
| 20 | 'filter' => FILTER_CALLBACK, |
||
| 21 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_TOKEN', |
||
| 22 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 23 | ), |
||
| 24 | 'vatID' => array( // italian rules |
||
| 25 | 'filter' => FILTER_VALIDATE_REGEXP, |
||
| 26 | 'options' => array('regexp'=>'/^[0-9]{11}$/'), |
||
| 27 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 28 | ), |
||
| 29 | 'legalName' => array( |
||
| 30 | 'filter' => FILTER_CALLBACK, |
||
| 31 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
| 32 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 33 | ), |
||
| 34 | 'businessName' => array( |
||
| 35 | // a schema:alternateName for schema:PostalAddress |
||
| 36 | 'filter' => FILTER_DEFAULT, |
||
| 37 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 38 | ), |
||
| 39 | 'addressDescription'=> array( // |
||
| 40 | 'filter' => FILTER_CALLBACK, |
||
| 41 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
| 42 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 43 | ), |
||
| 44 | 'addressCountry' => array( |
||
| 45 | 'default' => 'IT', |
||
| 46 | 'filter' => FILTER_VALIDATE_REGEXP, |
||
| 47 | 'options' => array('regexp'=>'/^[A-Z]{2}$/'), |
||
| 48 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 49 | ), |
||
| 50 | 'addressLocality' => array( |
||
| 51 | 'filter' => FILTER_CALLBACK, |
||
| 52 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
| 53 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 54 | ), |
||
| 55 | 'addressRegion' => array( |
||
| 56 | 'filter' => FILTER_CALLBACK, |
||
| 57 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
| 58 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 59 | ), |
||
| 60 | 'streetAddress' => array( |
||
| 61 | 'filter' => FILTER_CALLBACK, |
||
| 62 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
| 63 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 64 | ), |
||
| 65 | 'postalCode' => array( // italian rules |
||
| 66 | 'filter' => FILTER_VALIDATE_REGEXP, |
||
| 67 | 'options' => array('regexp'=>'/^[0-9]{5}$/'), |
||
| 68 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 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 | 'lat' => array( |
||
| 86 | 'filter' => FILTER_CALLBACK, |
||
| 87 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_GEO', |
||
| 88 | ), |
||
| 89 | 'long' => array( |
||
| 90 | 'filter' => FILTER_CALLBACK, |
||
| 91 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_GEO', |
||
| 92 | ), |
||
| 93 | 'similarStreet' => array( |
||
| 94 | 'filter' => FILTER_CALLBACK, |
||
| 95 | 'options' => '\BOTK\Filters::FILTER_VALIDATE_URI', |
||
| 96 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 97 | ), |
||
| 98 | 'hasMap' => array( |
||
| 99 | 'filter' => FILTER_CALLBACK, |
||
| 100 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_HTTP_URL', |
||
| 101 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 102 | ), |
||
| 103 | 'aggregateRatingValue' => array( |
||
| 104 | 'filter' => FILTER_VALIDATE_FLOAT, |
||
| 105 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 106 | ), |
||
| 107 | 'openingHours' => array( |
||
| 108 | 'filter' => FILTER_DEFAULT, |
||
| 109 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 110 | ), |
||
| 111 | 'near' => array( |
||
| 112 | 'filter' => FILTER_CALLBACK, |
||
| 113 | 'options' => '\BOTK\Filters::FILTER_VALIDATE_URI', |
||
| 114 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 115 | ), |
||
| 116 | 'similarName' => array( |
||
| 117 | 'filter' => FILTER_CALLBACK, |
||
| 118 | 'options' => '\BOTK\Filters::FILTER_VALIDATE_URI', |
||
| 119 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 120 | ), |
||
| 121 | 'numberOfEmployees' => array( |
||
| 122 | 'filter' => FILTER_VALIDATE_REGEXP, |
||
| 123 | 'options' => array('regexp'=>'/^[0-9]+\s*-?\s*[0-9]*$/'), |
||
| 124 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 125 | ), |
||
| 126 | |||
| 127 | ); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Redefine protected constructor to add address description as dynamic property |
||
| 131 | */ |
||
| 132 | 13 | protected function __construct(array $data = array(), array $customOptions = array()) |
|
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * If not existing, create an address description as a normalized address from following data properties: |
||
| 141 | * 'addressLocality', |
||
| 142 | * 'addressRegion', |
||
| 143 | * 'streetAddress', |
||
| 144 | * 'postalCode', |
||
| 145 | */ |
||
| 146 | 13 | private function addAddressDescription() |
|
| 167 | |||
| 168 | |||
| 169 | 4 | public function asTurtleFragment() |
|
| 259 | |||
| 260 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.