| Conditions | 4 |
| Paths | 3 |
| Total Lines | 59 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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 |
||
| 20 | public function up(Schema $schema) |
||
| 21 | { |
||
| 22 | $app = Application::getInstance(); |
||
| 23 | $repository = $app['orm.em']->getRepository('Eccube\Entity\Master\ProductListOrderBy'); |
||
| 24 | |||
| 25 | // mtb_product_list_orderbyが初期状態から変更がある場合は、マイグレーションを適用しない |
||
| 26 | $default = array( |
||
| 27 | array( |
||
| 28 | 'id' => 1, |
||
| 29 | 'name' => '価格順', |
||
| 30 | 'rank' => 0, |
||
| 31 | ), |
||
| 32 | array( |
||
| 33 | 'id' => 2, |
||
| 34 | 'name' => '新着順', |
||
| 35 | 'rank' => 1, |
||
| 36 | ), |
||
| 37 | ); |
||
| 38 | $entities = $repository->createQueryBuilder('pl') |
||
| 39 | ->orderBy('pl.id', 'ASC') |
||
| 40 | ->getQuery() |
||
| 41 | ->getArrayResult(); |
||
| 42 | |||
| 43 | if ($entities !== $default) { |
||
| 44 | return; |
||
| 45 | } |
||
| 46 | |||
| 47 | // 価格が高い順を追加 |
||
| 48 | $ProductListOrderBy = new ProductListOrderBy(); |
||
| 49 | $ProductListOrderBy->setId(3); |
||
| 50 | $ProductListOrderBy->setName('価格が高い順'); |
||
| 51 | $ProductListOrderBy->setRank(2); |
||
| 52 | $app['orm.em']->persist($ProductListOrderBy); |
||
| 53 | $app['orm.em']->flush($ProductListOrderBy); |
||
| 54 | |||
| 55 | // "価格順"の名称を"価格が低い順"へ変更 |
||
| 56 | $ProductListOrderBy = $repository->find(1); |
||
| 57 | if (!is_null($ProductListOrderBy) && $ProductListOrderBy->getName() === '価格順') { |
||
| 58 | $ProductListOrderBy->setName('価格が低い順'); |
||
| 59 | $app['orm.em']->persist($ProductListOrderBy); |
||
| 60 | $app['orm.em']->flush($ProductListOrderBy); |
||
| 61 | } |
||
| 62 | |||
| 63 | // 価格が低い順->価格が高い順->新着順の順にrankを振り直す |
||
| 64 | // 価格が低い順 |
||
| 65 | $entity = $repository->find(1); |
||
| 66 | $entity->setRank(0); |
||
| 67 | $app['orm.em']->flush($entity); |
||
| 68 | |||
| 69 | // 価格が高い順 |
||
| 70 | $entity = $repository->find(3); |
||
| 71 | $entity->setRank(1); |
||
| 72 | $app['orm.em']->flush($entity); |
||
| 73 | |||
| 74 | // 新着順 |
||
| 75 | $entity = $repository->find(2); |
||
| 76 | $entity->setRank(2); |
||
| 77 | $app['orm.em']->flush($entity); |
||
| 78 | } |
||
| 79 | |||
| 89 |