SpacecraftGroup::sortSpacecraftCollection()   C
last analyzed

Complexity

Conditions 15
Paths 1

Size

Total Lines 45
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 15.4005

Importance

Changes 0
Metric Value
cc 15
eloc 30
nc 1
nop 1
dl 0
loc 45
ccs 29
cts 33
cp 0.8788
crap 15.4005
rs 5.9166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Stu\Module\Spacecraft\Lib;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
8
use Stu\Orm\Entity\Ship;
9
use Stu\Orm\Entity\Spacecraft;
10
use Stu\Orm\Entity\User;
11
12
class SpacecraftGroup implements SpacecraftGroupInterface
13
{
14
    /** @var Collection<int, SpacecraftWrapperInterface> */
15
    private Collection $spacecraftWrappers;
16
17 8
    public function __construct(
18
        private String $name,
19
        private ?User $user
20
    ) {
21 8
        $this->spacecraftWrappers = new ArrayCollection();
22
    }
23
24 8
    #[\Override]
25
    public function addSpacecraftWrapper(SpacecraftWrapperInterface $wrapper): void
26
    {
27 8
        $this->spacecraftWrappers->add($wrapper);
28
    }
29
30 8
    #[\Override]
31
    public function getWrappers(): Collection
32
    {
33 8
        return $this->spacecraftWrappers;
34
    }
35
36 3
    #[\Override]
37
    public function getName(): string
38
    {
39 3
        return $this->name;
40
    }
41
42 3
    #[\Override]
43
    public function getUser(): ?User
44
    {
45 3
        return $this->user;
46
    }
47
48
    /** 
49
     * @param Collection<int, covariant Spacecraft> $spacecrafts 
0 ignored issues
show
Bug introduced by
The type Stu\Module\Spacecraft\Lib\covariant was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
     * 
51
     * @return Collection<int, Spacecraft>
52
     */
53 8
    public static function sortSpacecraftCollection(Collection $spacecrafts): Collection
54
    {
55 8
        $spacecraftArray = $spacecrafts->toArray();
56
57 8
        usort(
58 8
            $spacecraftArray,
59 8
            function (Spacecraft $a, Spacecraft $b): int {
60 7
                $fleetA = $a instanceof Ship ? $a->getFleet() : null;
61 7
                $fleetB = $b instanceof Ship ? $b->getFleet() : null;
62
63 7
                $fleetASort = $fleetA !== null ? $fleetA->getSort() : 0;
64 7
                $fleetBSort = $fleetB !== null ? $fleetB->getSort() : 0;
65 7
                if ($fleetBSort === $fleetASort) {
66 7
                    $fleetAid = $fleetA !== null ? $fleetA->getId() : 0;
67 7
                    $fleetBid = $fleetB !== null ? $fleetB->getId() : 0;
68 7
                    if ($fleetBid === $fleetAid) {
69 7
                        $aIsFleetLeader = $a instanceof Ship && $a->isFleetLeader();
70 7
                        $bIsFleetLeader = $b instanceof Ship && $b->isFleetLeader();
71 7
                        if ($bIsFleetLeader === $aIsFleetLeader) {
72 7
                            $catA = $a->getRump()->getCategoryId();
73 7
                            $catB = $b->getRump()->getCategoryId();
74 7
                            if ($catB === $catA) {
75 7
                                $roleA = $a->getRump()->getRoleId();
76 7
                                $roleB = $b->getRump()->getRoleId();
77 7
                                if ($roleB === $roleA) {
78 7
                                    if ($b->getRumpId() === $a->getRumpId()) {
79 7
                                        return $a->getName() <=> $b->getName();
80
                                    }
81
82
                                    return $b->getRumpId() <=> $a->getRumpId();
83
                                }
84
85
                                return $roleB <=> $roleA;
86
                            }
87
                            return $catB <=> $catA;
88
                        }
89 1
                        return $bIsFleetLeader <=> $aIsFleetLeader;
90
                    }
91
                    return $fleetBid <=> $fleetAid;
92
                }
93 7
                return $fleetBSort <=> $fleetASort;
94 8
            }
95 8
        );
96
97 8
        return new ArrayCollection($spacecraftArray);
98
    }
99
}
100