Passed
Pull Request — master (#11)
by Pavel
11:58
created

SymfonyNormalizerAdapter::normalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Bankiru\Api\JsonRpc\Adapters\Symfony;
4
5
use Bankiru\Api\JsonRpc\NormalizerInterface;
6
use Symfony\Component\Serializer\Normalizer\NormalizerInterface as SymfonyNormalizerInterface;
7
8
final class SymfonyNormalizerAdapter implements NormalizerInterface
9
{
10
    /** @var SymfonyNormalizerInterface */
11
    private $normalizer;
12
13
    /**
14
     * SymfonyNormalizerAdapter constructor.
15
     *
16
     * @param SymfonyNormalizerInterface $normalizer
17
     */
18
    public function __construct(SymfonyNormalizerInterface $normalizer)
19
    {
20
        $this->normalizer = $normalizer;
21
    }
22
23
    /** {@inheritdoc} */
24
    public function normalize($entity, array $context = [])
25
    {
26
        if (null === $entity) {
27
            return null;
28
        }
29
30
        return $this->normalizer->normalize($entity, ['groups' => $context]);
0 ignored issues
show
Bug introduced by
It seems like $entity defined by parameter $entity on line 24 can also be of type array; however, Symfony\Component\Serial...rInterface::normalize() does only seem to accept object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Documentation introduced by
array('groups' => $context) is of type array<string,array,{"groups":"array"}>, but the function expects a string|null.

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...
Bug Best Practice introduced by
The return type of return $this->normalizer...'groups' => $context)); (array|integer|double|string|boolean) is incompatible with the return type declared by the interface Bankiru\Api\JsonRpc\NormalizerInterface::normalize of type array|null.

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...
31
    }
32
}
33