SchemaDumperExtension   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 131
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A __construct() 0 4 1
A getFunctions() 0 6 1
B getColumnOptions() 0 23 6
A getColumnOption() 0 6 1
A getPlatform() 0 8 2
A getDefaultOptions() 0 13 4
1
<?php
2
3
namespace RDV\Bundle\MigrationBundle\Twig;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use Doctrine\DBAL\Schema\Column;
8
use Doctrine\DBAL\Types\Type;
9
10
class SchemaDumperExtension extends \Twig_Extension
11
{
12
    /**
13
     * @var ManagerRegistry
14
     */
15
    protected $managerRegistry;
16
17
    /**
18
     * @var AbstractPlatform
19
     */
20
    protected $platform;
21
22
    /**
23
     * @var Column
24
     */
25
    protected $defaultColumn;
26
27
    /**
28
     * @var array
29
     */
30
    protected $defaultColumnOptions = [];
31
32
    /**
33
     * @var array
34
     */
35
    protected $optionNames = [
36
        'default',
37
        'notnull',
38
        'length',
39
        'precision',
40
        'scale',
41
        'fixed',
42
        'unsigned',
43
        'autoincrement'
44
    ];
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getName()
50
    {
51
        return 'schema_dumper_extension';
52
    }
53
54
    /**
55
     * @param ManagerRegistry $managerRegistry
56
     */
57
    public function __construct(ManagerRegistry $managerRegistry)
58
    {
59
        $this->managerRegistry = $managerRegistry;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getFunctions()
66
    {
67
        return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('rdv_migrat..., 'getColumnOptions')); (array<string,Twig_Function_Method>) is incompatible with the return type declared by the interface Twig_ExtensionInterface::getFunctions of type Twig_SimpleFunction[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
68
            'rdv_migration_get_schema_column_options' => new \Twig_Function_Method($this, 'getColumnOptions'),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Function_Method has been deprecated with message: since 1.12 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
69
        );
70
    }
71
72
    /**
73
     * @param Column $column
74
     * @return array
75
     */
76
    public function getColumnOptions(Column $column)
77
    {
78
        $defaultOptions = $this->getDefaultOptions();
79
        $platform = $this->getPlatform();
80
        $options = [];
81
82
        foreach ($this->optionNames as $optionName) {
83
            $value = $this->getColumnOption($column, $optionName);
84
            if ($value !== $defaultOptions[$optionName]) {
85
                $options[$optionName] = $value;
86
            }
87
        }
88
89
        $comment = $column->getComment();
90
        if ($platform && $platform->isCommentedDoctrineType($column->getType())) {
91
            $comment .= $platform->getDoctrineTypeComment($column->getType());
92
        }
93
        if (!empty($comment)) {
94
            $options['comment'] = $comment;
95
        }
96
97
        return $options;
98
    }
99
100
    /**
101
     * @param Column $column
102
     * @param string $optionName
103
     * @return mixed
104
     */
105
    protected function getColumnOption(Column $column, $optionName)
106
    {
107
        $method = "get" . $optionName;
108
109
        return $column->$method();
110
    }
111
112
    /**
113
     * @return AbstractPlatform
114
     */
115
    protected function getPlatform()
116
    {
117
        if (!$this->platform) {
118
            $this->platform = $this->managerRegistry->getConnection()->getDatabasePlatform();
119
        }
120
121
        return $this->platform;
122
    }
123
124
    /**
125
     * @return array
126
     */
127
    protected function getDefaultOptions()
128
    {
129
        if (!$this->defaultColumn) {
130
            $this->defaultColumn = new Column('_template_', Type::getType(Type::STRING));
131
        }
132
        if (!$this->defaultColumnOptions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->defaultColumnOptions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
133
            foreach ($this->optionNames as $optionName) {
134
                $this->defaultColumnOptions[$optionName] = $this->getColumnOption($this->defaultColumn, $optionName);
135
            }
136
        }
137
138
        return $this->defaultColumnOptions;
139
    }
140
}
141