Completed
Push — IncomprehensibleFinder/base ( b21e9d...348f7f )
by Albert
03:11 queued 01:15
created

Finder::find()   C

Complexity

Conditions 10
Paths 28

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10.0056

Importance

Changes 0
Metric Value
dl 0
loc 48
c 0
b 0
f 0
ccs 25
cts 26
cp 0.9615
rs 5.3454
cc 10
eloc 28
nc 28
nop 1
crap 10.0056

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
declare(strict_types = 1);
4
5
namespace Kata\Algorithm;
6
7
final class Finder
8
{
9
    /** @var Thing[] */
10
    private $_p;
11
12 6
    public function __construct(array $p)
13
    {
14 6
        $this->_p = $p;
15 6
    }
16
17 6
    public function find(int $ft): F
18
    {
19
        /** @var F[] $tr */
20 6
        $tr = [];
21
22 6
        for ($i = 0; $i < count($this->_p); $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->_p); $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
                $r = new F();
25
26 4
                if ($this->_p[$i]->birthDate < $this->_p[$j]->birthDate) {
27 3
                    $r->p1 = $this->_p[$i];
28 3
                    $r->p2 = $this->_p[$j];
29
                } else {
30 3
                    $r->p1 = $this->_p[$j];
31 3
                    $r->p2 = $this->_p[$i];
32
                }
33
34 4
                $r->d = $r->p2->birthDate->getTimestamp()
35 4
                    - $r->p1->birthDate->getTimestamp();
36
37 4
                $tr[] = $r;
38
            }
39
        }
40
41 6
        if (count($tr) < 1) {
42 2
            return new F();
43
        }
44
45 4
        $answer = $tr[0];
46
47 4
        foreach ($tr as $result) {
48
            switch ($ft) {
49 4
                case FT::ONE:
50 2
                    if ($result->d < $answer->d) {
51 1
                        $answer = $result;
52
                    }
53 2
                    break;
54
55 2
                case FT::TWO:
56 2
                    if ($result->d > $answer->d) {
57
                        $answer = $result;
58
                    }
59 4
                    break;
60
            }
61
        }
62
63 4
        return $answer;
64
    }
65
}
66