Test Failed
Push — master ( a6b51e...5fffdb )
by Gabriel
08:05
created

Token::writeData()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.432

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
ccs 7
cts 10
cp 0.7
crap 4.432
1
<?php
2
3
namespace ByTIC\Hello\Models\AccessTokens;
4
5
use League\OAuth2\Server\Entities\ClientEntityInterface;
6
use League\OAuth2\Server\Entities\Traits\EntityTrait;
7
use League\OAuth2\Server\Entities\Traits\AccessTokenTrait;
8
use League\OAuth2\Server\Entities\Traits\TokenEntityTrait;
9
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
10
11
/**
12
 * Class Token
13
 * @package ByTIC\Auth\Models\AccessTokens
14
 *
15
 * @property int $user_id
16
 * @property int $client_id
17
 * @property string $revoked
18
 * @property string $expires_at
19
 */
20
class Token extends \Nip\Records\Record implements AccessTokenEntityInterface
21
{
22
    use EntityTrait {
23
        setIdentifier as setIdentifierTrait;
24
    }
25
    use TokenEntityTrait {
26
        getClient as getClientTrait;
27
        setUserIdentifier as setUserIdentifierTrait;
28
    }
29
    use AccessTokenTrait;
30
31
    /**
32
     * @inheritDoc
33
     */
34 4
    public function writeData($data = false)
35
    {
36 4
        parent::writeData($data);
37 4
        if (isset($data['expires_at'])) {
38
            $date = new \DateTime($data['expires_at']);
39
            $this->setExpiryDateTime($date);
40
        }
41 4
        if (isset($data['identifier'])) {
42
            $this->setIdentifier($data['identifier']);
43
        }
44 4
        if (isset($data['user_id'])) {
45 1
            $this->setUserIdentifier($data['user_id']);
46
        }
47 4
    }
48
49
    /**
50
     * @param ClientEntityInterface $clientEntity
51
     */
52 1
    public function populateFromClient(ClientEntityInterface $clientEntity)
53
    {
54 1
        $this->setClient($clientEntity);
55 1
        $this->client_id = $clientEntity->getIdentifier();
56 1
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61 2
    public function getClient()
62
    {
63 2
        $client = $this->getClientTrait();
64 2
        if ($client instanceof ClientEntityInterface) {
65 1
            return $client;
66
        }
67
        /** @var ClientEntityInterface $relationClient */
68 1
        $relationClient = $this->getRelation('Client')->getResults();
69 1
        $this->setClient($relationClient);
70 1
        return $relationClient;
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76 1
    public function setIdentifier($value)
77
    {
78 1
        $this->_data['identifier'] = $value;
79 1
        $this->setIdentifierTrait($value);
80 1
    }
81
82
    /**
83
     * @param int|string|null $identifier
84
     */
85 2
    public function setUserIdentifier($identifier)
86
    {
87 2
        $this->setUserIdentifierTrait($identifier);
88 2
        $this->user_id = $this->getUserIdentifier();
89 2
    }
90
91
    /**
92
     * @param $scopes
93
     */
94 1
    public function addScopes($scopes)
95
    {
96 1
        foreach ($scopes as $scope) {
97
            $this->addScope($scope);
98
        }
99 1
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    public function insert()
105
    {
106
        $this->castExpireDateTime();
107
        return parent::insert();
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function update()
114
    {
115
        $this->castExpireDateTime();
116
        return parent::insert();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::insert(); (boolean) is incompatible with the return type of the parent method Nip\Records\AbstractModels\Record::update of type Nip\Database\Result|false.

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...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (insert() instead of update()). Are you sure this is correct? If so, you might want to change this to $this->insert().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
117
    }
118
119
    protected function castExpireDateTime()
120
    {
121
        $date = $this->getExpiryDateTime();
122
        $this->expires_at = ($date) ? $date->format('Y-m-d') : '';
123
    }
124
}
125