ForeignKeys::getForeignKeyNames()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Reedware\LaravelBlueprints\Concerns;
4
5
use Illuminate\Database\Connection;
6
7
trait ForeignKeys
8
{
9
    /**
10
     * Drops the specified Foreign Key if it exists.
11
     *
12
     * @param  string                           $index
13
     * @param  \Illuminate\Database\Connection  $connection
14
     *
15
     * @return \Illuminate\Support\Fluent|null
16
     */
17
    public function dropForeignIfExists($index, Connection $connection = null)
18
    {
19
        // Drop the Foreign Key if it exists
20
        if($this->foreignExists($index, $connection)) {
21
            return $this->dropForeign($index);
0 ignored issues
show
Bug introduced by
The method dropForeign() does not exist on Reedware\LaravelBlueprints\Concerns\ForeignKeys. Did you maybe mean dropForeignIfExists()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
            return $this->/** @scrutinizer ignore-call */ dropForeign($index);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
22
        }
23
24
        // Return NULL
25
        return null;
26
    }
27
28
    /**
29
     * Returns whether or not the specified Foreign Key exists.
30
     *
31
     * @param  string                           $index
32
     * @param  \Illuminate\Database\Connection  $connection
33
     *
34
     * @return boolean
35
     */
36
    public function foreignExists($index, Connection $connection = null)
37
    {
38
        // Determine the Foreign Keys
39
        $foreigns = $this->getForeignKeyNames($connection);
40
41
        // Return the the Index is in the list of Foreigns
42
        return in_array($index, $foreigns);
43
    }
44
45
    /**
46
     * Returns the Foreign Key names.
47
     *
48
     * @param  \Illuminate\Database\Connection  $connection
49
     *
50
     * @return array
51
     */
52
    public function getForeignKeyNames(Connection $connection = null)
53
    {
54
        // Determine the Foreign Keys
55
        $foreigns = $this->getForeignKeys($connection);
56
57
        // Convert them to their Names
58
        return array_map(function($foreign) {
59
            return $foreign->getName();
60
        }, $foreigns);
61
    }
62
63
    /**
64
     * Returns the Foreign Keys for this Table.
65
     *
66
     * @param  \Illuminate\Database\Connection|null  $connection
67
     *
68
     * @return array
69
     */
70
    public function getForeignKeys(Connection $connection = null)
71
    {
72
        // Determine the Schema Manager
73
        $manager = $this->getSchemaManager($connection);
0 ignored issues
show
Bug introduced by
It seems like getSchemaManager() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        /** @scrutinizer ignore-call */ 
74
        $manager = $this->getSchemaManager($connection);
Loading history...
74
75
        // Return the Foreign Keys
76
        return $manager->listTableForeignKeys($this->table);
77
    }
78
}