MigrationExecutorWithNameGenerator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 34.94 %

Coupling/Cohesion

Components 1
Dependencies 5
Metric Value
wmc 11
lcom 1
cbo 5
dl 29
loc 83
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setNameGenerator() 0 7 2
A setExtensionManager() 0 7 2
A createSchemaObject() 0 13 3
A checkTableName() 14 14 2
A checkColumnName() 15 15 2

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
namespace RDV\Bundle\MigrationBundle\Migration;
4
5
use RDV\Bundle\MigrationBundle\Exception\InvalidNameException;
6
use RDV\Bundle\MigrationBundle\Migration\Schema\SchemaWithNameGenerator;
7
use RDV\Bundle\MigrationBundle\Tools\DbIdentifierNameGenerator;
8
9
class MigrationExecutorWithNameGenerator extends MigrationExecutor
10
{
11
    /**
12
     * @var DbIdentifierNameGenerator
13
     */
14
    protected $nameGenerator;
15
16
    /**
17
     * @param DbIdentifierNameGenerator $nameGenerator
18
     */
19
    public function setNameGenerator(DbIdentifierNameGenerator $nameGenerator)
20
    {
21
        $this->nameGenerator = $nameGenerator;
22
        if ($this->extensionManager) {
23
            $this->extensionManager->setNameGenerator($this->nameGenerator);
24
        }
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function setExtensionManager(MigrationExtensionManager $extensionManager)
31
    {
32
        parent::setExtensionManager($extensionManager);
33
        if ($this->nameGenerator) {
34
            $this->extensionManager->setNameGenerator($this->nameGenerator);
35
        }
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function createSchemaObject(array $tables = [], array $sequences = [], $schemaConfig = null)
42
    {
43
        if ($schemaConfig && $this->nameGenerator) {
44
            $schemaConfig->setMaxIdentifierLength($this->nameGenerator->getMaxIdentifierSize());
45
        }
46
47
        return new SchemaWithNameGenerator(
48
            $this->nameGenerator,
49
            $tables,
50
            $sequences,
51
            $schemaConfig
52
        );
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 View Code Duplication
    protected function checkTableName($tableName, Migration $migration)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
59
    {
60
        parent::checkTableName($tableName, $migration);
61
        if (strlen($tableName) > $this->nameGenerator->getMaxIdentifierSize()) {
62
            throw new InvalidNameException(
63
                sprintf(
64
                    'Max table name length is %s. Please correct "%s" table in "%s" migration',
65
                    $this->nameGenerator->getMaxIdentifierSize(),
66
                    $tableName,
67
                    get_class($migration)
68
                )
69
            );
70
        }
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 View Code Duplication
    protected function checkColumnName($tableName, $columnName, Migration $migration)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
77
    {
78
        parent::checkColumnName($tableName, $columnName, $migration);
79
        if (strlen($columnName) > $this->nameGenerator->getMaxIdentifierSize()) {
80
            throw new InvalidNameException(
81
                sprintf(
82
                    'Max column name length is %s. Please correct "%s:%s" column in "%s" migration',
83
                    $this->nameGenerator->getMaxIdentifierSize(),
84
                    $tableName,
85
                    $columnName,
86
                    get_class($migration)
87
                )
88
            );
89
        }
90
    }
91
}
92