Passed
Pull Request — master (#1969)
by Janko
22:34 queued 10:03
created

SpacecraftGroup::sortSpacecraftCollection()   C

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\ShipInterface;
10
use Stu\Orm\Entity\SpacecraftInterface;
11
use Stu\Orm\Entity\UserInterface;
12
13
class SpacecraftGroup implements SpacecraftGroupInterface
14
{
15
    /** @var Collection<int, SpacecraftWrapperInterface> */
16
    private Collection $spacecraftWrappers;
17
18 3
    public function __construct(
19
        private String $name,
20
        private ?UserInterface $user
21
    ) {
22 3
        $this->spacecraftWrappers = new ArrayCollection();
23
    }
24
25 3
    #[Override]
26
    public function addSpacecraftWrapper(SpacecraftWrapperInterface $wrapper): void
27
    {
28 3
        $this->spacecraftWrappers->add($wrapper);
29
    }
30
31 3
    #[Override]
32
    public function getWrappers(): Collection
33
    {
34 3
        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(): ?UserInterface
45
    {
46 3
        return $this->user;
47
    }
48
49
    /** 
50
     * @param Collection<int, covariant SpacecraftInterface> $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, SpacecraftInterface>
53
     */
54 3
    public static function sortSpacecraftCollection(Collection $spacecrafts): Collection
55
    {
56 3
        $spacecraftArray = $spacecrafts->toArray();
57
58 3
        usort(
59 3
            $spacecraftArray,
60 3
            function (SpacecraftInterface $a, SpacecraftInterface $b): int {
61 2
                $fleetA = $a instanceof ShipInterface ? $a->getFleet() : null;
62 2
                $fleetB = $b instanceof ShipInterface ? $b->getFleet() : null;
63
64 2
                $fleetASort = $fleetA !== null ? $fleetA->getSort() : 0;
65 2
                $fleetBSort = $fleetB !== null ? $fleetB->getSort() : 0;
66 2
                if ($fleetBSort === $fleetASort) {
67 1
                    $fleetAid = $fleetA !== null ? $fleetA->getId() : 0;
68 1
                    $fleetBid = $fleetB !== null ? $fleetB->getId() : 0;
69 1
                    if ($fleetBid === $fleetAid) {
70 1
                        $aIsFleetLeader = $a instanceof ShipInterface ? $a->isFleetLeader() : false;
71 1
                        $bIsFleetLeader = $b instanceof ShipInterface ? $b->isFleetLeader() : false;
72 1
                        if ($bIsFleetLeader === $aIsFleetLeader) {
73 1
                            $catA = $a->getRump()->getCategoryId();
74 1
                            $catB = $b->getRump()->getCategoryId();
75 1
                            if ($catB === $catA) {
76 1
                                $roleA = $a->getRump()->getRoleId();
77 1
                                $roleB = $b->getRump()->getRoleId();
78 1
                                if ($roleB === $roleA) {
79 1
                                    if ($b->getRumpId() === $a->getRumpId()) {
80 1
                                        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 2
                return $fleetBSort <=> $fleetASort;
95 3
            }
96 3
        );
97
98 3
        return new ArrayCollection($spacecraftArray);
99
    }
100
}
101