UsersTwoFactorAuthTable::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 14
loc 14
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
namespace App\Model\Table;
3
4
use Cake\Database\Schema\Table as Schema;
5
use Cake\ORM\Table;
6
7
class UsersTwoFactorAuthTable extends Table
8
{
9
    /**
10
     * Initialize schema method
11
     *
12
     * @param \Cake\Database\Schema\Table $schema The schema of the Table.
13
     *
14
     * @return \Cake\Database\Schema\Table
15
     */
16
    protected function _initializeSchema(Schema $schema)
17
    {
18
        $schema->columnType('secret', 'encryptedsecurity');
19
        $schema->columnType('username', 'encryptedsecurity');
20
        $schema->columnType('session', 'encryptedsecurity');
21
        $schema->columnType('recovery_code', 'encryptedsecurity');
22
23
        return $schema;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $schema; (Cake\Database\Schema\Table) is incompatible with the return type of the parent method Cake\ORM\Table::_initializeSchema of type Cake\Database\Schema\TableSchema.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
24
    }
25
26
    /**
27
     * Initialize method
28
     *
29
     * @param array $config The configuration for the Table.
30
     *
31
     * @return void
32
     */
33 View Code Duplication
    public function initialize(array $config)
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...
34
    {
35
        parent::initialize($config);
36
37
        $this->table('users_two_factor_auth');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::table() has been deprecated with message: 3.4.0 Use setTable()/getTable() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
38
        $this->displayField('id');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::displayField() has been deprecated with message: 3.4.0 Use setDisplayField()/getDisplayField() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
39
        $this->primaryKey('id');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::primaryKey() has been deprecated with message: 3.4.0 Use setPrimaryKey()/getPrimaryKey() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
40
41
        $this->addBehavior('Timestamp');
42
43
        $this->belongsTo('Users', [
44
            'foreignKey' => 'user_id'
45
        ]);
46
    }
47
}
48