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

TwitterMediaSizeSerializer::canSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

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...
25 3
        $size->h = $object->getHeight();
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 getHeight() does only exist in the following implementations of said interface: Twitter\Object\TwitterMediaSize.

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...
26 3
        $size->resize = $object->getResize();
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 getResize() does only exist in the following implementations of said interface: Twitter\Object\TwitterMediaSize.

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...
27
28 3
        return $size;
29
    }
30
31
    /**
32
     * @param  \stdClass $obj
33
     * @param  array     $context
34
     * @return TwitterMediaSize
35
     */
36 3
    public function unserialize($obj, array $context = [])
37
    {
38 3
        if (!$this->canUnserialize($obj)) {
39
            throw new \InvalidArgumentException('$object is not unserializable');
40
        }
41
42
        return TwitterMediaSize::create($context[self::NAME_VAR], $obj->w, $obj->h, $obj->resize);
43
    }
44 27
45
    /**
46 27
     * @param  TwitterSerializable $object
47
     * @return boolean
48
     */
49
    public function canSerialize(TwitterSerializable $object)
50
    {
51
        return $object instanceof TwitterMediaSize;
52
    }
53
54
    /**
55
     * @param  \stdClass $object
56
     * @return boolean
57
     */
58
    public function canUnserialize($object)
59
    {
60
        return isset($object->w) &&  isset($object->h);
61
    }
62
63
    /**
64
     * @return TwitterMediaSizeSerializer
65
     */
66
    public static function build()
67
    {
68
        return new self();
69
    }
70
}
71