Passed
Push — master ( 4ca145...e39a6b )
by Jakub
02:24
created

extractFromRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 9
nc 2
nop 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Eps\Req2CmdBundle\CommandExtractor;
5
6
use JMS\Serializer\ArrayTransformerInterface;
7
use JMS\Serializer\SerializerInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
10
class JMSSerializerCommandExtractor implements CommandExtractorInterface
11
{
12
    /**
13
     * @var SerializerInterface
14
     */
15
    private $jmsSerializer;
16
17
    /**
18
     * @var ArrayTransformerInterface
19
     */
20
    private $jmsArrayTransformer;
21
22
    /**
23
     * JMSSerializerCommandExtractor constructor.
24
     * @param SerializerInterface $jmsSerializer
25
     * @param ArrayTransformerInterface $jmsArrayTransformer
26
     */
27
    public function __construct(SerializerInterface $jmsSerializer, ArrayTransformerInterface $jmsArrayTransformer)
28
    {
29
        $this->jmsSerializer = $jmsSerializer;
30
        $this->jmsArrayTransformer = $jmsArrayTransformer;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     * @throws \LogicException
36
     */
37 View Code Duplication
    public function extractFromRequest(Request $request, string $commandClass, array $additionalProps = [])
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        if (!empty($additionalProps)) {
40
            $decodedContent = $this->jmsSerializer->deserialize(
41
                $request->getContent(),
1 ignored issue
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, JMS\Serializer\SerializerInterface::deserialize() does only seem to accept string, maybe add an additional type check?

This check looks at variables that 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...
42
                'array',
43
                $request->getRequestFormat()
44
            );
45
            $finalProps = array_merge($decodedContent, $additionalProps);
46
            return $this->jmsArrayTransformer->fromArray($finalProps, $commandClass);
47
        }
48
49
        return $this->jmsSerializer->deserialize($request->getContent(), $commandClass, $request->getRequestFormat());
1 ignored issue
show
Bug introduced by
It seems like $request->getContent() targeting Symfony\Component\HttpFo...n\Request::getContent() can also be of type resource; however, JMS\Serializer\SerializerInterface::deserialize() does only seem to accept string, maybe add an additional type check?

This check looks at variables that 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...
Bug Best Practice introduced by
The return type of return $this->jmsSeriali...t->getRequestFormat()); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface Eps\Req2CmdBundle\Comman...ace::extractFromRequest of type object.

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...
50
    }
51
}
52