Completed
Push — IncomprehensibleFinder/xavi ( c40efa...577002 )
by
unknown
02:24
created

Finder::find()   C

Complexity

Conditions 10
Paths 28

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 10.0045

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 27
cts 28
cp 0.9643
rs 6
c 0
b 0
f 0
cc 10
eloc 30
nc 28
nop 1
crap 10.0045

How to fix   Long Method    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
declare(strict_types = 1);
4
5
namespace Kata\Algorithm;
6
7
final class Finder
8
{
9
    /** @var Person[] */
10
    private $people;
11
12 6
    public function __construct(array $a_people)
13
    {
14 6
        $this->people = $a_people;
15 6
    }
16
17 6
    public function find(int $ft): PeopleComparison
18
    {
19
        /** @var PeopleComparison[] $tr */
20 6
        $tr = [];
21
22 6
        for ($i = 0; $i < count($this->people); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
23 5
            for ($j = $i + 1; $j < count($this->people); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
24 4
                $current_people_comparison = new PeopleComparison();
25
26 4
                $first_person = $this->people[$i];
27 4
                $second_person = $this->people[$j];
28
29 4
                if ($this->isFirstPersonMoreYoungerThanSecondPerson($i, $j)) {
30 3
                    $current_people_comparison->setFirstPerson($first_person);
31 3
                    $current_people_comparison->setSecondPerson($second_person);
32
                } else {
33 3
                    $current_people_comparison->setFirstPerson($second_person);
34 3
                    $current_people_comparison->setSecondPerson($first_person);
35
                }
36
37 4
                $current_people_comparison->setBirthdayDifference($current_people_comparison->secondPerson()->birthDate()->getTimestamp()
38 4
                    - $current_people_comparison->firstPerson()->birthDate()->getTimestamp());
39
40 4
                $tr[] = $current_people_comparison;
41
            }
42
        }
43
44 6
        if (empty($tr)) {
45 2
            return new PeopleComparison();
46
        }
47
48 4
        $answer = $tr[0];
49
50 4
        foreach ($tr as $result) {
51
            switch ($ft) {
52 4
                case FT::ONE:
53 2
                    if ($result->birthdayDifference() < $answer->birthdayDifference()) {
54 1
                        $answer = $result;
55
                    }
56 2
                    break;
57
58 2
                case FT::TWO:
59 2
                    if ($result->birthdayDifference() > $answer->birthdayDifference()) {
60
                        $answer = $result;
61
                    }
62 4
                    break;
63
            }
64
        }
65
66 4
        return $answer;
67
    }
68
69 4
    private function isFirstPersonMoreYoungerThanSecondPerson($first_person_index, $second_person_index): bool
70
    {
71 4
        return $this->people[$first_person_index]->birthDate() < $this->people[$second_person_index]->birthDate();
72
    }
73
}
74