KeyValuePair   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 98
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A fromNative() 0 15 2
A __construct() 0 5 1
A sameValueAs() 0 8 3
A getKey() 0 4 1
A getValue() 0 4 1
A __toString() 0 6 1
A jsonSerialize() 0 4 1
1
<?php
2
3
namespace ValueObjects\Structure;
4
5
use ValueObjects\StringLiteral\StringLiteral;
6
use ValueObjects\Util\Util;
7
use ValueObjects\ValueObjectInterface;
8
9
class KeyValuePair implements ValueObjectInterface
10
{
11
    /** @var ValueObjectInterface */
12
    protected $key;
13
14
    /** @var ValueObjectInterface */
15
    protected $value;
16
17
    /**
18
     * Returns a KeyValuePair from native PHP arguments evaluated as strings
19
     *
20
     * @param string $key
0 ignored issues
show
Bug introduced by
There is no parameter named $key. 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...
21
     * @param string $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. 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...
22
     * @return self
23
     * @throws \InvalidArgumentException
24
     */
25 2
    public static function fromNative()
26
    {
27 2
        $args = func_get_args();
28
29 2
        if (count($args) != 2) {
30 1
            throw new \BadMethodCallException('This methods expects two arguments. One for the key and one for the value.');
31
        }
32
33 1
        $keyString   = \strval($args[0]);
34 1
        $valueString = \strval($args[1]);
35 1
        $key   = new StringLiteral($keyString);
36 1
        $value = new StringLiteral($valueString);
37
38 1
        return new static($key, $value);
39
    }
40
41
    /**
42
     * Returns a KeyValuePair
43
     *
44
     * @param ValueObjectInterface $key
45
     * @param ValueObjectInterface $value
46
     */
47 13
    public function __construct(ValueObjectInterface $key, ValueObjectInterface $value)
48
    {
49 13
        $this->key   = $key;
50 13
        $this->value = $value;
51 13
    }
52
53
    /**
54
     * Tells whether two KeyValuePair are equal
55
     *
56
     * @param  ValueObjectInterface $keyValuePair
57
     * @return bool
58
     */
59 4
    public function sameValueAs(ValueObjectInterface $keyValuePair)
60
    {
61 4
        if (false === Util::classEquals($this, $keyValuePair)) {
62 1
            return false;
63
        }
64
65 4
        return $this->getKey()->sameValueAs($keyValuePair->getKey()) && $this->getValue()->sameValueAs($keyValuePair->getValue());
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 getKey() does only exist in the following implementations of said interface: ValueObjects\Structure\KeyValuePair.

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...
Bug introduced by
It seems like you code against a concrete implementation and not the interface ValueObjects\ValueObjectInterface as the method getValue() does only exist in the following implementations of said interface: ValueObjects\DateTime\Month, ValueObjects\DateTime\WeekDay, ValueObjects\Enum\Enum, ValueObjects\Geography\Continent, ValueObjects\Geography\CountryCode, ValueObjects\Geography\DistanceFormula, ValueObjects\Geography\DistanceUnit, ValueObjects\Geography\Ellipsoid, ValueObjects\Money\CurrencyCode, ValueObjects\Number\RoundingMode, ValueObjects\Person\Gender, ValueObjects\Structure\KeyValuePair, ValueObjects\Web\IPAddressVersion.

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...
66
    }
67
68
    /**
69
     * Returns key
70
     *
71
     * @return ValueObjectInterface
72
     */
73 8
    public function getKey()
74
    {
75 8
        return clone $this->key;
76
    }
77
78
    /**
79
     * Returns value
80
     *
81
     * @return ValueObjectInterface
82
     */
83 8
    public function getValue()
84
    {
85 8
        return clone $this->value;
86
    }
87
88
    /**
89
     * Returns a string representation of the KeyValuePair in format "$key => $value"
90
     *
91
     * @return string
92
     */
93 1
    public function __toString()
94
    {
95 1
        $string = sprintf('%s => %s', $this->getKey(), $this->getValue());
96
97 1
        return $string;
98
    }
99
100
    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...
101
    {
102
        return [$this->getKey(), $this->getValue()];
103
    }
104
105
106
}
107