Conditions | 11 |
Paths | 30 |
Total Lines | 53 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
43 | private function permute(array $charset, $length = null): Generator |
||
44 | { |
||
45 | $n = count($charset); |
||
46 | |||
47 | if (null === $length) { |
||
48 | $length = $n; |
||
49 | } |
||
50 | |||
51 | if ($length > $n) { |
||
52 | return; |
||
53 | } |
||
54 | |||
55 | $indices = range(0, $n - 1); |
||
56 | $cycles = range($n, $n - $length + 1, -1); |
||
57 | |||
58 | yield array_slice($charset, 0, $length); |
||
59 | |||
60 | if ($n <= 0) { |
||
61 | return; |
||
62 | } |
||
63 | |||
64 | while (true) { |
||
65 | $exitEarly = false; |
||
66 | for ($i = $length; $i--;) { |
||
67 | $cycles[$i]-= 1; |
||
68 | if ($cycles[$i] == 0) { |
||
69 | if ($i < count($indices)) { |
||
70 | $removed = array_splice($indices, $i, 1); |
||
71 | $indices[] = $removed[0]; |
||
72 | } |
||
73 | $cycles[$i] = $n - $i; |
||
74 | } else { |
||
75 | $j = $cycles[$i]; |
||
76 | $value = $indices[$i]; |
||
77 | $negative = $indices[count($indices) - $j]; |
||
78 | $indices[$i] = $negative; |
||
79 | $indices[count($indices) - $j] = $value; |
||
80 | $result = []; |
||
81 | $counter = 0; |
||
82 | foreach ($indices as $index) { |
||
83 | $result[] = $charset[$index]; |
||
84 | $counter++; |
||
85 | if ($counter == $length) { |
||
86 | break; |
||
87 | } |
||
88 | } |
||
89 | yield $result; |
||
90 | $exitEarly = true; |
||
91 | break; |
||
92 | } |
||
93 | } |
||
94 | if (!$exitEarly) { |
||
95 | break; |
||
96 | } |
||
121 |