| Total Complexity | 40 |
| Total Lines | 592 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Suite 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 Suite, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class Suite |
||
| 47 | { |
||
| 48 | const DEFAULT_NAME = 'DEFAULT_SUITE_NAME'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var int |
||
| 52 | * |
||
| 53 | * @ORM\Column(name="id", type="integer") |
||
| 54 | * @ORM\Id |
||
| 55 | * @ORM\GeneratedValue(strategy="AUTO") |
||
| 56 | * |
||
| 57 | * @Serializer\Exclude |
||
| 58 | */ |
||
| 59 | private $id; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Name of the suite. |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | * |
||
| 66 | * @ORM\Column(name="name", type="string", length=50) |
||
| 67 | * |
||
| 68 | * @Assert\NotBlank() |
||
| 69 | * |
||
| 70 | * @Serializer\Groups({"public", "private"}) |
||
| 71 | */ |
||
| 72 | private $name; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Tests warning limit. |
||
| 76 | * |
||
| 77 | * @var int |
||
| 78 | * |
||
| 79 | * @ORM\Column(name="warning", type="smallint") |
||
| 80 | * |
||
| 81 | * @Assert\NotBlank() |
||
| 82 | * @Assert\Range(min=0, max=100) |
||
| 83 | * |
||
| 84 | * @Serializer\Groups({"public", "private"}) |
||
| 85 | */ |
||
| 86 | private $warning; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Tests success limit. |
||
| 90 | * |
||
| 91 | * @var int |
||
| 92 | * |
||
| 93 | * @ORM\Column(name="success", type="smallint") |
||
| 94 | * |
||
| 95 | * @Assert\NotBlank() |
||
| 96 | * @Assert\Range(min=0, max=100) |
||
| 97 | * |
||
| 98 | * @Serializer\Groups({"public", "private"}) |
||
| 99 | */ |
||
| 100 | private $success; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Total number of passed tests. |
||
| 104 | * |
||
| 105 | * @var int |
||
| 106 | * |
||
| 107 | * @ORM\Column(name="passed", type="integer") |
||
| 108 | * |
||
| 109 | * @Assert\NotBlank() |
||
| 110 | * @Assert\Type("integer") |
||
| 111 | * |
||
| 112 | * @Serializer\Groups({"public", "private"}) |
||
| 113 | */ |
||
| 114 | private $passed = 0; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Total number of disabled tests. |
||
| 118 | * |
||
| 119 | * @var int |
||
| 120 | * |
||
| 121 | * @ORM\Column(name="failed", type="integer") |
||
| 122 | * |
||
| 123 | * @Assert\NotBlank() |
||
| 124 | * @Assert\Type("integer") |
||
| 125 | * |
||
| 126 | * @Serializer\Groups({"public", "private"}) |
||
| 127 | */ |
||
| 128 | private $failed = 0; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Total number of errored tests. |
||
| 132 | * |
||
| 133 | * @var int |
||
| 134 | * |
||
| 135 | * @ORM\Column(name="errored", type="integer") |
||
| 136 | * |
||
| 137 | * @Assert\NotBlank() |
||
| 138 | * @Assert\Type("integer") |
||
| 139 | * |
||
| 140 | * @Serializer\Groups({"public", "private"}) |
||
| 141 | */ |
||
| 142 | private $errored = 0; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Total number of skipped tests. |
||
| 146 | * |
||
| 147 | * @var int |
||
| 148 | * |
||
| 149 | * @ORM\Column(name="skipped", type="integer") |
||
| 150 | * |
||
| 151 | * @Assert\NotBlank() |
||
| 152 | * @Assert\Type("integer") |
||
| 153 | * |
||
| 154 | * @Serializer\Groups({"public", "private"}) |
||
| 155 | */ |
||
| 156 | private $skipped = 0; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Total number of disabled tests. |
||
| 160 | * |
||
| 161 | * @var int |
||
| 162 | * |
||
| 163 | * @ORM\Column(name="disabled", type="integer") |
||
| 164 | * |
||
| 165 | * @Assert\NotBlank() |
||
| 166 | * @Assert\Type("integer") |
||
| 167 | * |
||
| 168 | * @Serializer\Groups({"public", "private"}) |
||
| 169 | */ |
||
| 170 | private $disabled = 0; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Duration of the suite in seconds. |
||
| 174 | * |
||
| 175 | * @var float |
||
| 176 | * |
||
| 177 | * @ORM\Column(name="duration", type="float") |
||
| 178 | * |
||
| 179 | * @Assert\NotBlank() |
||
| 180 | * @Assert\Type("float") |
||
| 181 | * |
||
| 182 | * @Serializer\Groups({"public", "private"}) |
||
| 183 | */ |
||
| 184 | private $duration = 0; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Date time of the suite in ISO 8601 format (2017-07-01T12:30:01+02:00). |
||
| 188 | * |
||
| 189 | * @var DateTime |
||
| 190 | * |
||
| 191 | * @ORM\Column(name="datetime_suite", type="datetime") |
||
| 192 | * |
||
| 193 | * @Assert\NotBlank() |
||
| 194 | * @Assert\DateTime() |
||
| 195 | * |
||
| 196 | * @Serializer\Groups({"public", "private"}) |
||
| 197 | */ |
||
| 198 | protected $datetime; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @Gedmo\SortablePosition |
||
| 202 | * @ORM\Column(name="position", type="integer") |
||
| 203 | * |
||
| 204 | * @Serializer\Exclude |
||
| 205 | */ |
||
| 206 | private $position; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @var Campaign |
||
| 210 | * |
||
| 211 | * @Gedmo\SortableGroup |
||
| 212 | * @ORM\ManyToOne(targetEntity="Campaign") |
||
| 213 | * @ORM\JoinColumn(name="campaign_id", referencedColumnName="id", nullable=false, onDelete="cascade") |
||
| 214 | * |
||
| 215 | * @Serializer\Exclude |
||
| 216 | */ |
||
| 217 | private $campaign; |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Constructor. |
||
| 221 | * |
||
| 222 | * @param Project $project |
||
| 223 | * @param Campaign $campaign |
||
| 224 | */ |
||
| 225 | public function __construct(Project $project, Campaign $campaign) |
||
| 226 | { |
||
| 227 | $this->setWarning($project->getWarning()); |
||
| 228 | $this->setSuccess($project->getSuccess()); |
||
| 229 | $this->setCampaign($campaign); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get id. |
||
| 234 | * |
||
| 235 | * @return int |
||
| 236 | */ |
||
| 237 | public function getId(): int |
||
| 238 | { |
||
| 239 | return $this->id; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Set name. |
||
| 244 | * |
||
| 245 | * @param string $name Name |
||
| 246 | * |
||
| 247 | * @return Suite |
||
| 248 | */ |
||
| 249 | public function setName(string $name): self |
||
| 250 | { |
||
| 251 | $this->name = $name; |
||
| 252 | |||
| 253 | return $this; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get name. |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public function getName(): string |
||
| 262 | { |
||
| 263 | return $this->name; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Set warning limit. |
||
| 268 | * |
||
| 269 | * @param int $warning Warning limit |
||
| 270 | * |
||
| 271 | * @return Suite |
||
| 272 | */ |
||
| 273 | public function setWarning(int $warning): self |
||
| 274 | { |
||
| 275 | $this->warning = $warning; |
||
| 276 | |||
| 277 | return $this; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get warning limit. |
||
| 282 | * |
||
| 283 | * @return int |
||
| 284 | */ |
||
| 285 | public function getWarning(): int |
||
| 286 | { |
||
| 287 | return $this->warning; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Set success limit. |
||
| 292 | * |
||
| 293 | * @param int $success Success limit |
||
| 294 | * |
||
| 295 | * @return Suite |
||
| 296 | */ |
||
| 297 | public function setSuccess(int $success): self |
||
| 298 | { |
||
| 299 | $this->success = $success; |
||
| 300 | |||
| 301 | return $this; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get success limit. |
||
| 306 | * |
||
| 307 | * @return int |
||
| 308 | */ |
||
| 309 | public function getSuccess(): int |
||
| 310 | { |
||
| 311 | return $this->success; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Set passed tests count. |
||
| 316 | * |
||
| 317 | * @param int $passed Passed tests |
||
| 318 | * |
||
| 319 | * @return Suite |
||
| 320 | */ |
||
| 321 | public function setpassed(int $passed): self |
||
| 322 | { |
||
| 323 | $this->passed = $passed; |
||
| 324 | |||
| 325 | return $this; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get passed tests count. |
||
| 330 | * |
||
| 331 | * @return int |
||
| 332 | */ |
||
| 333 | public function getPassed(): int |
||
| 334 | { |
||
| 335 | return $this->passed; |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Set failed tests count. |
||
| 340 | * |
||
| 341 | * @param int $failed Failed tests |
||
| 342 | * |
||
| 343 | * @return Suite |
||
| 344 | */ |
||
| 345 | public function setFailed(int $failed): self |
||
| 346 | { |
||
| 347 | $this->failed = $failed; |
||
| 348 | |||
| 349 | return $this; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get failed tests count. |
||
| 354 | * |
||
| 355 | * @return int |
||
| 356 | */ |
||
| 357 | public function getFailed(): int |
||
| 358 | { |
||
| 359 | return $this->failed; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Set errored tests count. |
||
| 364 | * |
||
| 365 | * @param int $errored Errored tests |
||
| 366 | * |
||
| 367 | * @return Suite |
||
| 368 | */ |
||
| 369 | public function setErrored(int $errored): self |
||
| 370 | { |
||
| 371 | $this->errored = $errored; |
||
| 372 | |||
| 373 | return $this; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get errored tests count. |
||
| 378 | * |
||
| 379 | * @return int |
||
| 380 | */ |
||
| 381 | public function getErrored(): int |
||
| 382 | { |
||
| 383 | return $this->errored; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Set skipped tests count. |
||
| 388 | * |
||
| 389 | * @param int $skipped Skipped tests |
||
| 390 | * |
||
| 391 | * @return Suite |
||
| 392 | */ |
||
| 393 | public function setSkipped(int $skipped): self |
||
| 394 | { |
||
| 395 | $this->skipped = $skipped; |
||
| 396 | |||
| 397 | return $this; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get skipped tests count. |
||
| 402 | * |
||
| 403 | * @return int |
||
| 404 | */ |
||
| 405 | public function getSkipped(): int |
||
| 406 | { |
||
| 407 | return $this->skipped; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Set disabled tests count. |
||
| 412 | * |
||
| 413 | * @param int $disabled Disable tests |
||
| 414 | * |
||
| 415 | * @return Suite |
||
| 416 | */ |
||
| 417 | public function setDisabled(int $disabled): self |
||
| 418 | { |
||
| 419 | $this->disabled = $disabled; |
||
| 420 | |||
| 421 | return $this; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get disabled tests count. |
||
| 426 | * |
||
| 427 | * @return int |
||
| 428 | */ |
||
| 429 | public function getDisabled(): int |
||
| 430 | { |
||
| 431 | return $this->disabled; |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Set duration of the suite in second. |
||
| 436 | * |
||
| 437 | * @param float $duration Duration |
||
| 438 | * |
||
| 439 | * @return Suite |
||
| 440 | */ |
||
| 441 | public function setDuration(float $duration): self |
||
| 442 | { |
||
| 443 | $this->duration = $duration; |
||
| 444 | |||
| 445 | return $this; |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get duration of the suite in seconds. |
||
| 450 | * |
||
| 451 | * @return float |
||
| 452 | */ |
||
| 453 | public function getDuration(): float |
||
| 454 | { |
||
| 455 | return $this->duration; |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Set datetime of suite. |
||
| 460 | * |
||
| 461 | * @param DateTime $datetime DateTime of suite. |
||
| 462 | * |
||
| 463 | * @return Suite |
||
| 464 | */ |
||
| 465 | public function setDateTime(DateTime $datetime): self |
||
| 466 | { |
||
| 467 | $this->datetime = $datetime; |
||
| 468 | |||
| 469 | return $this; |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get datetime of suite. |
||
| 474 | * |
||
| 475 | * @return DateTime |
||
| 476 | */ |
||
| 477 | public function getDateTime(): DateTime |
||
| 478 | { |
||
| 479 | return $this->datetime; |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Set order. |
||
| 484 | * |
||
| 485 | * @param int $position The order. |
||
| 486 | * |
||
| 487 | * @return Suite |
||
| 488 | */ |
||
| 489 | public function setPosition(int $position): self |
||
| 490 | { |
||
| 491 | $this->position = $position; |
||
| 492 | |||
| 493 | return $this; |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get position. |
||
| 498 | * |
||
| 499 | * @return int |
||
| 500 | */ |
||
| 501 | public function getPosition(): int |
||
| 502 | { |
||
| 503 | return $this->position; |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Get reference id (Incremental integer). |
||
| 508 | * |
||
| 509 | * @return int |
||
| 510 | * |
||
| 511 | * @Serializer\VirtualProperty |
||
| 512 | * @Serializer\SerializedName("refid") |
||
| 513 | * @Serializer\Type("int") |
||
| 514 | * @Serializer\Groups({"public", "private"}) |
||
| 515 | */ |
||
| 516 | public function getRefid(): int |
||
| 517 | { |
||
| 518 | return $this->position + 1; |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Set campaign. |
||
| 523 | * |
||
| 524 | * @param Campaign $campaign Campaign |
||
| 525 | * |
||
| 526 | * @return Suite |
||
| 527 | */ |
||
| 528 | public function setCampaign(Campaign $campaign): self |
||
| 529 | { |
||
| 530 | $this->campaign = $campaign; |
||
| 531 | |||
| 532 | return $this; |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Get campaign. |
||
| 537 | * |
||
| 538 | * @return Campaign |
||
| 539 | */ |
||
| 540 | public function getCampaign(): Campaign |
||
| 541 | { |
||
| 542 | return $this->campaign; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Get enabled tests. |
||
| 547 | * |
||
| 548 | * @return int |
||
| 549 | */ |
||
| 550 | public function getEnabled(): int |
||
| 551 | { |
||
| 552 | return $this->passed |
||
| 553 | + $this->failed |
||
| 554 | + $this->errored |
||
| 555 | + $this->skipped; |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Get percentage of successful tests. |
||
| 560 | * |
||
| 561 | * @return float |
||
| 562 | */ |
||
| 563 | public function getPercentage(): float |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Get suite status. |
||
| 574 | * |
||
| 575 | * @return int Status::FAILED|Status::WARNING|Status::SUCCESS |
||
| 576 | */ |
||
| 577 | public function getStatus(): int |
||
| 578 | { |
||
| 579 | if (0 === $this->getEnabled()) { |
||
| 580 | if ((0 === $this->getWarning()) && (0 === $this->getSuccess())) { |
||
| 581 | return Status::SUCCESS; |
||
| 582 | } |
||
| 583 | |||
| 584 | return Status::WARNING; |
||
| 585 | } |
||
| 586 | if ($this->getPercentage() < $this->getWarning()) { |
||
| 587 | return Status::FAILED; |
||
| 588 | } |
||
| 589 | if ($this->getPercentage() < $this->getSuccess()) { |
||
| 590 | return Status::WARNING; |
||
| 591 | } |
||
| 592 | |||
| 593 | return Status::SUCCESS; |
||
| 594 | } |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Set from DTO limits suite. |
||
| 598 | * |
||
| 599 | * @param SuiteLimitsDTO $dto DTO object |
||
| 600 | * |
||
| 601 | * @return Suite |
||
| 602 | */ |
||
| 603 | public function setFromLimitsDTO(SuiteLimitsDTO $dto): self |
||
| 619 | } |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Set from DTO suite. |
||
| 623 | * |
||
| 624 | * @param SuiteDTO $dto DTO object |
||
| 625 | * |
||
| 626 | * @return Suite |
||
| 627 | */ |
||
| 628 | public function setFromDTO(SuiteDTO $dto): self |
||
| 640 |