Failed Conditions
Push — master ( ac0e13...24dbc4 )
by Sergei
22s queued 15s
created

Platforms/Keywords/ReservedKeywordsValidator.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Platforms\Keywords;
6
7
use Doctrine\DBAL\Schema\Column;
8
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
9
use Doctrine\DBAL\Schema\Index;
10
use Doctrine\DBAL\Schema\Schema;
11
use Doctrine\DBAL\Schema\Sequence;
12
use Doctrine\DBAL\Schema\Table;
13
use Doctrine\DBAL\Schema\Visitor\Visitor;
14
use function implode;
15
use function str_replace;
16
17
class ReservedKeywordsValidator implements Visitor
18
{
19
    /** @var KeywordList[] */
20
    private $keywordLists = [];
21
22
    /** @var string[] */
23
    private $violations = [];
24
25
    /**
26
     * @param KeywordList[] $keywordLists
27
     */
28
    public function __construct(array $keywordLists)
29
    {
30
        $this->keywordLists = $keywordLists;
31
    }
32
33
    /**
34
     * @return string[]
35
     */
36
    public function getViolations() : array
37
    {
38
        return $this->violations;
39
    }
40
41
    /**
42
     * @return string[]
43
     */
44
    private function isReservedWord(string $word) : array
45
    {
46
        if ($word[0] === '`') {
47
            $word = str_replace('`', '', $word);
48
        }
49
50
        $keywordLists = [];
51
        foreach ($this->keywordLists as $keywordList) {
52
            if (! $keywordList->isKeyword($word)) {
53
                continue;
54
            }
55
56
            $keywordLists[] = $keywordList->getName();
57
        }
58
59
        return $keywordLists;
60
    }
61
62
    /**
63
     * @param string[] $violatedPlatforms
64
     */
65
    private function addViolation(string $asset, array $violatedPlatforms) : void
66
    {
67
        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...
68
            return;
69
        }
70
71
        $this->violations[] = $asset . ' keyword violations: ' . implode(', ', $violatedPlatforms);
72
    }
73
74
    public function acceptColumn(Table $table, Column $column) : void
75
    {
76
        $this->addViolation(
77
            'Table ' . $table->getName() . ' column ' . $column->getName(),
78
            $this->isReservedWord($column->getName())
79
        );
80
    }
81
82
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) : void
83
    {
84
    }
85
86
    public function acceptIndex(Table $table, Index $index) : void
87
    {
88
    }
89
90
    public function acceptSchema(Schema $schema) : void
91
    {
92
    }
93
94
    public function acceptSequence(Sequence $sequence) : void
95
    {
96
    }
97
98
    public function acceptTable(Table $table) : void
99
    {
100
        $this->addViolation(
101
            'Table ' . $table->getName(),
102
            $this->isReservedWord($table->getName())
103
        );
104
    }
105
}
106