Conditions | 9 |
Paths | 18 |
Total Lines | 55 |
Code Lines | 31 |
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 declare(strict_types=1); |
||
85 | protected function normalizeTableNode(TableNode $tableNode): TableNode |
||
86 | { |
||
87 | $tableNodeParsedArray = [ |
||
88 | 0 => \array_map( |
||
89 | function (string $column): string { |
||
90 | return \explode(':', $column)[0]; |
||
91 | }, |
||
92 | $tableNode->getRow(0) |
||
93 | ), |
||
94 | ]; |
||
95 | |||
96 | foreach ($tableNode as $rowIndex => $nodeRow) { |
||
97 | $nodeRowParsed = []; |
||
98 | foreach ($nodeRow as $cellNameWithDataType => $cellValue) { |
||
99 | $cellNameWithDataType = \explode(':', $cellNameWithDataType); |
||
100 | $cellDataType = $cellNameWithDataType[1] ?? false; |
||
101 | |||
102 | if (false !== $cellDataType) { |
||
103 | if (0 === \strpos($cellDataType, '?')) { |
||
104 | if ('' === $cellValue) { |
||
105 | $cellValue = null; |
||
106 | } else { |
||
107 | $cellDataType = \ltrim($cellDataType, '?'); |
||
108 | \settype($cellValue, $cellDataType); |
||
109 | } |
||
110 | } else { |
||
111 | \settype($cellValue, $cellDataType); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | $nodeRowParsed[] = $cellValue; |
||
116 | } |
||
117 | |||
118 | $tableNodeParsedArray[$rowIndex + 1] = $nodeRowParsed; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Handling node validation. This is known exception, as it checks for type of string in cell values. If it is |
||
123 | * not string, exception is thrown, but rest validation is valid. |
||
124 | */ |
||
125 | try { |
||
126 | new TableNode($tableNodeParsedArray); |
||
127 | } catch (\Throwable $e) { |
||
128 | if (70 !== $e->getLine() || 'Table is not two-dimensional.' !== $e->getMessage()) { |
||
129 | throw $e; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | $tableNodeParsed = new TableNode([]); |
||
134 | |||
135 | $tableProperty = new \ReflectionProperty($tableNodeParsed, 'table'); |
||
136 | $tableProperty->setAccessible(true); |
||
137 | $tableProperty->setValue($tableNodeParsed, $tableNodeParsedArray); |
||
138 | |||
139 | return $tableNodeParsed; |
||
140 | } |
||
172 |