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 Override;
0 ignored issues
show
Bug introduced by
The type Override 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...
8
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
9
use Stu\Orm\Entity\Ship;
10
use Stu\Orm\Entity\Spacecraft;
11
use Stu\Orm\Entity\User;
12
13
class SpacecraftGroup implements SpacecraftGroupInterface
14
{
15
    /** @var Collection<int, SpacecraftWrapperInterface> */
16
    private Collection $spacecraftWrappers;
17
18 8
    public function __construct(
19
        private String $name,
20
        private ?User $user
21
    ) {
22 8
        $this->spacecraftWrappers = new ArrayCollection();
23
    }
24
25 8
    #[Override]
26
    public function addSpacecraftWrapper(SpacecraftWrapperInterface $wrapper): void
27
    {
28 8
        $this->spacecraftWrappers->add($wrapper);
29
    }
30
31 8
    #[Override]
32
    public function getWrappers(): Collection
33
    {
34 8
        return $this->spacecraftWrappers;
35
    }
36
37 3
    #[Override]
38
    public function getName(): string
39
    {
40 3
        return $this->name;
41
    }
42
43 3
    #[Override]
44
    public function getUser(): ?User
45
    {
46 3
        return $this->user;
47
    }
48
49
    /** 
50
     * @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...
51
     * 
52
     * @return Collection<int, Spacecraft>
53
     */
54 8
    public static function sortSpacecraftCollection(Collection $spacecrafts): Collection
55
    {
56 8
        $spacecraftArray = $spacecrafts->toArray();
57
58 8
        usort(
59 8
            $spacecraftArray,
60 8
            function (Spacecraft $a, Spacecraft $b): int {
61 7
                $fleetA = $a instanceof Ship ? $a->getFleet() : null;
62 7
                $fleetB = $b instanceof Ship ? $b->getFleet() : null;
63
64 7
                $fleetASort = $fleetA !== null ? $fleetA->getSort() : 0;
65 7
                $fleetBSort = $fleetB !== null ? $fleetB->getSort() : 0;
66 7
                if ($fleetBSort === $fleetASort) {
67 7
                    $fleetAid = $fleetA !== null ? $fleetA->getId() : 0;
68 7
                    $fleetBid = $fleetB !== null ? $fleetB->getId() : 0;
69 7
                    if ($fleetBid === $fleetAid) {
70 7
                        $aIsFleetLeader = $a instanceof Ship && $a->isFleetLeader();
71 7
                        $bIsFleetLeader = $b instanceof Ship && $b->isFleetLeader();
72 7
                        if ($bIsFleetLeader === $aIsFleetLeader) {
73 7
                            $catA = $a->getRump()->getCategoryId();
74 7
                            $catB = $b->getRump()->getCategoryId();
75 7
                            if ($catB === $catA) {
76 7
                                $roleA = $a->getRump()->getRoleId();
77 7
                                $roleB = $b->getRump()->getRoleId();
78 7
                                if ($roleB === $roleA) {
79 7
                                    if ($b->getRumpId() === $a->getRumpId()) {
80 7
                                        return $a->getName() <=> $b->getName();
81
                                    }
82
83
                                    return $b->getRumpId() <=> $a->getRumpId();
84
                                }
85
86
                                return $roleB <=> $roleA;
87
                            }
88
                            return $catB <=> $catA;
89
                        }
90 1
                        return $bIsFleetLeader <=> $aIsFleetLeader;
91
                    }
92
                    return $fleetBid <=> $fleetAid;
93
                }
94 7
                return $fleetBSort <=> $fleetASort;
95 8
            }
96 8
        );
97
98 8
        return new ArrayCollection($spacecraftArray);
99
    }
100
}
101