Conditions | 2 |
Total Lines | 60 |
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 |
||
25 | public function set(array $data): self |
||
26 | { |
||
27 | $iterator = new class ($this->types, $data) implements Iterator { |
||
28 | /** @var array */ |
||
29 | private $types; |
||
30 | |||
31 | /** @var array */ |
||
32 | private $data; |
||
33 | |||
34 | /** @var int */ |
||
35 | private $position; |
||
36 | |||
37 | public function __construct(array $types, array $data) |
||
38 | { |
||
39 | $typeCount = count($types); |
||
40 | |||
41 | $dataCount = count($data); |
||
42 | |||
43 | if ($typeCount !== $dataCount) { |
||
44 | throw WrongType::fromMessage("Tuple count mismatch, excpected exactly {$typeCount} elements, and got {$dataCount}"); |
||
45 | } |
||
46 | |||
47 | $this->types = $types; |
||
48 | $this->data = $data; |
||
49 | $this->position = 0; |
||
50 | } |
||
51 | |||
52 | public function current(): array |
||
53 | { |
||
54 | return ['type' => current($this->types), 'value' => current($this->data)]; |
||
55 | } |
||
56 | |||
57 | public function next(): void |
||
58 | { |
||
59 | $this->position++; |
||
60 | } |
||
61 | |||
62 | public function key(): int |
||
63 | { |
||
64 | return $this->position; |
||
65 | } |
||
66 | |||
67 | public function valid(): bool |
||
68 | { |
||
69 | return isset($this->types[$this->position]) && array_key_exists($this->position, $this->data); |
||
70 | } |
||
71 | |||
72 | public function rewind(): void |
||
73 | { |
||
74 | $this->position = 0; |
||
75 | } |
||
76 | }; |
||
77 | |||
78 | foreach ($iterator as $key => ['type' => $type, 'value' => $value]) { |
||
79 | $data[$key] = $this->validateType($type, $value); |
||
80 | } |
||
81 | |||
82 | $this->data = $data; |
||
83 | |||
84 | return $this; |
||
85 | } |
||
122 |