Failed Conditions
Push — master ( edfbda...298c91 )
by Luís
16s
created

Tools/Console/Command/ReservedWordsCommand.php (1 issue)

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\Tools\Console\Command;
21
22
use Doctrine\DBAL\Platforms\Keywords\ReservedKeywordsValidator;
23
use Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Input\InputOption;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
class ReservedWordsCommand extends Command
29
{
30
    /**
31
     * @var array
32
     */
33
    private $keywordListClasses = [
34
        'mysql'         => 'Doctrine\DBAL\Platforms\Keywords\MySQLKeywords',
35
        'mysql57'       => 'Doctrine\DBAL\Platforms\Keywords\MySQL57Keywords',
36
        'sqlserver'     => 'Doctrine\DBAL\Platforms\Keywords\SQLServerKeywords',
37
        'sqlserver2005' => 'Doctrine\DBAL\Platforms\Keywords\SQLServer2005Keywords',
38
        'sqlserver2008' => 'Doctrine\DBAL\Platforms\Keywords\SQLServer2008Keywords',
39
        'sqlserver2012' => 'Doctrine\DBAL\Platforms\Keywords\SQLServer2012Keywords',
40
        'sqlite'        => 'Doctrine\DBAL\Platforms\Keywords\SQLiteKeywords',
41
        'pgsql'         => 'Doctrine\DBAL\Platforms\Keywords\PostgreSQLKeywords',
42
        'pgsql91'       => 'Doctrine\DBAL\Platforms\Keywords\PostgreSQL91Keywords',
43
        'pgsql92'       => 'Doctrine\DBAL\Platforms\Keywords\PostgreSQL92Keywords',
44
        'oracle'        => 'Doctrine\DBAL\Platforms\Keywords\OracleKeywords',
45
        'db2'           => 'Doctrine\DBAL\Platforms\Keywords\DB2Keywords',
46
        'sqlanywhere'   => 'Doctrine\DBAL\Platforms\Keywords\SQLAnywhereKeywords',
47
        'sqlanywhere11' => 'Doctrine\DBAL\Platforms\Keywords\SQLAnywhere11Keywords',
48
        'sqlanywhere12' => 'Doctrine\DBAL\Platforms\Keywords\SQLAnywhere12Keywords',
49
        'sqlanywhere16' => 'Doctrine\DBAL\Platforms\Keywords\SQLAnywhere16Keywords',
50
    ];
51
52
    /**
53
     * If you want to add or replace a keywords list use this command.
54
     *
55
     * @param string $name
56
     * @param string $class
57
     *
58
     * @return void
59
     */
60
    public function setKeywordListClass($name, $class)
61
    {
62
        $this->keywordListClasses[$name] = $class;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected function configure()
69
    {
70
        $this
71
        ->setName('dbal:reserved-words')
72
        ->setDescription('Checks if the current database contains identifiers that are reserved.')
73
        ->setDefinition([
74
            new InputOption(
75
                'list', 'l', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Keyword-List name.'
76
            )
77
        ])
78
        ->setHelp(<<<EOT
79
Checks if the current database contains tables and columns
80
with names that are identifiers in this dialect or in other SQL dialects.
81
82
By default SQLite, MySQL, PostgreSQL, Microsoft SQL Server, Oracle
83
and SQL Anywhere keywords are checked:
84
85
    <info>%command.full_name%</info>
86
87
If you want to check against specific dialects you can
88
pass them to the command:
89
90
    <info>%command.full_name% -l mysql -l pgsql</info>
91
92
The following keyword lists are currently shipped with Doctrine:
93
94
    * mysql
95
    * mysql57
96
    * pgsql
97
    * pgsql92
98
    * sqlite
99
    * oracle
100
    * sqlserver
101
    * sqlserver2005
102
    * sqlserver2008
103
    * sqlserver2012
104
    * sqlanywhere
105
    * sqlanywhere11
106
    * sqlanywhere12
107
    * sqlanywhere16
108
    * db2 (Not checked by default)
109
EOT
110
        );
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    protected function execute(InputInterface $input, OutputInterface $output)
117
    {
118
        /* @var $conn \Doctrine\DBAL\Connection */
119
        $conn = $this->getHelper('db')->getConnection();
120
121
        $keywordLists = (array) $input->getOption('list');
122
        if ( ! $keywordLists) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $keywordLists 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...
123
            $keywordLists = [
124
                'mysql',
125
                'mysql57',
126
                'pgsql',
127
                'pgsql92',
128
                'sqlite',
129
                'oracle',
130
                'sqlserver',
131
                'sqlserver2005',
132
                'sqlserver2008',
133
                'sqlserver2012',
134
                'sqlanywhere',
135
                'sqlanywhere11',
136
                'sqlanywhere12',
137
                'sqlanywhere16',
138
            ];
139
        }
140
141
        $keywords = [];
142
        foreach ($keywordLists as $keywordList) {
143
            if (!isset($this->keywordListClasses[$keywordList])) {
144
                throw new \InvalidArgumentException(
145
                    "There exists no keyword list with name '" . $keywordList . "'. ".
146
                    "Known lists: " . implode(", ", array_keys($this->keywordListClasses))
147
                );
148
            }
149
            $class = $this->keywordListClasses[$keywordList];
150
            $keywords[] = new $class;
151
        }
152
153
        $output->write('Checking keyword violations for <comment>' . implode(", ", $keywordLists) . "</comment>...", true);
154
155
        /* @var $schema \Doctrine\DBAL\Schema\Schema */
156
        $schema = $conn->getSchemaManager()->createSchema();
157
        $visitor = new ReservedKeywordsValidator($keywords);
158
        $schema->visit($visitor);
159
160
        $violations = $visitor->getViolations();
161
        if (count($violations) == 0) {
162
            $output->write("No reserved keywords violations have been found!", true);
163
        } else {
164
            $output->write('There are <error>' . count($violations) . '</error> reserved keyword violations in your database schema:', true);
165
            foreach ($violations as $violation) {
166
                $output->write('  - ' . $violation, true);
167
            }
168
169
            return 1;
170
        }
171
172
        return 0;
173
    }
174
}
175