| Conditions | 7 |
| Paths | 24 |
| Total Lines | 66 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 18 | public function up(Schema $schema) |
||
| 19 | { |
||
| 20 | $app = Application::getInstance(); |
||
| 21 | $repository = $app['orm.em']->getRepository('Eccube\Entity\Master\ProductListOrderBy'); |
||
| 22 | |||
| 23 | $isDefault = true; |
||
| 24 | $default = array( |
||
| 25 | array( |
||
| 26 | 'id' => 1, |
||
| 27 | 'name' => '価格順', |
||
| 28 | 'rank' => 0, |
||
| 29 | ), |
||
| 30 | array( |
||
| 31 | 'id' => 2, |
||
| 32 | 'name' => '新着順', |
||
| 33 | 'rank' => 1, |
||
| 34 | ), |
||
| 35 | ); |
||
| 36 | |||
| 37 | // mtb_product_list_orderbyが初期状態から変更がないかどうかの判定 |
||
| 38 | $entities = $repository->findBy(array(), array('id' => 'ASC')); |
||
| 39 | foreach ($entities as $entity) { |
||
| 40 | if (!in_array($entity->toArray(), $default, true)) { |
||
| 41 | $isDefault = false; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | // 価格が高い順ソートの追加 |
||
| 46 | $ProductListOrderBy = $repository->find(100); |
||
| 47 | if (is_null($ProductListOrderBy)) { |
||
| 48 | $rank = $repository->createQueryBuilder('pl') |
||
| 49 | ->select('MAX(pl.rank)') |
||
| 50 | ->getQuery() |
||
| 51 | ->getSingleScalarResult(); |
||
| 52 | $ProductListOrderBy = new ProductListOrderBy(); |
||
| 53 | $ProductListOrderBy->setId(100); |
||
| 54 | $ProductListOrderBy->setName('価格が高い順'); |
||
| 55 | $ProductListOrderBy->setRank($rank + 1); |
||
| 56 | $app['orm.em']->persist($ProductListOrderBy); |
||
| 57 | $app['orm.em']->flush($ProductListOrderBy); |
||
| 58 | } |
||
| 59 | |||
| 60 | // "価格順"の名称を"価格が低い順"へ変更 |
||
| 61 | $ProductListOrderBy = $repository->find(1); |
||
| 62 | if (!is_null($ProductListOrderBy) && $ProductListOrderBy->getName() === '価格順') { |
||
| 63 | $ProductListOrderBy->setName('価格が低い順'); |
||
| 64 | $app['orm.em']->persist($ProductListOrderBy); |
||
| 65 | $app['orm.em']->flush($ProductListOrderBy); |
||
| 66 | } |
||
| 67 | |||
| 68 | // mtb_product_list_orderbyが初期状態から変更がなければ、価格が低い順->価格が高い順->新着順の順にrankを振り直す |
||
| 69 | if ($isDefault) { |
||
| 70 | $entity = $repository->find(1); |
||
| 71 | $entity->setRank(0); |
||
| 72 | $app['orm.em']->flush($entity); |
||
| 73 | |||
| 74 | $entity = $repository->find(2); |
||
| 75 | $entity->setRank(2); |
||
| 76 | $app['orm.em']->flush($entity); |
||
| 77 | |||
| 78 | $entity = $repository->find(100); |
||
| 79 | dump($entity); |
||
| 80 | $entity->setRank(1); |
||
| 81 | $app['orm.em']->flush($entity); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 94 |