GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TableApi::addForeignKey()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 15
nc 16
nop 3
1
<?php
2
3
namespace Dami\Migration\Api;
4
5
use Rentgen\Database\Column\DateTimeColumn;
6
use Rentgen\Database\Column\TextColumn;
7
use Rentgen\Database\Constraint\ForeignKey;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Dami\Migration\Api\ForeignKey.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Rentgen\Database\Constraint\Unique;
9
use Rentgen\Database\Index;
10
use Rentgen\Database\Schema;
11
use Rentgen\Database\Table;
12
use Rentgen\Schema\Manipulation;
13
14
class TableApi extends Table
15
{
16
    private $alterTable = false;
0 ignored issues
show
Unused Code introduced by
The property $alterTable is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
18
    /**
19
     * Constructor.
20
     *
21
     * @param string       $table        Table name.
0 ignored issues
show
Bug introduced by
There is no parameter named $table. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
22
     * @param Schema       $schema
23
     * @param Manipulation $manipulation
24
     * @param array        $actions
25
     */
26
    public function __construct($name, Schema $schema = null, Manipulation $manipulation, &$actions)
27
    {
28
        parent::__construct($name, $schema);
29
        $this->manipulation = $manipulation;
0 ignored issues
show
Bug introduced by
The property manipulation does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
        $this->actions = &$actions;
0 ignored issues
show
Bug introduced by
The property actions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
    }
32
33
    /**
34
     * @param string $method A method name.
35
     * @param array  $params Parameters.
36
     *
37
     * @return TableApi Self.
38
     */
39
    public function __call($method, $params)
40
    {
41
        if ($method === 'addTimestamps') {
42
            $this->columns[] = new DateTimeColumn('created_at', array('not_null' => true));
43
            $this->columns[] = new DateTimeColumn('updated_at', array('not_null' => true));
44
        } elseif ($method === 'addUserstamps') {
45
            $this->columns[] = new IntegerColumn('created_by', array('not_null' => true, 'default' => 0));
46
            $this->columns[] = new IntegerColumn('updated_by', array('not_null' => true, 'default' => 0));
47
        } else {
48
            $this->columns[] = (new ColumnFactory($method, $params))->createInstance();
49
        }
50
        
51
        return $this;
52
    }
53
54
    /**
55
     * Drop a column.
56
     *
57
     * @param string $name Column name.
58
     *
59
     * @return TableApi Self.
60
     */
61
    public function dropColumn($name)
62
    {
63
        $column = new TextColumn($name);
64
        $column->setTable($this);
65
66
        $this->actions[] =  function () use ($column) {
67
             return $this->manipulation->drop($column);
68
        };
69
70
        return $this;
71
    }
72
73
    /**
74
     * Add a foreign key.
75
     *
76
     * @param string $referenceTable   Referenced table name.
77
     * @param string $referenceColumns Columns of referenced table.
78
     * @param array  $options          Optional options.
79
     *
80
     * @return TableApi Self.
81
     */
82
    public function addForeignKey($referenceTable, $referenceColumns, array $options = [])
83
    {
84
        $schema = isset($options['schema']) ? new Schema($options['schema']) : null;
85
86
        $foreignKey = new ForeignKey($this, new Table($referenceTable, $schema));
87
        $foreignKey->setReferencedColumns($referenceColumns);
0 ignored issues
show
Documentation introduced by
$referenceColumns is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
88
89
        if (isset($options['column'])) {
90
            $foreignKey->setColumns($options['column']);
91
        } else {
92
            $foreignKey->setColumns($referenceColumns);
0 ignored issues
show
Documentation introduced by
$referenceColumns is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
        }
94
        if (isset($options['update'])) {
95
            $foreignKey->setUpdateAction($options['update']);
96
        }
97
        if (isset($options['delete'])) {
98
            $foreignKey->setDeleteAction($options['delete']);
99
        }
100
        $this->actions[] =  function () use ($foreignKey) {
101
             return $this->manipulation->create($foreignKey);
102
        };
103
104
        return $this;
105
    }
106
107
    /**
108
     * Drop a foreign key.
109
     *
110
     * @param string $referenceTable   Referenced table name.
111
     * @param string $referenceColumns Columns of referenced table.
112
     * @param array  $options          Optional options.
113
     *
114
     * @return TableApi Self.
115
     */
116
    public function dropForeignKey($referenceTable, $referenceColumns, array $options = [])
117
    {
118
        $schema = isset($options['schema']) ? new Schema($options['schema']) : null;
119
120
        $foreignKey = new ForeignKey($this, new Table($referenceTable, $schema));
121
        $foreignKey->setColumns($referenceColumns);
0 ignored issues
show
Documentation introduced by
$referenceColumns is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
122
        $foreignKey->setReferencedColumns($referenceColumns);
0 ignored issues
show
Documentation introduced by
$referenceColumns is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
124
        $this->actions[] =  function () use ($foreignKey) {
125
             return $this->manipulation->drop($foreignKey);
126
        };
127
128
        return $this;
129
    }
130
131
    /**
132
     * Add a unique constraint.
133
     *
134
     * @param array $columns Unique columns.
135
     *
136
     * @return TableApi Self.
137
     */
138 View Code Duplication
    public function addUnique($columns)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $unique = new Unique($columns, $this);
141
142
        $this->actions[] =  function () use ($unique) {
143
             return $this->manipulation->create($unique);
144
        };
145
146
        return $this;
147
    }
148
149
    /**
150
     * Drop a unique constraint.
151
     *
152
     * @param array $columns Unique columns.
153
     *
154
     * @return TableApi Self.
155
     */
156 View Code Duplication
    public function dropUnique($columns)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158
        $unique = new Unique($columns, $this);
159
160
        $this->actions[] =  function () use ($unique) {
161
             return $this->manipulation->drop($unique);
162
        };
163
164
        return $this;
165
    }
166
167
    /**
168
     * Add a index on columns.
169
     *
170
     * @param array $columns Index columns.
171
     *
172
     * @return TableApi Self.
173
     */
174 View Code Duplication
    public function addIndex($columns)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176
        $index = new Index($columns, $this);
177
178
        $this->actions[] =  function () use ($index) {
179
             return $this->manipulation->create($index);
180
        };
181
182
        return $this;
183
    }
184
185
    /**
186
     * Drop a index on columns.
187
     *
188
     * @param array $columns Index columns.
189
     *
190
     * @return TableApi Self.
191
     */
192 View Code Duplication
    public function dropIndex($columns)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
    {
194
        $index = new Index($columns, $this);
195
196
        $this->actions[] =  function () use ($index) {
197
             return $this->manipulation->drop($index);
198
        };
199
200
        return $this;
201
    }
202
}
203