Conditions | 10 |
Paths | 28 |
Total Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Tests | 25 |
CRAP Score | 10.0056 |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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++) { |
|
|
|||
23 | 5 | for ($j = $i + 1; $j < count($this->_p); $j++) { |
|
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 |
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: