Completed
Push — master ( 8bcfe7...12792b )
by Rémi
03:26
created

TwitterEntityIndicesSerializer::unserialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Twitter\Serializer;
4
5
use Twitter\Object\TwitterEntityIndices;
6
use Twitter\TwitterSerializable;
7
use Twitter\TwitterSerializer;
8
9
class TwitterEntityIndicesSerializer implements TwitterSerializer
10
{
11
    /**
12
     * @param  TwitterSerializable $object
13
     * @return array
14
     */
15 6
    public function serialize(TwitterSerializable $object)
16
    {
17 6
        if (!$this->canSerialize($object)) {
18 3
            throw new \InvalidArgumentException('$object must be an instance of TwitterEntityIndices');
19
        }
20
21 3
        return [$object->getFrom(), $object->getTo()];
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getFrom() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntityIndices.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
Bug introduced by
It seems like you code against a concrete implementation and not the interface Twitter\TwitterSerializable as the method getTo() does only exist in the following implementations of said interface: Twitter\Object\TwitterEntityIndices.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
22
    }
23
24
    /**
25
     * @param  array $array
26
     * @param  array $context
27
     * @return TwitterEntityIndices
28
     */
29 3
    public function unserialize($array, array $context = [])
30
    {
31 3
        if (!$this->canUnserialize($array)) {
0 ignored issues
show
Documentation introduced by
$array is of type array, but the function expects a object<stdClass>.

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...
32
            throw new \InvalidArgumentException('$object is not unserializable');
33
        }
34
35
        return TwitterEntityIndices::create($array[0], $array[1]);
36
    }
37 39
38
    /**
39 39
     * @param  TwitterSerializable $object
40
     * @return boolean
41
     */
42
    public function canSerialize(TwitterSerializable $object)
43
    {
44
        return $object instanceof TwitterEntityIndices;
45
    }
46
47
    /**
48
     * @param  \stdClass $object
49
     * @return boolean
50
     */
51
    public function canUnserialize($object)
52
    {
53
        return is_array($object) && count($object) === 2;
54
    }
55
56
    /**
57
     * @return TwitterEntityIndicesSerializer
58
     */
59
    public static function build()
60
    {
61
        return new self();
62
    }
63
}
64