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 | protected static $DEFAULT_OPTIONS = array ( |
||
| 13 | 'businessType' => array( |
||
| 14 | // additional types as extension of schema:LocalBusiness |
||
| 15 | 'filter' => FILTER_DEFAULT, |
||
| 16 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 17 | ), |
||
| 18 | 'taxID' => array( |
||
| 19 | 'filter' => FILTER_CALLBACK, |
||
| 20 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_TOKEN', |
||
| 21 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 22 | ), |
||
| 23 | 'vatID' => array( // italian rules |
||
| 24 | 'filter' => FILTER_VALIDATE_REGEXP, |
||
| 25 | 'options' => array('regexp'=>'/^[0-9]{11}$/'), |
||
| 26 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 27 | ), |
||
| 28 | 'legalName' => array( |
||
| 29 | 'filter' => FILTER_CALLBACK, |
||
| 30 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
| 31 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 32 | ), |
||
| 33 | 'businessName' => array( |
||
| 34 | // a schema:alternateName for schema:PostalAddress |
||
| 35 | 'filter' => FILTER_DEFAULT, |
||
| 36 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 37 | ), |
||
| 38 | 'addressDescription'=> array( // |
||
| 39 | 'filter' => FILTER_CALLBACK, |
||
| 40 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
| 41 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 42 | ), |
||
| 43 | 'addressCountry' => array( |
||
| 44 | 'default' => 'IT', |
||
| 45 | 'filter' => FILTER_VALIDATE_REGEXP, |
||
| 46 | 'options' => array('regexp'=>'/^[A-Z]{2}$/'), |
||
| 47 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 48 | ), |
||
| 49 | 'addressLocality' => array( |
||
| 50 | 'filter' => FILTER_CALLBACK, |
||
| 51 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
| 52 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 53 | ), |
||
| 54 | 'addressRegion' => array( |
||
| 55 | 'filter' => FILTER_CALLBACK, |
||
| 56 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
| 57 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 58 | ), |
||
| 59 | 'streetAddress' => array( |
||
| 60 | 'filter' => FILTER_CALLBACK, |
||
| 61 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_ADDRESS', |
||
| 62 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 63 | ), |
||
| 64 | 'postalCode' => array( // italian rules |
||
| 65 | 'filter' => FILTER_VALIDATE_REGEXP, |
||
| 66 | 'options' => array('regexp'=>'/^[0-9]{5}$/'), |
||
| 67 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 68 | ), |
||
| 69 | 'telephone' => array( |
||
| 70 | 'filter' => FILTER_CALLBACK, |
||
| 71 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_TELEPHONE', |
||
| 72 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 73 | ), |
||
| 74 | 'faxNumber' => array( |
||
| 75 | 'filter' => FILTER_CALLBACK, |
||
| 76 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_TELEPHONE', |
||
| 77 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 78 | ), |
||
| 79 | 'email' => array( |
||
| 80 | 'filter' => FILTER_CALLBACK, |
||
| 81 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_EMAIL', |
||
| 82 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 83 | ), |
||
| 84 | 'lat' => array( |
||
| 85 | 'filter' => FILTER_CALLBACK, |
||
| 86 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_GEO', |
||
| 87 | ), |
||
| 88 | 'long' => array( |
||
| 89 | 'filter' => FILTER_CALLBACK, |
||
| 90 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_GEO', |
||
| 91 | ), |
||
| 92 | 'similarStreet' => array( |
||
| 93 | 'filter' => FILTER_CALLBACK, |
||
| 94 | 'options' => '\BOTK\Filters::FILTER_VALIDATE_URI', |
||
| 95 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 96 | ), |
||
| 97 | 'hasMap' => array( |
||
| 98 | 'filter' => FILTER_CALLBACK, |
||
| 99 | 'options' => '\BOTK\Filters::FILTER_SANITIZE_HTTP_URL', |
||
| 100 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 101 | ), |
||
| 102 | 'aggregateRatingValue' => array( |
||
| 103 | 'filter' => FILTER_VALIDATE_FLOAT, |
||
| 104 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 105 | ), |
||
| 106 | 'openingHours' => array( |
||
| 107 | 'filter' => FILTER_DEFAULT, |
||
| 108 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 109 | ), |
||
| 110 | 'near' => array( |
||
| 111 | 'filter' => FILTER_CALLBACK, |
||
| 112 | 'options' => '\BOTK\Filters::FILTER_VALIDATE_URI', |
||
| 113 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 114 | ), |
||
| 115 | 'similarName' => array( |
||
| 116 | 'filter' => FILTER_CALLBACK, |
||
| 117 | 'options' => '\BOTK\Filters::FILTER_VALIDATE_URI', |
||
| 118 | 'flags' => FILTER_FORCE_ARRAY, |
||
| 119 | ), |
||
| 120 | 'numberOfEmployees' => array( |
||
| 121 | 'filter' => FILTER_VALIDATE_REGEXP, |
||
| 122 | 'options' => array('regexp'=>'/^[0-9]+\s*-?\s*[0-9]*$/'), |
||
| 123 | 'flags' => FILTER_REQUIRE_SCALAR, |
||
| 124 | ) |
||
| 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() |
|
| 245 | |||
| 246 | } |