KeyValuePair::getKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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