DataReceiver::toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace JulianoBailao\DomusApi\Core;
4
5
class DataReceiver
6
{
7
    /**
8
     * The id from update requests.
9
     *
10
     * @var int
11
     */
12
    protected $id;
13
14
    /**
15
     * The Endpoint object.
16
     *
17
     * @var Endpoint
18
     */
19
    protected $endpoint;
20
21
    /**
22
     * The request data.
23
     *
24
     * @var array
25
     */
26
    protected $data;
27
28
    /**
29
     * Create a new DataReceiver instance.
30
     *
31
     * @param string $method
0 ignored issues
show
Bug introduced by
There is no parameter named $method. 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...
32
     * @param string $uri
0 ignored issues
show
Bug introduced by
There is no parameter named $uri. 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...
33
     * @param int    $id
34
     */
35 24
    public function __construct(Endpoint $endpoint, $id = null)
36
    {
37 24
        $this->id = $id;
38 24
        $this->endpoint = $endpoint;
39 24
        $this->data = new \stdClass();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \stdClass() of type object<stdClass> is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40 24
    }
41
42
    /**
43
     * Fill all the request data.
44
     *
45
     * @param array $data
46
     *
47
     * @return self
48
     */
49 3
    public function fill(array $data)
50
    {
51 3
        foreach ($data as $key => $value) {
52 3
            $this->data->{$key} = $value;
53 3
        }
54
55 3
        return $this;
56
    }
57
58
    /**
59
     * Get the request data.
60
     *
61
     * @return array
62
     */
63 21
    public function toArray()
64
    {
65 21
        $this->data->id = $this->id;
66
67 21
        return json_decode(json_encode($this->data), true);
68
    }
69
70
    /**
71
     * Run the save request.
72
     *
73
     * @return stdClass
74
     */
75 21
    public function save()
76
    {
77 21
        return $this->endpoint->save($this);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class JulianoBailao\DomusApi\Core\Endpoint as the method save() does only exist in the following sub-classes of JulianoBailao\DomusApi\Core\Endpoint: JulianoBailao\DomusApi\Endpoints\People, JulianoBailao\DomusApi\Endpoints\Products, JulianoBailao\DomusApi\Endpoints\Profitability, JulianoBailao\DomusApi\E...Secondary\Neighborhoods, JulianoBailao\DomusApi\Endpoints\Secondary\Streets. 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...
78
    }
79
80
    /**
81
     * Get the request id.
82
     *
83
     * @return int
84
     */
85 12
    public function getId()
86
    {
87 12
        return $this->id;
88
    }
89
90
    /**
91
     * Set a specific data point.
92
     *
93
     * @param string $name
94
     * @param string $value
95
     */
96 24
    public function __set($name, $value = null)
97
    {
98 24
        $this->data->{$name} = $value;
99 24
    }
100
101
    /**
102
     * Get a specific data point.
103
     *
104
     * @param string $name
105
     *
106
     * @return mixed
107
     */
108 3
    public function __get($name)
109
    {
110 3
        return $this->data->{$name};
111
    }
112
}
113