Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 22 | class Version20140407162508_RenameItemGenres extends AbstractMigration implements ContainerAwareInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var EntityManagerInterface |
||
| 26 | */ |
||
| 27 | protected $em; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Rename genres. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $rename = [ |
||
| 35 | 'Mysticism' => 'Mystery', |
||
| 36 | 'Ettie' => 'Ecchi', |
||
| 37 | 'For children' => ['Kids', 'Детское'], |
||
| 38 | 'Meho' => 'Mecha', |
||
| 39 | 'Musical' => ['Music', 'Музыка'], |
||
| 40 | 'Samurai action' => ['Samurai', 'Самураи'], |
||
| 41 | 'Senen' => 'Shounen', |
||
| 42 | 'Senen-ai' => 'Shounen-ai', |
||
| 43 | 'Psychology' => ['Psychological', 'Психологическое'], |
||
| 44 | 'Fantastic' => 'Sci-fi', |
||
| 45 | 'Everyday' => 'Slice of life', |
||
| 46 | 'Vampires' => 'Vampire', |
||
| 47 | 'Urey' => 'Yuri', |
||
| 48 | ]; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Restore genres. |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $restore = [ |
||
| 56 | 'Mystery' => 'Mysticism', |
||
| 57 | 'Ecchi' => 'Ettie', |
||
| 58 | 'Kids' => ['For children', 'Для детей'], |
||
| 59 | 'Mecha' => 'Meho', |
||
| 60 | 'Music' => ['Musical', 'Музыкальный'], |
||
| 61 | 'Samurai' => ['Samurai action', 'Самурайский боевик'], |
||
| 62 | 'Shounen' => 'Senen', |
||
| 63 | 'Shounen-ai' => 'Senen-ai', |
||
| 64 | 'Psychological' => ['Psychology', 'Психология'], |
||
| 65 | 'Sci-fi' => 'Fantastic', |
||
| 66 | 'Slice of life' => 'Everyday', |
||
| 67 | 'Vampire' => 'Vampires', |
||
| 68 | 'Yuri' => 'Urey', |
||
| 69 | ]; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Set container. |
||
| 73 | * |
||
| 74 | * @param ContainerInterface $container |
||
| 75 | */ |
||
| 76 | public function setContainer(ContainerInterface $container = null) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param Schema $schema |
||
| 83 | */ |
||
| 84 | public function up(Schema $schema) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param Schema $schema |
||
| 118 | */ |
||
| 119 | public function down(Schema $schema) |
||
| 154 | } |
||
| 155 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: