| Total Complexity | 62 |
| Total Lines | 531 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Wander 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.
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 Wander, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class Wander |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @ORM\Id |
||
| 45 | * @ORM\GeneratedValue |
||
| 46 | * @ORM\Column(type="integer") |
||
| 47 | * |
||
| 48 | * @Groups({"wander:list", "wander:item"}) |
||
| 49 | */ |
||
| 50 | private $id; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @ORM\Column(type="string", length=1024) |
||
| 54 | * |
||
| 55 | * @Groups({"wander:list", "wander:item"}) |
||
| 56 | */ |
||
| 57 | private $title; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @ORM\Column(type="datetime") |
||
| 61 | * |
||
| 62 | * @Groups({"wander:list", "wander:item"}) |
||
| 63 | */ |
||
| 64 | private $startTime; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @ORM\Column(type="datetime") |
||
| 68 | * |
||
| 69 | * @Groups({"wander:list", "wander:item"}) |
||
| 70 | */ |
||
| 71 | private $endTime; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @ORM\Column(type="text", nullable=true) |
||
| 75 | * |
||
| 76 | * @Groups({"wander:list", "wander:item"}) |
||
| 77 | */ |
||
| 78 | private $description; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @ORM\Column(type="string", length=255) |
||
| 82 | * |
||
| 83 | * @Groups({"wander:list", "wander:item"}) |
||
| 84 | */ |
||
| 85 | private $gpxFilename; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @ORM\OneToMany(targetEntity=Image::class, mappedBy="wander", cascade={"persist"}) |
||
| 89 | * @ORM\OrderBy({ "capturedAt" = "ASC", "id" = "ASC" }) |
||
| 90 | * |
||
| 91 | * @Groups({"wander:item"}) |
||
| 92 | * @ApiSubresource |
||
| 93 | * @ ApiProperty(attributes={"fetchEager": true}) |
||
| 94 | */ |
||
| 95 | private $images; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @ORM\Column(type="float", nullable=true) |
||
| 99 | * |
||
| 100 | * Distance walked, in metres. |
||
| 101 | * |
||
| 102 | */ |
||
| 103 | private $distance; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @ORM\Column(type="float", nullable=true) |
||
| 107 | */ |
||
| 108 | private $avgSpeed; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @ORM\Column(type="float", nullable=true) |
||
| 112 | */ |
||
| 113 | private $avgPace; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @ORM\Column(type="float", nullable=true) |
||
| 117 | */ |
||
| 118 | private $minAltitude; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @ORM\Column(type="float", nullable=true) |
||
| 122 | */ |
||
| 123 | private $maxAltitude; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @ORM\Column(type="float", nullable=true) |
||
| 127 | */ |
||
| 128 | private $cumulativeElevationGain; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var string|null |
||
| 132 | * |
||
| 133 | * @ApiProperty(iri="http://schema.org/contentUrl") |
||
| 134 | * @Groups({"wander:list", "wander:item"}) |
||
| 135 | */ |
||
| 136 | public $contentUrl; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @ORM\Column(type="array", nullable=true) |
||
| 140 | */ |
||
| 141 | private $centroid = []; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @ORM\Column(type="float", nullable=true) |
||
| 145 | */ |
||
| 146 | private $angleFromHome; |
||
| 147 | |||
| 148 | public function __construct() |
||
| 149 | { |
||
| 150 | $this->images = new ArrayCollection(); |
||
| 151 | } |
||
| 152 | |||
| 153 | public function getId(): ?int |
||
| 154 | { |
||
| 155 | return $this->id; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function getTitle(): ?string |
||
| 159 | { |
||
| 160 | return $this->title; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function setTitle(string $title): self |
||
| 164 | { |
||
| 165 | $this->title = $title; |
||
| 166 | |||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function getStartTime(): ?\DateTimeInterface |
||
| 171 | { |
||
| 172 | return $this->startTime; |
||
| 173 | } |
||
| 174 | |||
| 175 | public function setStartTime(\DateTimeInterface $startTime): self |
||
| 176 | { |
||
| 177 | $this->startTime = $startTime; |
||
| 178 | |||
| 179 | return $this; |
||
| 180 | } |
||
| 181 | |||
| 182 | public function getEndTime(): ?\DateTimeInterface |
||
| 183 | { |
||
| 184 | return $this->endTime; |
||
| 185 | } |
||
| 186 | |||
| 187 | public function setEndTime(\DateTimeInterface $endTime): self |
||
| 188 | { |
||
| 189 | $this->endTime = $endTime; |
||
| 190 | |||
| 191 | return $this; |
||
| 192 | } |
||
| 193 | |||
| 194 | public function getDescription(): ?string |
||
| 195 | { |
||
| 196 | return $this->description; |
||
| 197 | } |
||
| 198 | |||
| 199 | public function setDescription(?string $description): self |
||
| 200 | { |
||
| 201 | $this->description = $description; |
||
| 202 | |||
| 203 | return $this; |
||
| 204 | } |
||
| 205 | |||
| 206 | public function getGpxFilename(): ?string |
||
| 207 | { |
||
| 208 | return $this->gpxFilename; |
||
| 209 | } |
||
| 210 | |||
| 211 | public function setGpxFilename(string $gpxFilename): self |
||
| 212 | { |
||
| 213 | $this->gpxFilename = $gpxFilename; |
||
| 214 | |||
| 215 | return $this; |
||
| 216 | } |
||
| 217 | |||
| 218 | public function isTimeLengthSuspicious() |
||
| 219 | { |
||
| 220 | $interval = $this->startTime->diff($this->endTime, true); |
||
| 221 | return $interval->h > 12; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return Collection|Image[] |
||
| 226 | */ |
||
| 227 | public function getImages(): Collection |
||
| 228 | { |
||
| 229 | return $this->images; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return Collection|Image[] |
||
| 234 | */ |
||
| 235 | public function getImagesWithNoTitle(): Collection |
||
| 236 | { |
||
| 237 | $criteria = Criteria::create() |
||
| 238 | ->where(Criteria::expr()->isNull('title')); |
||
| 239 | return $this->getImages()->matching($criteria); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return Collection|Image[] |
||
| 244 | */ |
||
| 245 | public function getImagesWithNoLatLng(): Collection |
||
| 246 | { |
||
| 247 | $criteria = Criteria::create() |
||
| 248 | ->where(Criteria::expr()->isNull('latlng')); |
||
| 249 | return $this->getImages()->matching($criteria); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return Collection|Image[] |
||
| 254 | */ |
||
| 255 | public function getImagesWithNoLocation(): Collection |
||
| 256 | { |
||
| 257 | $criteria = Criteria::create() |
||
| 258 | ->where(Criteria::expr()->isNull('location')); |
||
| 259 | return $this->getImages()->matching($criteria); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return Collection|Image[] |
||
| 264 | */ |
||
| 265 | public function getImagesWithNoRating(): Collection |
||
| 266 | { |
||
| 267 | $criteria = Criteria::create() |
||
| 268 | ->where(Criteria::expr()->isNull('rating')); |
||
| 269 | return $this->getImages()->matching($criteria); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return Collection|Image[] |
||
| 274 | */ |
||
| 275 | public function getImagesWithNoTags(): Collection |
||
| 276 | { |
||
| 277 | return $this->getImages()->filter(function($image) { |
||
| 278 | return $image->getTags()->isEmpty(); |
||
| 279 | }); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return Collection|Image[] |
||
| 284 | */ |
||
| 285 | public function getImagesWithNoAutoTags(): Collection |
||
| 286 | { |
||
| 287 | return $this->getImages()->filter(function($image) { |
||
| 288 | return $image->getAutoTagsCount() == 0; |
||
| 289 | }); |
||
| 290 | } |
||
| 291 | |||
| 292 | public function addImage(Image $image): self |
||
| 293 | { |
||
| 294 | if (!$this->images->contains($image)) { |
||
| 295 | $this->images[] = $image; |
||
| 296 | $image->setWander($this); |
||
| 297 | } |
||
| 298 | |||
| 299 | return $this; |
||
| 300 | } |
||
| 301 | |||
| 302 | public function removeImage(Image $image): self |
||
| 303 | { |
||
| 304 | $image->setWander(null); |
||
| 305 | $this->images->removeElement($image); |
||
| 306 | |||
| 307 | return $this; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @ORM\PreRemove |
||
| 312 | */ |
||
| 313 | public function removeAllImages(): self |
||
| 314 | { |
||
| 315 | $images = $this->images; |
||
| 316 | foreach ($images as $image) { |
||
| 317 | $this->removeImage($image); |
||
| 318 | } |
||
| 319 | return $this; |
||
| 320 | } |
||
| 321 | |||
| 322 | public function getDistance(): ?float |
||
| 323 | { |
||
| 324 | return $this->distance; |
||
| 325 | } |
||
| 326 | |||
| 327 | public function setDistance(?float $distance): self |
||
| 328 | { |
||
| 329 | $this->distance = $distance; |
||
| 330 | |||
| 331 | return $this; |
||
| 332 | } |
||
| 333 | |||
| 334 | public function getAvgSpeed(): ?float |
||
| 335 | { |
||
| 336 | return $this->avgSpeed; |
||
| 337 | } |
||
| 338 | |||
| 339 | public function setAvgSpeed(?float $avgSpeed): self |
||
| 340 | { |
||
| 341 | $this->avgSpeed = $avgSpeed; |
||
| 342 | |||
| 343 | return $this; |
||
| 344 | } |
||
| 345 | |||
| 346 | public function getAvgPace(): ?float |
||
| 347 | { |
||
| 348 | return $this->avgPace; |
||
| 349 | } |
||
| 350 | |||
| 351 | public function setAvgPace(?float $avgPace): self |
||
| 352 | { |
||
| 353 | $this->avgPace = $avgPace; |
||
| 354 | |||
| 355 | return $this; |
||
| 356 | } |
||
| 357 | |||
| 358 | public function getMinAltitude(): ?float |
||
| 359 | { |
||
| 360 | return $this->minAltitude; |
||
| 361 | } |
||
| 362 | |||
| 363 | public function setMinAltitude(?float $minAltitude): self |
||
| 364 | { |
||
| 365 | $this->minAltitude = $minAltitude; |
||
| 366 | |||
| 367 | return $this; |
||
| 368 | } |
||
| 369 | |||
| 370 | public function getMaxAltitude(): ?float |
||
| 371 | { |
||
| 372 | return $this->maxAltitude; |
||
| 373 | } |
||
| 374 | |||
| 375 | public function setMaxAltitude(?float $maxAltitude): self |
||
| 376 | { |
||
| 377 | $this->maxAltitude = $maxAltitude; |
||
| 378 | |||
| 379 | return $this; |
||
| 380 | } |
||
| 381 | |||
| 382 | public function getCumulativeElevationGain(): ?float |
||
| 383 | { |
||
| 384 | return $this->cumulativeElevationGain; |
||
| 385 | } |
||
| 386 | |||
| 387 | public function setCumulativeElevationGain(?float $cumulativeElevationGain): self |
||
| 388 | { |
||
| 389 | $this->cumulativeElevationGain = $cumulativeElevationGain; |
||
| 390 | |||
| 391 | return $this; |
||
| 392 | } |
||
| 393 | |||
| 394 | public function getDuration(): ?CarbonInterval |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @return array<float> |
||
| 406 | */ |
||
| 407 | public function getCentroid(): ?array |
||
| 408 | { |
||
| 409 | return $this->centroid; |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param array<float> $centroid |
||
| 414 | */ |
||
| 415 | public function setCentroid(?array $centroid): self |
||
| 416 | { |
||
| 417 | $this->centroid = $centroid; |
||
| 418 | |||
| 419 | return $this; |
||
| 420 | } |
||
| 421 | |||
| 422 | // We could calculate the angle from the Centroid each time, but |
||
| 423 | // for now I'm just going to store it to save time. |
||
| 424 | public function getAngleFromHome(): ?float |
||
| 427 | } |
||
| 428 | |||
| 429 | public function setAngleFromHome(?float $angleFromHome): self |
||
| 430 | { |
||
| 431 | $this->angleFromHome = $angleFromHome; |
||
| 432 | |||
| 433 | return $this; |
||
| 434 | } |
||
| 435 | |||
| 436 | // Utilities |
||
| 437 | |||
| 438 | // TODO: This is more like display code. Perhaps this should be in |
||
| 439 | // a Twig extension and we should stick to storing the angle in here. |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @var array<int, string> |
||
| 443 | */ |
||
| 444 | private static $compass = [ |
||
| 445 | 0 => 'N', |
||
| 446 | 1 => 'NE', |
||
| 447 | 2 => 'NE', |
||
| 448 | 3 => 'E', |
||
| 449 | 4 => 'E', |
||
| 450 | 5 => 'SE', |
||
| 451 | 6 => 'SE', |
||
| 452 | 7 => 'S', |
||
| 453 | 8 => 'S', |
||
| 454 | 9 => 'SW', |
||
| 455 | 10 => 'SW', |
||
| 456 | 11 => 'W', |
||
| 457 | 12 => 'W', |
||
| 458 | 13 => 'NW', |
||
| 459 | 14 => 'NW', |
||
| 460 | 15 => 'N' |
||
| 461 | ]; |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @ORM\Column(type="text", nullable=true) |
||
| 465 | * |
||
| 466 | * NB: We're not using this at the moment (we've replaced it with the much more compact |
||
| 467 | * Google polyline encoding of $googlePolyline) but we might want to again later, so I'm |
||
| 468 | * not removing it from the entity for now. |
||
| 469 | * |
||
| 470 | */ |
||
| 471 | private $geoJson; |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @ORM\OneToOne(targetEntity=Image::class, mappedBy="featuringWander", cascade={"persist"}) |
||
| 475 | * @Groups({"wander:item"}) |
||
| 476 | * @ApiSubresource |
||
| 477 | * @ ApiProperty(attributes={"fetchEager": true}) |
||
| 478 | */ |
||
| 479 | private $featuredImage; |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @ORM\Column(type="text", nullable=true) |
||
| 483 | * |
||
| 484 | * @Groups({"wander:list", "wander:item"}) |
||
| 485 | * |
||
| 486 | */ |
||
| 487 | private $googlePolyline; |
||
| 488 | |||
| 489 | public function getSector(): ?string |
||
| 490 | { |
||
| 491 | if ($this->angleFromHome !== null) { |
||
| 492 | if ($this->angleFromHome >= 0 && $this->angleFromHome < 360.0) { |
||
| 493 | return self::$compass[floor($this->angleFromHome / 22.5)]; |
||
| 494 | } |
||
| 495 | } |
||
| 496 | return null; |
||
| 497 | } |
||
| 498 | |||
| 499 | public function getCentroidAsString(): ?string { |
||
| 500 | if ($this->centroid !== null) { |
||
| 501 | return implode(", ", $this->centroid); |
||
| 502 | } |
||
| 503 | return null; |
||
| 504 | } |
||
| 505 | |||
| 506 | // TODO: Put this in but didn't use it in the end; maybe I don't need it |
||
| 507 | // as in Twig we can use wander.images.count anyway. Take out? |
||
| 508 | public function getImageCount(): int |
||
| 509 | { |
||
| 510 | // https://stackoverflow.com/a/8511611/300836 |
||
| 511 | return $this->images->count(); |
||
| 512 | } |
||
| 513 | |||
| 514 | public function __toString():string |
||
| 515 | { |
||
| 516 | $result = $this->title ?? ''; |
||
| 517 | if (isset($this->startTime)) { |
||
| 518 | $result .= ' (' . $this->startTime->format('j M Y') . ')'; |
||
| 519 | } |
||
| 520 | return $result; |
||
| 521 | } |
||
| 522 | |||
| 523 | public function getGeoJson(): ?string |
||
| 524 | { |
||
| 525 | return $this->geoJson; |
||
| 526 | } |
||
| 527 | |||
| 528 | public function setGeoJson(?string $geoJson): self |
||
| 529 | { |
||
| 530 | $this->geoJson = $geoJson; |
||
| 531 | |||
| 532 | return $this; |
||
| 533 | } |
||
| 534 | |||
| 535 | public function getFeaturedImage(): ?Image |
||
| 536 | { |
||
| 537 | return $this->featuredImage; |
||
| 538 | } |
||
| 539 | |||
| 540 | public function hasFeaturedImage(): bool |
||
| 541 | { |
||
| 542 | return $this->featuredImage !== null; |
||
| 543 | } |
||
| 544 | |||
| 545 | public function setFeaturedImage(?Image $featuredImage): self |
||
| 560 | } |
||
| 561 | |||
| 562 | public function getGooglePolyline(): ?string |
||
| 565 | } |
||
| 566 | |||
| 567 | public function setGooglePolyline(?string $googlePolyline): self |
||
| 568 | { |
||
| 569 | $this->googlePolyline = $googlePolyline; |
||
| 570 | |||
| 571 | return $this; |
||
| 572 | } |
||
| 573 | } |
||
| 574 |