ResourceResponse::getFirstName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace DoS\UserBundle\OAuth;
4
5
use DoS\UserBundle\Model\CustomerInterface;
6
use HWI\Bundle\OAuthBundle\OAuth\Response\PathUserResponse;
7
8
class ResourceResponse extends PathUserResponse
9
{
10
    /**
11
     * @param array|string $response
12
     */
13
    public function setResponse($response)
14
    {
15
        parent::setResponse($response);
16
17
        if (count($this->response)) {
18
            $keys = array_keys($this->response);
19
            $this->setPaths(array_combine($keys, $keys));
20
        }
21
    }
22
23
    /**
24
     * @param string     $path
25
     * @param null|mixed $default
26
     *
27
     * @return null|mixed
28
     */
29
    public function getPathValue($path, $default = null)
30
    {
31
        return $this->getValueForPath($path) ?: $default;
32
    }
33
34
    /**
35
     * @return null|string
36
     */
37
    public function getFirstName()
38
    {
39
        return $this->getPathValue('firstname');
40
    }
41
42
    /**
43
     * @return null|string
44
     */
45
    public function getLastName()
46
    {
47
        return $this->getPathValue('lastname');
48
    }
49
50
    /**
51
     * @return null|string
52
     */
53
    public function getBirthday()
54
    {
55
        if ($birthday = $this->getPathValue('birthday')) {
56
            // YYYY-MM-DD
57
            if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $birthday)) {
58
                return \DateTime::createFromFormat('Y-m-d', $birthday);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \DateTime::create...at('Y-m-d', $birthday); (DateTime|false) is incompatible with the return type documented by DoS\UserBundle\OAuth\ResourceResponse::getBirthday of type null|string.

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...
59
            }
60
61
            throw new \LogicException('Unkonw birthday format.');
62
        }
63
64
        return $birthday;
65
    }
66
67
    /**
68
     * @return null|string
69
     */
70
    public function getGender()
71
    {
72
        $gender = CustomerInterface::UNKNOWN_GENDER;
73
74
        switch($this->getPathValue('gender')) {
75
            case 'male':
76
                $gender = CustomerInterface::MALE_GENDER;
77
                break;
78
79
            case 'female':
80
                $gender = CustomerInterface::FEMALE_GENDER;
81
                break;
82
        }
83
84
        return $gender;
85
    }
86
87
    /**
88
     * @return string|null
89
     */
90
    public function getLocale()
91
    {
92
        return $this->getPathValue('locale');
93
    }
94
}
95