Name   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 82.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 141
ccs 29
cts 35
cp 0.8286
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A fromNative() 0 10 1
A __construct() 0 6 1
A getFirstName() 0 4 1
A getMiddleName() 0 4 1
A getLastName() 0 4 1
A getFullName() 0 10 3
A sameValueAs() 0 8 2
A __toString() 0 4 1
A jsonSerialize() 0 11 1
1
<?php
2
3
namespace ValueObjects\Person;
4
5
use ValueObjects\StringLiteral\StringLiteral;
6
use ValueObjects\Util\Util;
7
use ValueObjects\ValueObjectInterface;
8
9
class Name implements ValueObjectInterface
10
{
11
    /**
12
     * First name
13
     *
14
     * @var \ValueObjects\StringLiteral\StringLiteral
15
     */
16
    private $firstName;
17
18
    /**
19
     * Middle name
20
     *
21
     * @var \ValueObjects\StringLiteral\StringLiteral
22
     */
23
    private $middleName;
24
25
    /**
26
     * Last name
27
     *
28
     * @var \ValueObjects\StringLiteral\StringLiteral
29
     */
30
    private $lastName;
31
32
    /**
33
     * Returns a Name objects form PHP native values
34
     *
35
     * @param  string $first_name
0 ignored issues
show
Bug introduced by
There is no parameter named $first_name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
36
     * @param  string $middle_name
0 ignored issues
show
Bug introduced by
There is no parameter named $middle_name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
37
     * @param  string $last_name
0 ignored issues
show
Bug introduced by
There is no parameter named $last_name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
38
     * @return Name
39
     */
40 1
    public static function fromNative()
41
    {
42 1
        $args = func_get_args();
43
44 1
        $firstName  = new StringLiteral($args[0]);
45 1
        $middleName = new StringLiteral($args[1]);
46 1
        $lastName   = new StringLiteral($args[2]);
47
48 1
        return new static($firstName, $middleName, $lastName);
49
    }
50
51
    /**
52
     * Returns a Name object
53
     *
54
     * @param StringLiteral $first_name
55
     * @param StringLiteral $middle_name
56
     * @param StringLiteral $last_name
57
     */
58 8
    public function __construct(StringLiteral $first_name, StringLiteral $middle_name, StringLiteral $last_name)
59
    {
60 8
        $this->firstName  = $first_name;
61 8
        $this->middleName = $middle_name;
62 8
        $this->lastName   = $last_name;
63 8
    }
64
65
    /**
66
     * Returns the first name
67
     *
68
     * @return StringLiteral
69
     */
70 1
    public function getFirstName()
71
    {
72 1
        return $this->firstName;
73
    }
74
75
    /**
76
     * Returns the middle name
77
     *
78
     * @return StringLiteral
79
     */
80 1
    public function getMiddleName()
81
    {
82 1
        return $this->middleName;
83
    }
84
85
    /**
86
     * Returns the last name
87
     *
88
     * @return StringLiteral
89
     */
90 1
    public function getLastName()
91
    {
92 1
        return $this->lastName;
93
    }
94
95
    /**
96
     * Returns the full name
97
     *
98
     * @return StringLiteral
99
     */
100 5
    public function getFullName()
101
    {
102 5
        $fullNameString = $this->firstName .
103 5
            ($this->middleName->isEmpty() ? '' : ' ' . $this->middleName) .
104 5
            ($this->lastName->isEmpty() ? '' : ' ' . $this->lastName);
105
106 5
        $fullName = new StringLiteral($fullNameString);
107
108 5
        return $fullName;
109
    }
110
111
    /**
112
     * Tells whether two names are equal by comparing their values
113
     *
114
     * @param  ValueObjectInterface $name
115
     * @return bool
116
     */
117 2
    public function sameValueAs(ValueObjectInterface $name)
118
    {
119 2
        if (false === Util::classEquals($this, $name)) {
120 1
            return false;
121
        }
122
123 2
        return $this->getFullName() == $name->getFullName();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface ValueObjects\ValueObjectInterface as the method getFullName() does only exist in the following implementations of said interface: ValueObjects\Person\Name.

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...
124
    }
125
126
    /**
127
     * Returns the full name
128
     *
129
     * @return string
130
     */
131 1
    public function __toString()
132
    {
133 1
        return \strval($this->getFullName());
134
    }
135
136
    function jsonSerialize()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for jsonSerialize.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
137
    {
138
        return [
139
            'parts' => [
140
                'first' => $this->getFirstName(),
141
                'middle' => $this->getMiddleName(),
142
                'last' => $this->getLastName()
143
            ],
144
            'full' => (string) $this
145
        ];
146
    }
147
148
149
}
150