Completed
Pull Request — master (#71)
by
unknown
02:09
created

MacAddress::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace ValueObjects\Identity;
4
5
use ValueObjects\Exception\InvalidNativeArgumentException;
6
use ValueObjects\StringLiteral\StringLiteral;
7
use ValueObjects\Util\Util;
8
use ValueObjects\ValueObjectInterface;
9
10
class MacAddress extends StringLiteral
11
{
12
    /**
13
     * Returns a MacAddress
14
     *
15
     * @param string $value
16
     */
17
    public function __construct($value)
18
    {
19
        $filteredValue = filter_var($value, FILTER_VALIDATE_MAC);
20
21
        if ($filteredValue === false) {
22
            throw new InvalidNativeArgumentException($value, array('string (valid Mac address)'));
23
        }
24
25
        $this->value = $filteredValue;
26
    }
27
28
    private function lowercaseHex()
29
    {
30
        return strtolower(str_replace(['-', ':', '.'], '', $this->toNative()));
31
    }
32
33
    public function sameValueAs(ValueObjectInterface $macAddress)
34
    {
35
        if (false === Util::classEquals($this, $macAddress)) {
36
            return false;
37
        }
38
39
        return $this->lowercaseHex() === $macAddress->lowercaseHex();
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 lowercaseHex() does only exist in the following implementations of said interface: ValueObjects\Identity\MacAddress.

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...
40
    }
41
}
42