Version20140407162508_RenameItemGenres   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 133
Duplicated Lines 16.54 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 22
loc 133
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setContainer() 0 4 1
B up() 11 31 3
B down() 11 35 3

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\DoctrineMigrations;
11
12
use Doctrine\DBAL\Migrations\AbstractMigration;
13
use Doctrine\ORM\EntityManagerInterface;
14
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Doctrine\DBAL\Schema\Schema;
17
use AnimeDb\Bundle\CatalogBundle\Entity\Genre;
18
19
/**
20
 * Auto-generated Migration: Please modify to your needs!
21
 */
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)
77
    {
78
        $this->em = $container->get('doctrine.orm.entity_manager');
0 ignored issues
show
Bug introduced by
It seems like $container is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
79
    }
80
81
    /**
82
     * @param Schema $schema
83
     */
84
    public function up(Schema $schema)
85
    {
86
        $rep = $this->em->getRepository('AnimeDbCatalogBundle:Genre');
87
88
        /* @var $genre Genre */
89 View Code Duplication
        foreach ($this->rename as $from => $to) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
            $genre = $rep->findOneBy(['name' => $from]);
91
            if (is_array($to)) {
92
                $genre->setName($to[1])->setTranslatableLocale('ru');
93
                $this->em->persist($genre);
94
                $this->em->flush($genre);
0 ignored issues
show
Unused Code introduced by
The call to EntityManagerInterface::flush() has too many arguments starting with $genre.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
95
                $to = $to[0];
96
            }
97
            $genre->setName($to)->setTranslatableLocale('en');
98
            $this->em->persist($genre);
99
        }
100
        // remove
101
        $genre = $rep->findOneBy(['name' => 'Mystery play']);
102
        $this->em->remove($genre);
103
104
        // rename russian
105
        $genre = $rep->findOneBy(['name' => 'History']);
106
        $genre->setName('Исторический')->setTranslatableLocale('ru');
107
        $this->em->persist($genre);
108
109
        $genre = $rep->findOneBy(['name' => 'War']);
110
        $genre->setName('Военное')->setTranslatableLocale('ru');
111
        $this->em->persist($genre);
112
113
        $this->em->flush();
114
    }
115
116
    /**
117
     * @param Schema $schema
118
     */
119
    public function down(Schema $schema)
120
    {
121
        $rep = $this->em->getRepository('AnimeDbCatalogBundle:Genre');
122
123
        /* @var $genre Genre */
124 View Code Duplication
        foreach ($this->restore as $from => $to) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
            $genre = $rep->findOneBy(['name' => $from]);
126
            if (is_array($to)) {
127
                $genre->setName($to[1])->setTranslatableLocale('ru');
128
                $this->em->persist($genre);
129
                $this->em->flush($genre);
0 ignored issues
show
Unused Code introduced by
The call to EntityManagerInterface::flush() has too many arguments starting with $genre.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
130
                $to = $to[0];
131
            }
132
            $genre->setName($to)->setTranslatableLocale('en');
133
            $this->em->persist($genre);
134
        }
135
        // new genre
136
        $genre = new Genre();
137
        $genre->setName('Mystery play')->setTranslatableLocale('en');
138
        $this->em->persist($genre);
139
        $this->em->flush();
140
        $genre->setName('Мистерия')->setTranslatableLocale('ru');
141
        $this->em->persist($genre);
142
143
        // rename russian
144
        $genre = $rep->findOneBy(['name' => 'History']);
145
        $genre->setName('История')->setTranslatableLocale('ru');
146
        $this->em->persist($genre);
147
148
        $genre = $rep->findOneBy(['name' => 'War']);
149
        $genre->setName('Война')->setTranslatableLocale('ru');
150
        $this->em->persist($genre);
151
152
        $this->em->flush();
153
    }
154
}
155