Completed
Push — master ( 4dd992...7f3992 )
by Cheren
04:17
created

Initial   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 158
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B up() 0 124 1
A down() 0 5 1
1
<?php
2
/**
3
 * CakeCMS Community
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package     Community
10
 * @license     MIT
11
 * @copyright   MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link        https://github.com/CakeCMS/Community".
13
 * @author      Sergey Kalistratov <[email protected]>
14
 */
15
16
use Migrations\AbstractMigration;
17
18
/**
19
 * Class Initial
20
 */
21
class Initial extends AbstractMigration
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
22
{
23
24
    /**
25
     * Whether the tables created in this migration
26
     * should auto-create an `id` field or not
27
     *
28
     * This option is global for all tables created in the migration file.
29
     * If you set it to false, you have to manually add the primary keys for your
30
     * tables using the Migrations\Table::addPrimaryKey() method
31
     *
32
     * @var bool
33
     */
34
    public $autoId = false;
35
36
    /**
37
     * Migrate Up.
38
     *
39
     * @return void
40
     * @throws InvalidArgumentException
41
     * @throws RuntimeException
42
     */
43
    public function up()
44
    {
45
        /** @var \Migrations\Table $table */
46
        $this->table('groups')
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Phinx\Db\Table as the method addPrimaryKey() does only exist in the following sub-classes of Phinx\Db\Table: Migrations\Table. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
47
            ->addColumn('id', 'integer', [
48
                'limit'         => 11,
49
                'autoIncrement' => true,
50
                'default'       => null,
51
                'null'          => false,
52
                'signed'        => false
53
            ])
54
            ->addPrimaryKey([
55
                'id'
56
            ])
57
            ->addColumn('parent_id', 'integer', [
58
                'limit'     => 10,
59
                'default'   => null,
60
                'null'      => true
61
            ])
62
            ->addColumn('name', 'string', [
63
                'limit'     => 100,
64
                'default'   => null,
65
                'null'      => false
66
            ])
67
            ->addColumn('slug', 'string', [
68
                'limit'     => 100,
69
                'default'   => null,
70
                'null'      => false
71
            ])
72
            ->addColumn('params', 'text', [
73
                'default'   => null,
74
                'limit'     => null,
75
                'null'      => true
76
            ])
77
            ->addColumn('lft', 'integer', [
78
                'limit'     => 10,
79
                'default'   => null,
80
                'null'      => true
81
            ])
82
            ->addColumn('rght', 'integer', [
83
                'limit'     => 10,
84
                'default'   => null,
85
                'null'      => true
86
            ])
87
            ->create();
88
89
        $this->table('users')
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Phinx\Db\Table as the method addPrimaryKey() does only exist in the following sub-classes of Phinx\Db\Table: Migrations\Table. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
90
            ->addColumn('id', 'integer', [
91
                'limit'         => 11,
92
                'autoIncrement' => true,
93
                'default'       => null,
94
                'null'          => false,
95
                'signed'        => false
96
            ])
97
            ->addPrimaryKey([
98
                'id'
99
            ])
100
            ->addColumn('group_id', 'integer', [
101
                'limit'     => 10,
102
                'default'   => null,
103
                'null'      => false
104
            ])
105
            ->addColumn('login', 'string', [
106
                'limit'     => 60,
107
                'default'   => null,
108
                'null'      => false
109
            ])
110
            ->addColumn('name', 'string', [
111
                'limit'     => 60,
112
                'default'   => null,
113
                'null'      => false
114
            ])
115
            ->addColumn('slug', 'string', [
116
                'limit'     => 60,
117
                'default'   => null,
118
                'null'      => false
119
            ])
120
            ->addColumn('email', 'string', [
121
                'limit'     => 50,
122
                'default'   => null,
123
                'null'      => false
124
            ])
125
            ->addColumn('password', 'string', [
126
                'limit'     => 100,
127
                'default'   => null,
128
                'null'      => false
129
            ])
130
            ->addColumn('token', 'string', [
131
                'limit'     => 60,
132
                'default'   => null,
133
                'null'      => false
134
            ])
135
            ->addColumn('status', 'boolean', [
136
                'limit'     => null,
137
                'default'   => false,
138
                'null'      => false
139
            ])
140
            ->addColumn('params', 'text', [
141
                'default'   => null,
142
                'limit'     => null,
143
                'null'      => true
144
            ])
145
            ->addColumn('last_login', 'datetime', [
146
                'limit'     => null,
147
                'null'      => true,
148
                'default'   => '0000-00-00 00:00:00'
149
            ])
150
            ->addColumn('last_action', 'datetime', [
151
                'limit'     => null,
152
                'null'      => true,
153
                'default'   => '0000-00-00 00:00:00'
154
            ])
155
            ->addColumn('modified', 'datetime', [
156
                'limit'     => null,
157
                'null'      => true,
158
                'default'   => '0000-00-00 00:00:00'
159
            ])
160
            ->addColumn('created', 'datetime', [
161
                'limit'     => null,
162
                'null'      => true,
163
                'default'   => '0000-00-00 00:00:00'
164
            ])
165
            ->create();
166
    }
167
168
    /**
169
     * Migrate down.
170
     *
171
     * @return void
172
     */
173
    public function down()
174
    {
175
        $this->dropTable('groups');
176
        $this->dropTable('users');
177
    }
178
}
179