Completed
Push — IncomprehensibleFinder/base ( 818385...29033c )
by Albert
07:21
created

Finder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 2
                    break;
60
            }
61
        }
62
63 4
        return $answer;
64
    }
65
}
66