Failed Conditions
Pull Request — master (#3233)
by Sergey
10:50
created

ForeignKeyConstraint::intersectsIndexColumns()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
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\Schema;
21
22
use Doctrine\DBAL\Platforms\AbstractPlatform;
23
use function array_combine;
24
use function array_keys;
25
use function array_map;
26
use function end;
27
use function explode;
28
use function in_array;
29
use function strtolower;
30
use function strtoupper;
31
32
/**
33
 * An abstraction class for a foreign key constraint.
34
 *
35
 * @author Benjamin Eberlei <[email protected]>
36
 * @author Steve Müller <[email protected]>
37
 * @link   www.doctrine-project.org
38
 * @since  2.0
39
 */
40
class ForeignKeyConstraint extends AbstractAsset implements Constraint
41
{
42
    /**
43
     * Instance of the referencing table the foreign key constraint is associated with.
44
     *
45
     * @var \Doctrine\DBAL\Schema\Table
46
     */
47
    protected $_localTable;
48
49
    /**
50
     * Asset identifier instances of the referencing table column names the foreign key constraint is associated with.
51
     * array($columnName => Identifier)
52
     *
53
     * @var Identifier[]
54
     */
55
    protected $_localColumnNames;
56
57
    /**
58
     * Table or asset identifier instance of the referenced table name the foreign key constraint is associated with.
59
     *
60
     * @var Table|Identifier
61
     */
62
    protected $_foreignTableName;
63
64
    /**
65
     * Asset identifier instances of the referenced table column names the foreign key constraint is associated with.
66
     * array($columnName => Identifier)
67
     *
68
     * @var Identifier[]
69
     */
70
    protected $_foreignColumnNames;
71
72
    /**
73
     * @var array Options associated with the foreign key constraint.
74
     */
75
    protected $_options;
76
77
    /**
78
     * Initializes the foreign key constraint.
79
     *
80
     * @param array        $localColumnNames   Names of the referencing table columns.
81
     * @param Table|string $foreignTableName   Referenced table.
82
     * @param array        $foreignColumnNames Names of the referenced table columns.
83
     * @param string|null  $name               Name of the foreign key constraint.
84
     * @param array        $options            Options associated with the foreign key constraint.
85
     */
86 5011
    public function __construct(array $localColumnNames, $foreignTableName, array $foreignColumnNames, $name = null, array $options = [])
87
    {
88 5011
        $this->_setName($name);
89
        $identifierConstructorCallback = function ($column) {
90 4291
            return new Identifier($column);
91 5011
        };
92 5011
        $this->_localColumnNames = $localColumnNames
93 4211
            ? array_combine($localColumnNames, array_map($identifierConstructorCallback, $localColumnNames))
94 800
            : [];
95
96 5011
        if ($foreignTableName instanceof Table) {
97 1118
            $this->_foreignTableName = $foreignTableName;
98
        } else {
99 3947
            $this->_foreignTableName = new Identifier($foreignTableName);
100
        }
101
102 5011
        $this->_foreignColumnNames = $foreignColumnNames
103 4211
            ? array_combine($foreignColumnNames, array_map($identifierConstructorCallback, $foreignColumnNames))
104 800
            : [];
105 5011
        $this->_options = $options;
106 5011
    }
107
108
    /**
109
     * Returns the name of the referencing table
110
     * the foreign key constraint is associated with.
111
     *
112
     * @return string
113
     */
114 40
    public function getLocalTableName()
115
    {
116 40
        return $this->_localTable->getName();
117
    }
118
119
    /**
120
     * Sets the Table instance of the referencing table
121
     * the foreign key constraint is associated with.
122
     *
123
     * @param \Doctrine\DBAL\Schema\Table $table Instance of the referencing table.
124
     *
125
     * @return void
126
     */
127 2312
    public function setLocalTable(Table $table)
128
    {
129 2312
        $this->_localTable = $table;
130 2312
    }
131
132
    /**
133
     * @return Table
134
     */
135
    public function getLocalTable()
136
    {
137
        return $this->_localTable;
138
    }
139
140
    /**
141
     * Returns the names of the referencing table columns
142
     * the foreign key constraint is associated with.
143
     *
144
     * @return array
145
     */
146 3050
    public function getLocalColumns()
147
    {
148 3050
        return array_keys($this->_localColumnNames);
149
    }
150
151
    /**
152
     * Returns the quoted representation of the referencing table column names
153
     * the foreign key constraint is associated with.
154
     *
155
     * But only if they were defined with one or the referencing table column name
156
     * is a keyword reserved by the platform.
157
     * Otherwise the plain unquoted value as inserted is returned.
158
     *
159
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The platform to use for quotation.
160
     *
161
     * @return array
162
     */
163 2729
    public function getQuotedLocalColumns(AbstractPlatform $platform)
164
    {
165 2729
        $columns = [];
166
167 2729
        foreach ($this->_localColumnNames as $column) {
168 2649
            $columns[] = $column->getQuotedName($platform);
169
        }
170
171 2729
        return $columns;
172
    }
173
174
    /**
175
     * Returns unquoted representation of local table column names for comparison with other FK
176
     *
177
     * @return array
178
     */
179 185
    public function getUnquotedLocalColumns()
180
    {
181 185
        return array_map([$this, 'trimQuotes'], $this->getLocalColumns());
182
    }
183
184
    /**
185
     * Returns unquoted representation of foreign table column names for comparison with other FK
186
     *
187
     * @return array
188
     */
189 146
    public function getUnquotedForeignColumns()
190
    {
191 146
        return array_map([$this, 'trimQuotes'], $this->getForeignColumns());
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     *
197
     * @see getLocalColumns
198
     */
199 2312
    public function getColumns()
200
    {
201 2312
        return $this->getLocalColumns();
202
    }
203
204
    /**
205
     * Returns the quoted representation of the referencing table column names
206
     * the foreign key constraint is associated with.
207
     *
208
     * But only if they were defined with one or the referencing table column name
209
     * is a keyword reserved by the platform.
210
     * Otherwise the plain unquoted value as inserted is returned.
211
     *
212
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The platform to use for quotation.
213
     *
214
     * @see getQuotedLocalColumns
215
     *
216
     * @return array
217
     */
218 260
    public function getQuotedColumns(AbstractPlatform $platform)
219
    {
220 260
        return $this->getQuotedLocalColumns($platform);
221
    }
222
223
    /**
224
     * Returns the name of the referenced table
225
     * the foreign key constraint is associated with.
226
     *
227
     * @return string
228
     */
229 1749
    public function getForeignTableName()
230
    {
231 1749
        return $this->_foreignTableName->getName();
232
    }
233
234
    /**
235
     * Returns the non-schema qualified foreign table name.
236
     *
237
     * @return string
238
     */
239 146
    public function getUnqualifiedForeignTableName()
240
    {
241 146
        $parts = explode(".", $this->_foreignTableName->getName());
242
243 146
        return strtolower(end($parts));
244
    }
245
246
    /**
247
     * Returns the quoted representation of the referenced table name
248
     * the foreign key constraint is associated with.
249
     *
250
     * But only if it was defined with one or the referenced table name
251
     * is a keyword reserved by the platform.
252
     * Otherwise the plain unquoted value as inserted is returned.
253
     *
254
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The platform to use for quotation.
255
     *
256
     * @return string
257
     */
258 2729
    public function getQuotedForeignTableName(AbstractPlatform $platform)
259
    {
260 2729
        return $this->_foreignTableName->getQuotedName($platform);
261
    }
262
263
    /**
264
     * Returns the names of the referenced table columns
265
     * the foreign key constraint is associated with.
266
     *
267
     * @return array
268
     */
269 1809
    public function getForeignColumns()
270
    {
271 1809
        return array_keys($this->_foreignColumnNames);
272
    }
273
274
    /**
275
     * Returns the quoted representation of the referenced table column names
276
     * the foreign key constraint is associated with.
277
     *
278
     * But only if they were defined with one or the referenced table column name
279
     * is a keyword reserved by the platform.
280
     * Otherwise the plain unquoted value as inserted is returned.
281
     *
282
     * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The platform to use for quotation.
283
     *
284
     * @return array
285
     */
286 2729
    public function getQuotedForeignColumns(AbstractPlatform $platform)
287
    {
288 2729
        $columns = [];
289
290 2729
        foreach ($this->_foreignColumnNames as $column) {
291 2649
            $columns[] = $column->getQuotedName($platform);
292
        }
293
294 2729
        return $columns;
295
    }
296
297
    /**
298
     * Returns whether or not a given option
299
     * is associated with the foreign key constraint.
300
     *
301
     * @param string $name Name of the option to check.
302
     *
303
     * @return bool
304
     */
305 2369
    public function hasOption($name)
306
    {
307 2369
        return isset($this->_options[$name]);
308
    }
309
310
    /**
311
     * Returns an option associated with the foreign key constraint.
312
     *
313
     * @param string $name Name of the option the foreign key constraint is associated with.
314
     *
315
     * @return mixed
316
     */
317 339
    public function getOption($name)
318
    {
319 339
        return $this->_options[$name];
320
    }
321
322
    /**
323
     * Returns the options associated with the foreign key constraint.
324
     *
325
     * @return array
326
     */
327 101
    public function getOptions()
328
    {
329 101
        return $this->_options;
330
    }
331
332
    /**
333
     * Returns the referential action for UPDATE operations
334
     * on the referenced table the foreign key constraint is associated with.
335
     *
336
     * @return string|null
337
     */
338 126
    public function onUpdate()
339
    {
340 126
        return $this->onEvent('onUpdate');
341
    }
342
343
    /**
344
     * Returns the referential action for DELETE operations
345
     * on the referenced table the foreign key constraint is associated with.
346
     *
347
     * @return string|null
348
     */
349 106
    public function onDelete()
350
    {
351 106
        return $this->onEvent('onDelete');
352
    }
353
354
    /**
355
     * Returns the referential action for a given database operation
356
     * on the referenced table the foreign key constraint is associated with.
357
     *
358
     * @param string $event Name of the database operation/event to return the referential action for.
359
     *
360
     * @return string|null
361
     */
362 126
    private function onEvent($event)
363
    {
364 126
        if (isset($this->_options[$event])) {
365 60
            $onEvent = strtoupper($this->_options[$event]);
366
367 60
            if ( ! in_array($onEvent, ['NO ACTION', 'RESTRICT'])) {
368 20
                return $onEvent;
369
            }
370
        }
371
372 126
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type null|string.
Loading history...
373
    }
374
375
    /**
376
     * Checks whether this foreign key constraint intersects the given index columns.
377
     *
378
     * Returns `true` if at least one of this foreign key's local columns
379
     * matches one of the given index's columns, `false` otherwise.
380
     *
381
     * @param Index $index The index to be checked against.
382
     *
383
     * @return bool
384
     */
385 326
    public function intersectsIndexColumns(Index $index)
386
    {
387 326
        foreach ($index->getColumns() as $indexColumn) {
388 326
            foreach ($this->_localColumnNames as $localColumn) {
389 326
                if (strtolower($indexColumn) === strtolower($localColumn->getName())) {
390 326
                    return true;
391
                }
392
            }
393
        }
394
395 80
        return false;
396
    }
397
}
398