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

TwitterDeleteSerializer::unserialize()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 6
Bugs 2 Features 1
Metric Value
c 6
b 2
f 1
dl 0
loc 22
ccs 14
cts 14
cp 1
rs 8.9197
cc 4
eloc 15
nc 3
nop 2
crap 4
1
<?php
2
3
namespace Twitter\Serializer;
4
5
use Twitter\Object\TwitterDelete;
6
use Twitter\TwitterSerializable;
7
use Twitter\TwitterSerializer;
8
9
class TwitterDeleteSerializer implements TwitterSerializer
10
{
11
    /**
12
     * @param  TwitterSerializable $object
13
     * @return \stdClass
14
     */
15 12
    public function serialize(TwitterSerializable $object)
16
    {
17 12
        if (!$this->canSerialize($object)) {
18 3
            throw new \InvalidArgumentException('$object must be an instance of TwitterDelete');
19
        }
20
21 9
        $refObject = new \stdClass();
22 9
        $refObject->id = $object->getId();
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 getId() does only exist in the following implementations of said interface: Twitter\Object\AbstractMessage, Twitter\Object\AbstractTwitterMedia, Twitter\Object\Tweet, Twitter\Object\TwitterDelete, Twitter\Object\TwitterDirectMessage, Twitter\Object\TwitterExtendedEntity, Twitter\Object\TwitterMedia, Twitter\Object\TwitterUser, Twitter\Object\TwitterUserMention.

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...
23 9
        $refObject->user_id = $object->getUserId();
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 getUserId() does only exist in the following implementations of said interface: Twitter\Object\TwitterDelete.

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...
24
25 9
        $obj = new \stdClass();
26
27 9
        switch ($object->getType()) {
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 getType() does only exist in the following implementations of said interface: Twitter\Object\AbstractTwitterMedia, Twitter\Object\TwitterCoordinates, Twitter\Object\TwitterDelete, Twitter\Object\TwitterEvent, Twitter\Object\TwitterExtendedEntity, Twitter\Object\TwitterMedia.

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...
28 9
            case TwitterDelete::TWEET:
29 3
                $obj->status = $refObject;
30 3
                break;
31 6
            case TwitterDelete::DM:
32 3
                $obj->direct_message = $refObject;
33 3
                break;
34 2
            default:
35 3
                throw new \InvalidArgumentException('Invalid delete type');
36 6
        }
37
38 6
        if ($object->getDate()) {
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 getDate() does only exist in the following implementations of said interface: Twitter\Object\AbstractMessage, Twitter\Object\Tweet, Twitter\Object\TwitterDelete, Twitter\Object\TwitterDirectMessage, Twitter\Object\TwitterEvent.

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...
39 6
            $obj->timestamp_ms = $object->getDate()->getTimestamp() * 1000;
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 getDate() does only exist in the following implementations of said interface: Twitter\Object\AbstractMessage, Twitter\Object\Tweet, Twitter\Object\TwitterDelete, Twitter\Object\TwitterDirectMessage, Twitter\Object\TwitterEvent.

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...
40 4
        }
41
42 6
        $delete = new \stdClass();
43 6
        $delete->delete = $obj;
44
45 6
        return $delete;
46
    }
47
48
    /**
49
     * @param  \stdClass $obj
50
     * @param  array     $context
51
     * @return TwitterDelete
52
     */
53 6
    public function unserialize($obj, array $context = [])
54
    {
55 6
        if (!$this->canUnserialize($obj)) {
56
            throw new \InvalidArgumentException('$object is not unserializable');
57 6
        }
58 3
59 3
        $d = $obj->delete;
60 2
        if (isset($d->status)) {
61 3
            $ref = $d->status;
62 3
            $type = TwitterDelete::TWEET;
63
        } else {
64
            $ref = $d->direct_message;
65 6
            $type = TwitterDelete::DM;
66 4
        }
67 6
68 6
        return TwitterDelete::create(
69 6
            $type,
70 4
            $ref->id,
71
            $ref->user_id,
72
            (new \DateTimeImmutable())->setTimestamp((int) floor($d->timestamp_ms / 1000))?:new \DateTimeImmutable()
73
        );
74
    }
75
76 6
    /**
77
     * @param  TwitterSerializable $object
78 6
     * @return boolean
79
     */
80
    public function canSerialize(TwitterSerializable $object)
81
    {
82
        return $object instanceof TwitterDelete;
83
    }
84
85
    /**
86
     * @param  \stdClass $object
87
     * @return boolean
88
     */
89
    public function canUnserialize($object)
90
    {
91
        return isset($object->delete);
92
    }
93
94
    /**
95
     * @return TwitterDeleteSerializer
96
     */
97
    public static function build()
98
    {
99
        return new self();
100
    }
101
}
102