| Conditions | 8 |
| Paths | 36 |
| Total Lines | 82 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 | $isDefault = true; |
||
| 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 | |||
| 39 | // mtb_product_list_orderbyが初期状態から変更がないかどうかの判定 |
||
| 40 | $entities = $repository->findBy(array(), array('id' => 'ASC')); |
||
| 41 | foreach ($entities as $entity) { |
||
| 42 | if (!in_array($entity->toArray(), $default, true)) { |
||
| 43 | $isDefault = false; |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | // 価格が高い順ソートの追加 |
||
| 48 | $id = $repository->createQueryBuilder('pl') |
||
| 49 | ->select('MAX(pl.id) + 1') |
||
| 50 | ->getQuery() |
||
| 51 | ->getSingleScalarResult(); |
||
| 52 | |||
| 53 | $rank = $repository->createQueryBuilder('pl') |
||
| 54 | ->select('MAX(pl.rank) + 1') |
||
| 55 | ->getQuery() |
||
| 56 | ->getSingleScalarResult(); |
||
| 57 | |||
| 58 | $ProductListOrderBy = new ProductListOrderBy(); |
||
| 59 | $ProductListOrderBy->setId($id); |
||
| 60 | $ProductListOrderBy->setName('価格が高い順'); |
||
| 61 | $ProductListOrderBy->setRank($rank); |
||
| 62 | $app['orm.em']->persist($ProductListOrderBy); |
||
| 63 | $app['orm.em']->flush($ProductListOrderBy); |
||
| 64 | |||
| 65 | // constant.ymlへ価格が高い順のIDを記録. |
||
| 66 | if ($id !== $app['config']['product_order_price_higher']) { |
||
| 67 | $file = $app['config']['root_dir'].'/app/config/eccube/constant.yml'; |
||
| 68 | $fs = new Filesystem(); |
||
| 69 | |||
| 70 | $constant = $fs->exists($file) |
||
| 71 | ? Yaml::parse(file_get_contents($file)) |
||
| 72 | : array(); |
||
| 73 | |||
| 74 | $constant['product_order_price_higher'] = $id; |
||
| 75 | $yaml = Yaml::dump($constant); |
||
| 76 | $fs->dumpFile($file, $yaml); |
||
| 77 | } |
||
| 78 | |||
| 79 | // "価格順"の名称を"価格が低い順"へ変更 |
||
| 80 | $ProductListOrderBy = $repository->find(1); |
||
| 81 | if (!is_null($ProductListOrderBy) && $ProductListOrderBy->getName() === '価格順') { |
||
| 82 | $ProductListOrderBy->setName('価格が低い順'); |
||
| 83 | $app['orm.em']->persist($ProductListOrderBy); |
||
| 84 | $app['orm.em']->flush($ProductListOrderBy); |
||
| 85 | } |
||
| 86 | |||
| 87 | // mtb_product_list_orderbyが初期状態から変更がなければ、価格が低い順->価格が高い順->新着順の順にrankを振り直す |
||
| 88 | if ($isDefault) { |
||
| 89 | $entity = $repository->find(1); |
||
| 90 | $entity->setRank(0); |
||
| 91 | $app['orm.em']->flush($entity); |
||
| 92 | |||
| 93 | $entity = $repository->find(2); |
||
| 94 | $entity->setRank(2); |
||
| 95 | $app['orm.em']->flush($entity); |
||
| 96 | |||
| 97 | $entity = $repository->find($id); |
||
| 98 | $entity->setRank(1); |
||
| 99 | $app['orm.em']->flush($entity); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 112 |