Complex classes like ServiceTrait 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 ServiceTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | trait ServiceTrait |
||
| 15 | { |
||
| 16 | private $_aggregateRating; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Set aggregate rating |
||
| 20 | * |
||
| 21 | * The overall rating, based on a collection of reviews or ratings, of the item. |
||
| 22 | * |
||
| 23 | * @param AggregateRating $rating |
||
|
|
|||
| 24 | * @return static |
||
| 25 | */ |
||
| 26 | public function setAggregateRating($aggregateRating) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Get aggregate rating |
||
| 35 | * |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | public function getAggregateRating() |
||
| 42 | |||
| 43 | private $_areaServed; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Set area served |
||
| 47 | * |
||
| 48 | * The geographic area where a service or offered item is provided. Supersedes serviceArea. |
||
| 49 | * |
||
| 50 | * @param mixed $areaServed |
||
| 51 | * @return static |
||
| 52 | */ |
||
| 53 | public function setAreaServed($areaServed) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get area served |
||
| 62 | * |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | public function getAreaServed() |
||
| 69 | |||
| 70 | private $_audience; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Set the audience. |
||
| 74 | * |
||
| 75 | * An intended audience, i.e. a group for whom something was created. Supersedes serviceAudience. |
||
| 76 | * |
||
| 77 | * @param Audience $audience |
||
| 78 | * @return static |
||
| 79 | */ |
||
| 80 | public function setAudience($audience) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Get the audience |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public function getAudience() |
||
| 95 | |||
| 96 | private $_availableChannel; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Set available channel |
||
| 100 | * |
||
| 101 | * A means of accessing the service (e.g. a phone bank, a web site, a location, etc.). |
||
| 102 | * |
||
| 103 | * @param ServiceChannel $availableChannel |
||
| 104 | * @return static |
||
| 105 | */ |
||
| 106 | public function setAvailableChannel($availableChannel) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get available channel |
||
| 114 | * |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function getAvailableChannel() |
||
| 121 | |||
| 122 | private $_award; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Set an award |
||
| 126 | * |
||
| 127 | * An award won by or for this item. Supersedes awards. |
||
| 128 | * |
||
| 129 | * @param mixed $award |
||
| 130 | * @return static |
||
| 131 | */ |
||
| 132 | public function setAward($award) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get the award |
||
| 141 | * |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function getAward() |
||
| 148 | |||
| 149 | private $_brand; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Set a brand |
||
| 153 | * |
||
| 154 | * The brand(s) associated with a product or service, or the brand(s) maintained by an organization or business person. |
||
| 155 | * |
||
| 156 | * @param mixed $brand |
||
| 157 | * @return static |
||
| 158 | */ |
||
| 159 | public function setBrand($brand) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get the brand |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function getBrand() |
||
| 174 | |||
| 175 | private $_broker; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Set the broker |
||
| 179 | * |
||
| 180 | * An entity that arranges for an exchange between a buyer and a seller. In most cases a broker never acquires or |
||
| 181 | * releases ownership of a product or service involved in an exchange. If it is not clear whether an entity is a |
||
| 182 | * broker, seller, or buyer, the latter two terms are preferred. Supersedes bookingAgent. |
||
| 183 | * |
||
| 184 | * @param mixed $broker |
||
| 185 | * @return static |
||
| 186 | */ |
||
| 187 | public function setBroker($broker) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Get the broker |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function getBroker() |
||
| 203 | |||
| 204 | private $_category; |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set the category |
||
| 208 | * |
||
| 209 | * A category for the item. Greater signs or slashes can be used to informally indicate a category hierarchy. |
||
| 210 | * |
||
| 211 | * @param mixed $category |
||
| 212 | * @return static |
||
| 213 | */ |
||
| 214 | public function setCategory($category) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get the category |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function getCategory() |
||
| 230 | |||
| 231 | private $_hasOfferCatalog; |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Set whether service has an offer catalog |
||
| 235 | * |
||
| 236 | * Indicates an OfferCatalog listing for this Organization, Person, or Service. |
||
| 237 | * |
||
| 238 | * @param mixed $hasOfferCatalog |
||
| 239 | * @return static |
||
| 240 | */ |
||
| 241 | public function setHasOfferCatalog($hasOfferCatalog) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get the offer catalog indication |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public function getHasOfferCatalog() |
||
| 257 | |||
| 258 | private $_hoursAvailable; |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set the hours available |
||
| 262 | * |
||
| 263 | * The hours during which this service or contact is available. |
||
| 264 | * |
||
| 265 | * @param mixed $hoursAvailable |
||
| 266 | * @return static |
||
| 267 | */ |
||
| 268 | public function setHoursAvailable($hoursAvailable) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get the hours available |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public function getHoursAvailable() |
||
| 284 | |||
| 285 | private $_isRelatedTo; |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set a related product |
||
| 289 | * |
||
| 290 | * A pointer to another, somehow related product (or multiple products). |
||
| 291 | * |
||
| 292 | * @param mixed $isRelatedTo |
||
| 293 | * @return static |
||
| 294 | */ |
||
| 295 | public function setIsRelatedTo($isRelatedTo) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get related items |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function getIsRelatedTo() |
||
| 310 | |||
| 311 | private $_isSimilarTo; |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Set similar items |
||
| 315 | * |
||
| 316 | * A pointer to another, functionally similar product (or multiple products). |
||
| 317 | * |
||
| 318 | * @param mixed $isSimilarTo |
||
| 319 | * @return static |
||
| 320 | */ |
||
| 321 | public function setIsSimilarTo($isSimilarTo) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get similar items |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | public function getIsSimilarTo() |
||
| 337 | |||
| 338 | private $_logo; |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Set the logo |
||
| 342 | * |
||
| 343 | * An associated logo. |
||
| 344 | * |
||
| 345 | * @param mixed $logo |
||
| 346 | * @return static |
||
| 347 | */ |
||
| 348 | public function setLogo($logo) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get the logo |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function getLogo() |
||
| 364 | |||
| 365 | private $_offers; |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Set offers |
||
| 369 | * |
||
| 370 | * An offer to provide this item—for example, an offer to sell a product, rent the DVD of a movie, perform a service, |
||
| 371 | * or give away tickets to an event. |
||
| 372 | * |
||
| 373 | * @param mixed $offers |
||
| 374 | * @return static |
||
| 375 | */ |
||
| 376 | public function setOffers($offers) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get offers |
||
| 385 | * |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | public function getOffers() |
||
| 392 | |||
| 393 | private $_provider; |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Set the service provider |
||
| 397 | * |
||
| 398 | * The service provider, service operator, or service performer; the goods producer. Another party (a seller) may offer those services or goods on behalf of the provider. A provider may also serve as the seller. Supersedes carrier. |
||
| 399 | * |
||
| 400 | * @param mixed $provider |
||
| 401 | * @return static |
||
| 402 | */ |
||
| 403 | public function setProvider($provider) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get the provider |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | public function getProvider() |
||
| 419 | |||
| 420 | private $_providerMobility; |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Set the provider mobility |
||
| 424 | * |
||
| 425 | * Indicates the mobility of a provided service (e.g. 'static', 'dynamic'). |
||
| 426 | * |
||
| 427 | * @param mixed $providerMobility |
||
| 428 | * @return static |
||
| 429 | */ |
||
| 430 | public function setProviderMobility($providerMobility) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Get the provider mobility |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public function getProviderMobility() |
||
| 446 | |||
| 447 | private $_review; |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Set a review |
||
| 451 | * |
||
| 452 | * A review of the item. Supersedes reviews. |
||
| 453 | * |
||
| 454 | * @param mixed $review |
||
| 455 | * @return static |
||
| 456 | */ |
||
| 457 | public function setReview($review) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Get a review |
||
| 466 | * |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public function getReview() |
||
| 473 | |||
| 474 | private $_serviceOutput; |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Set the service output |
||
| 478 | * |
||
| 479 | * The tangible thing generated by the service, e.g. a passport, permit, etc. Supersedes produces. |
||
| 480 | * |
||
| 481 | * @param mixed $serviceOutput |
||
| 482 | * @return static |
||
| 483 | */ |
||
| 484 | public function setServiceOutput($serviceOutput) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Get the service output |
||
| 493 | * |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | public function getServiceOutput() |
||
| 500 | |||
| 501 | private $_serviceType; |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Set the service type |
||
| 505 | * |
||
| 506 | * The type of service being offered, e.g. veterans' benefits, emergency relief, etc. |
||
| 507 | * |
||
| 508 | * @param mixed $serviceType |
||
| 509 | * @return static |
||
| 510 | */ |
||
| 511 | public function setServiceType($serviceType) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Get the service type |
||
| 520 | * |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | public function getServiceType() |
||
| 527 | |||
| 528 | private $_slogan; |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Set the slogan |
||
| 532 | * |
||
| 533 | * A slogan or motto associated with the item. |
||
| 534 | * |
||
| 535 | * @param mixed $slogan |
||
| 536 | * @return static |
||
| 537 | */ |
||
| 538 | public function setSlogan($slogan) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Get the slogan |
||
| 547 | * |
||
| 548 | * @return string |
||
| 549 | */ |
||
| 550 | public function getSlogan() |
||
| 554 | |||
| 555 | private $_termsOfService; |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Set the terms of service |
||
| 559 | * |
||
| 560 | * Human-readable terms of service documentation. |
||
| 561 | * |
||
| 562 | * @param mixed $termsOfService |
||
| 563 | * @return static |
||
| 564 | */ |
||
| 565 | public function setTermsOfService($termsOfService) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Get the terms of service |
||
| 574 | * |
||
| 575 | * @return string |
||
| 576 | */ |
||
| 577 | public function getTermsOfService() |
||
| 581 | } |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.