Conditions | 1 |
Total Lines | 125 |
Lines | 0 |
Ratio | 0 % |
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 |
||
38 | public function providesToArray(): iterable |
||
39 | { |
||
40 | yield [ |
||
41 | 'data' => [1, 3, 4], |
||
42 | 'result' => [1, 3, 4], |
||
43 | ]; |
||
44 | |||
45 | yield [ |
||
46 | 'data' => '1', |
||
47 | 'result' => new RuntimeException('Cannot cast to array'), |
||
48 | ]; |
||
49 | |||
50 | yield [ |
||
51 | 'data' => [ |
||
52 | 2, |
||
53 | (static function () { |
||
54 | $o = new stdClass(); |
||
55 | $o->foo = 'bar'; |
||
56 | $o->debug = true; |
||
57 | |||
58 | return $o; |
||
59 | })(), |
||
60 | ], |
||
61 | 'result' => [ |
||
62 | 2, |
||
63 | [ |
||
64 | 'foo' => 'bar', |
||
65 | 'debug' => true, |
||
66 | ], |
||
67 | ], |
||
68 | ]; |
||
69 | |||
70 | yield [ |
||
71 | 'data' => new class implements Arrayable { |
||
72 | /** |
||
73 | * @return array<mixed> |
||
74 | */ |
||
75 | public function __toArray(): array |
||
76 | { |
||
77 | return ['1', 2, 'test']; |
||
78 | } |
||
79 | }, |
||
80 | 'result' => ['1', 2, 'test'], |
||
81 | ]; |
||
82 | |||
83 | yield [ |
||
84 | 'data' => new class implements CoreArrayable { |
||
85 | /** |
||
86 | * @return array<mixed> |
||
87 | */ |
||
88 | public function __toArray(): array |
||
89 | { |
||
90 | return ['1' => 2, '3' => 5]; |
||
91 | } |
||
92 | }, |
||
93 | 'result' => ['1' => 2, '3' => 5], |
||
94 | ]; |
||
95 | |||
96 | yield [ |
||
97 | 'data' => new ArrayIterator([3, 4, 5]), |
||
98 | 'result' => [3, 4, 5], |
||
99 | ]; |
||
100 | |||
101 | yield [ |
||
102 | 'data' => (static function () { |
||
103 | yield 2.3; |
||
104 | yield 8.10; |
||
105 | })(), |
||
106 | 'result' => [2.3, 8.10], |
||
107 | ]; |
||
108 | |||
109 | yield [ |
||
110 | 'data' => [ |
||
111 | 1, |
||
112 | 3, |
||
113 | [ |
||
114 | 3, |
||
115 | new ArrayIterator([ |
||
116 | 3, |
||
117 | 4, |
||
118 | (static function () { |
||
119 | yield 2.3; |
||
120 | yield new class implements Arrayable { |
||
121 | /** |
||
122 | * @return array<mixed> |
||
123 | */ |
||
124 | public function __toArray(): array |
||
125 | { |
||
126 | return ['1', 2, 'test']; |
||
127 | } |
||
128 | }; |
||
129 | })(), |
||
130 | ]), |
||
131 | ], |
||
132 | ], |
||
133 | 'result' => [1, 3, [3, [3, 4, [2.3, ['1', 2, 'test']]]]], |
||
134 | ]; |
||
135 | |||
136 | yield [ |
||
137 | 'data' => new ArrayIterator([1, new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])])]), |
||
138 | 'result' => [1, [3, [4, [3, 4, 5]]]], |
||
139 | ]; |
||
140 | |||
141 | yield [ |
||
142 | 'data' => new ArrayIterator([1, new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])])]), |
||
143 | 'result' => [1, new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])])], |
||
144 | 'limit' => 1, |
||
145 | ]; |
||
146 | |||
147 | yield [ |
||
148 | 'data' => new ArrayIterator([1, new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])])]), |
||
149 | 'result' => [1, [3, [4, new ArrayIterator([3, 4, 5])]]], |
||
150 | 'limit' => 3, |
||
151 | ]; |
||
152 | |||
153 | yield [ |
||
154 | 'data' => '1', |
||
155 | 'result' => new UnexpectedValueException('Limit value should be positive number'), |
||
156 | 'limit' => -1, |
||
157 | ]; |
||
158 | |||
159 | yield [ |
||
160 | 'data' => '1', |
||
161 | 'result' => new UnexpectedValueException('Limit value should be positive number'), |
||
162 | 'limit' => 0, |
||
163 | ]; |
||
166 |