Completed
Push — IncomprehensibleFinder/koldo-x... ( 1ccc0f )
by Xavier Serrat
02:32
created

Finder::find()   C

Complexity

Conditions 11
Paths 35

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 11.0044

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 29
cts 30
cp 0.9667
rs 6.7696
c 0
b 0
f 0
cc 11
nc 35
nop 1
crap 11.0044

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
use DateTime;
8
use function dump;
9
10
final class Finder
11
{
12
    /** @var Person[] */
13
    private $peopleByAgeDifferenceCollection;
14
15 6
    public function __construct(PeopleByAgeDifferenceCollection $peopleByAgeDifferenceCollection)
16
    {
17 6
        $this->peopleByAgeDifferenceCollection = $peopleByAgeDifferenceCollection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $peopleByAgeDifferenceCollection of type object<Kata\Algorithm\Pe...geDifferenceCollection> is incompatible with the declared type array<integer,object<Kata\Algorithm\Person>> of property $peopleByAgeDifferenceCollection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
18 6
    }
19
20 6
    public function find(int $finderCriteria): PeopleByAgeDifference
21
    {
22
        /** @var PeopleByAgeDifference[] $tr */
23 6
        $tr = [];
24
25
        /** @var Person $firstPerson */
26 6
        foreach ($this->peopleByAgeDifferenceCollection as $firstPerson) {
27
            /** @var Person $secondPerson */
28 5
            foreach ($this->peopleByAgeDifferenceCollection as $secondPerson) {
29 5
                if ($firstPerson->equals($secondPerson)){
0 ignored issues
show
Documentation introduced by
$secondPerson is of type object<Kata\Algorithm\Person>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30 5
                    continue;
31
                }
32 4
                $peopleByAgeDifference = new PeopleByAgeDifference();
33
34 4
                if ($this->isFirstBirthdaySmallerThanSecondOne(
35 4
                    $firstPerson->birthDate,
36 4
                    $secondPerson->birthDate
37
                )
38
                ) {
39 4
                    $peopleByAgeDifference->firstPerson = $firstPerson;
40 4
                    $peopleByAgeDifference->secondPerson = $secondPerson;
41
                } else {
42 4
                    $peopleByAgeDifference->firstPerson = $secondPerson;
43 4
                    $peopleByAgeDifference->secondPerson = $firstPerson;
44
                }
45
46 4
                $peopleByAgeDifference->timeDifference = $peopleByAgeDifference->secondPerson->birthDate->getTimestamp()
47 4
                    - $peopleByAgeDifference->firstPerson->birthDate->getTimestamp();
48
49 4
                $tr[] = $peopleByAgeDifference;
50
51
            }
52
        }
53
54 6
        if (count($tr) < 1) {
55 2
            return new PeopleByAgeDifference();
56
        }
57
58 4
        $answer = $tr[0];
59
60 4
        foreach ($tr as $result) {
61
            switch ($finderCriteria) {
62 4
                case FinderCriteria::CLOSEST:
63 2
                    if ($result->timeDifference < $answer->timeDifference) {
64 1
                        $answer = $result;
65
                    }
66 2
                    break;
67
68 2
                case FinderCriteria::FURTHEST:
69 2
                    if ($result->timeDifference > $answer->timeDifference) {
70
                        $answer = $result;
71
                    }
72 2
                    break;
73
            }
74
        }
75
76 4
        return $answer;
77
    }
78
79 4
    private function isFirstBirthdaySmallerThanSecondOne(
80
        DateTime $firstBirthday,
81
        DateTime $secondBirthday
82
    ): bool {
83 4
        return $firstBirthday < $secondBirthday;
84
    }
85
}
86