Conditions | 15 |
Paths | 640 |
Total Lines | 80 |
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 |
||
97 | protected function loadCsv(CsvImportService $csv, &$errors) |
||
98 | { |
||
99 | $columnConfig = $this->getColumnConfig(); |
||
100 | |||
101 | if ($csv === false) { |
||
102 | $errors[] = trans('csvimport.text.error.format_invalid'); |
||
103 | } |
||
104 | |||
105 | // 必須カラムの確認 |
||
106 | $requiredColumns = array_map(function ($value) { |
||
107 | return $value['name']; |
||
108 | }, array_filter($columnConfig, function ($value) { |
||
109 | return $value['required']; |
||
110 | })); |
||
111 | $csvColumns = $csv->getColumnHeaders(); |
||
112 | if (count(array_diff($requiredColumns, $csvColumns)) > 0) { |
||
113 | $errors[] = trans('csvimport.text.error.format_invalid'); |
||
114 | } |
||
115 | |||
116 | // 行数の確認 |
||
117 | $size = count($csv); |
||
118 | if ($size < 1) { |
||
119 | $errors[] = trans('csvimport.text.error.format_invalid'); |
||
120 | } |
||
121 | |||
122 | $columnNames = array_combine(array_keys($columnConfig), array_column($columnConfig, 'name')); |
||
123 | |||
124 | foreach ($csv as $line => $row) { |
||
125 | // 出荷IDがなければエラー |
||
126 | if (!isset($row[$columnNames['id']])) { |
||
127 | $errors[] = trans('csvimportcontroller.require', ['%line%' => $line, '%name%' => $columnNames['id']]); |
||
128 | continue; |
||
129 | } |
||
130 | |||
131 | /* @var Shipping $Shipping */ |
||
132 | $Shipping = is_numeric($row[$columnNames['id']]) ? $this->shippingRepository->find($row[$columnNames['id']]) : null; |
||
133 | |||
134 | // 存在しない出荷IDはエラー |
||
135 | if (is_null($Shipping)) { |
||
136 | $errors[] = trans('csvimportcontroller.notfound', ['%line%' => $line, '%name%' => $columnNames['id']]); |
||
137 | continue; |
||
138 | } |
||
139 | |||
140 | if (isset($row[$columnNames['tracking_number']])) { |
||
141 | $Shipping->setTrackingNumber($row[$columnNames['tracking_number']]); |
||
142 | } |
||
143 | |||
144 | if (isset($row[$columnNames['shipping_date']])) { |
||
145 | // 日付フォーマットが異なる場合はエラー |
||
146 | $shippingDate = \DateTime::createFromFormat('Y-m-d', $row[$columnNames['shipping_date']]); |
||
147 | if ($shippingDate === false) { |
||
148 | $errors[] = trans('csvimportcontroller.invalid_date_format', ['%line%' => $line, '%name%' => $columnNames['id']]); |
||
149 | continue; |
||
150 | } |
||
151 | |||
152 | $shippingDate->setTime(0, 0, 0); |
||
153 | $Shipping->setShippingDate($shippingDate); |
||
154 | } |
||
155 | |||
156 | $Order = $Shipping->getOrder(); |
||
157 | $RelateShippings = $Order->getShippings(); |
||
158 | $allShipped = true; |
||
159 | foreach ($RelateShippings as $RelateShipping) { |
||
160 | if (!$RelateShipping->getShippingDate()) { |
||
161 | $allShipped = false; |
||
162 | break; |
||
163 | } |
||
164 | } |
||
165 | $OrderStatus = $this->entityManager->find(OrderStatus::class, OrderStatus::DELIVERED); |
||
166 | if ($allShipped) { |
||
167 | if ($this->orderStateMachine->can($Order, $OrderStatus)) { |
||
168 | $this->orderStateMachine->apply($Order, $OrderStatus); |
||
169 | } else { |
||
170 | $from = $Order->getOrderStatus()->getName(); |
||
171 | $to = $OrderStatus->getName(); |
||
172 | $errors[] = sprintf('%s: %s から %s へステータス変更できませんでした', $Shipping->getId(), $from, $to); |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | |||
211 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.