Failed Conditions
Push — master ( 379085...b45ed5 )
by Marco
54s queued 28s
created

ReservedKeywordsValidator::getViolations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Doctrine\DBAL\Platforms\Keywords;
4
5
use Doctrine\DBAL\Schema\Column;
6
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
7
use Doctrine\DBAL\Schema\Index;
8
use Doctrine\DBAL\Schema\Schema;
9
use Doctrine\DBAL\Schema\Sequence;
10
use Doctrine\DBAL\Schema\Table;
11
use Doctrine\DBAL\Schema\Visitor\Visitor;
12
use function implode;
13
use function str_replace;
14
15
class ReservedKeywordsValidator implements Visitor
16
{
17
    /** @var KeywordList[] */
18
    private $keywordLists = [];
19
20
    /** @var string[] */
21
    private $violations = [];
22
23
    /**
24
     * @param KeywordList[] $keywordLists
25
     */
26 38
    public function __construct(array $keywordLists)
27
    {
28 38
        $this->keywordLists = $keywordLists;
29 38
    }
30
31
    /**
32
     * @return string[]
33
     */
34 38
    public function getViolations()
35
    {
36 38
        return $this->violations;
37
    }
38
39
    /**
40
     * @param string $word
41
     *
42
     * @return string[]
43
     */
44 38
    private function isReservedWord($word)
45
    {
46 38
        if ($word[0] === '`') {
47
            $word = str_replace('`', '', $word);
48
        }
49
50 38
        $keywordLists = [];
51 38
        foreach ($this->keywordLists as $keywordList) {
52 38
            if (! $keywordList->isKeyword($word)) {
53
                continue;
54
            }
55
56 38
            $keywordLists[] = $keywordList->getName();
57
        }
58
59 38
        return $keywordLists;
60
    }
61
62
    /**
63
     * @param string   $asset
64
     * @param string[] $violatedPlatforms
65
     *
66
     * @return void
67
     */
68 38
    private function addViolation($asset, $violatedPlatforms)
69
    {
70 38
        if (! $violatedPlatforms) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $violatedPlatforms of type string[] 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...
71
            return;
72
        }
73
74 38
        $this->violations[] = $asset . ' keyword violations: ' . implode(', ', $violatedPlatforms);
75 38
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 19
    public function acceptColumn(Table $table, Column $column)
81
    {
82 19
        $this->addViolation(
83 19
            'Table ' . $table->getName() . ' column ' . $column->getName(),
84 19
            $this->isReservedWord($column->getName())
85
        );
86 19
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
92
    {
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function acceptIndex(Table $table, Index $index)
99
    {
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function acceptSchema(Schema $schema)
106
    {
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function acceptSequence(Sequence $sequence)
113
    {
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 19
    public function acceptTable(Table $table)
120
    {
121 19
        $this->addViolation(
122 19
            'Table ' . $table->getName(),
123 19
            $this->isReservedWord($table->getName())
124
        );
125 19
    }
126
}
127