GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

JmsSerializer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/*
3
 * Copyright (c) 2012-2014 Janos Szurovecz
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6
 * this software and associated documentation files (the "Software"), to deal in
7
 * the Software without restriction, including without limitation the rights to
8
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
 * of the Software, and to permit persons to whom the Software is furnished to do
10
 * so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in all
13
 * copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
 * SOFTWARE.
22
 */
23
24
namespace predaddy\serializer;
25
26
use JMS\Serializer\SerializerInterface;
27
use precore\lang\ObjectClass;
28
use precore\lang\ObjectInterface;
29
30
final class JmsSerializer implements Serializer
31
{
32
    /**
33
     * @var SerializerInterface
34
     */
35
    private $jmsSerializer;
36
37
    /**
38
     * @var string
39
     */
40
    private $format;
41
42
    /**
43
     * @param SerializerInterface $jmsSerializer
44
     * @param string $format xml|json|yaml
45
     */
46
    public function __construct(SerializerInterface $jmsSerializer, $format)
47
    {
48
        $this->jmsSerializer = $jmsSerializer;
49
        $this->format = $format;
50
    }
51
52
    /**
53
     * @param ObjectInterface $object
54
     * @return string
55
     */
56
    public function serialize(ObjectInterface $object)
57
    {
58
        return $this->jmsSerializer->serialize($object, $this->format);
59
    }
60
61
    /**
62
     * @param string $serialized
63
     * @param ObjectClass $outputClass
64
     * @return ObjectInterface
65
     */
66
    public function deserialize($serialized, ObjectClass $outputClass)
67
    {
68
        return $this->jmsSerializer->deserialize($serialized, $outputClass->getName(), $this->format);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->jmsSeriali...Name(), $this->format); (object|array|integer|double|string|boolean) is incompatible with the return type declared by the interface predaddy\serializer\Serializer::deserialize of type precore\lang\ObjectInterface.

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...
69
    }
70
}
71