Completed
Push — master ( 09e072...c7757e )
by Luís
15s
created

ReservedKeywordsValidator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 111
ccs 25
cts 35
cp 0.7143
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A addViolation() 0 7 2
A __construct() 0 3 1
A acceptColumn() 0 5 1
A acceptIndex() 0 2 1
A acceptSchema() 0 2 1
A acceptTable() 0 5 1
A getViolations() 0 3 1
A acceptSequence() 0 2 1
A isReservedWord() 0 14 4
A acceptForeignKey() 0 2 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Platforms\Keywords;
21
22
use Doctrine\DBAL\Schema\Visitor\Visitor;
23
use Doctrine\DBAL\Schema\Table;
24
use Doctrine\DBAL\Schema\Column;
25
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
26
use Doctrine\DBAL\Schema\Schema;
27
use Doctrine\DBAL\Schema\Sequence;
28
use Doctrine\DBAL\Schema\Index;
29
30
class ReservedKeywordsValidator implements Visitor
31
{
32
    /**
33
     * @var KeywordList[]
34
     */
35
    private $keywordLists = [];
36
37
    /**
38
     * @var array
39
     */
40
    private $violations = [];
41
42
    /**
43
     * @param \Doctrine\DBAL\Platforms\Keywords\KeywordList[] $keywordLists
44
     */
45 2
    public function __construct(array $keywordLists)
46
    {
47 2
        $this->keywordLists = $keywordLists;
48 2
    }
49
50
    /**
51
     * @return array
52
     */
53 2
    public function getViolations()
54
    {
55 2
        return $this->violations;
56
    }
57
58
    /**
59
     * @param string $word
60
     *
61
     * @return array
62
     */
63 2
    private function isReservedWord($word)
64
    {
65 2
        if ($word[0] == "`") {
66
            $word = str_replace('`', '', $word);
67
        }
68
69 2
        $keywordLists = [];
70 2
        foreach ($this->keywordLists as $keywordList) {
71 2
            if ($keywordList->isKeyword($word)) {
72 2
                $keywordLists[] = $keywordList->getName();
73
            }
74
        }
75
76 2
        return $keywordLists;
77
    }
78
79
    /**
80
     * @param string $asset
81
     * @param array  $violatedPlatforms
82
     *
83
     * @return void
84
     */
85 2
    private function addViolation($asset, $violatedPlatforms)
86
    {
87 2
        if ( ! $violatedPlatforms) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $violatedPlatforms 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...
88
            return;
89
        }
90
91 2
        $this->violations[] = $asset . ' keyword violations: ' . implode(', ', $violatedPlatforms);
92 2
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 1
    public function acceptColumn(Table $table, Column $column)
98
    {
99 1
        $this->addViolation(
100 1
            'Table ' . $table->getName() . ' column ' . $column->getName(),
101 1
            $this->isReservedWord($column->getName())
102
        );
103 1
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
109
    {
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function acceptIndex(Table $table, Index $index)
116
    {
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function acceptSchema(Schema $schema)
123
    {
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function acceptSequence(Sequence $sequence)
130
    {
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 1
    public function acceptTable(Table $table)
137
    {
138 1
        $this->addViolation(
139 1
            'Table ' . $table->getName(),
140 1
            $this->isReservedWord($table->getName())
141
        );
142 1
    }
143
}
144