Completed
Pull Request — master (#76)
by
unknown
02:48
created

Boolean   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 81
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fromNative() 0 6 1
A __construct() 0 8 2
A sameValueAs() 0 8 2
A toBool() 0 4 1
A toNative() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace ValueObjects\BoolLiteral;
4
5
use ValueObjects\Exception\InvalidNativeArgumentException;
6
use ValueObjects\StringLiteral\StringLiteral;
7
use ValueObjects\Util\Util;
8
use ValueObjects\ValueObjectInterface;
9
10
class Boolean implements ValueObjectInterface
11
{
12
    /**
13
     * @var \ValueObjects\StringLiteral\StringLiteral
14
     */
15
    private $boolean;
16
17
    /**
18
     * Returns a Name objects form PHP native values
19
     *
20
     * @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...
21
     *
22
     * @return static
23
     */
24
    public static function fromNative()
25
    {
26
        $value = StringLiteral::fromNative(func_get_arg(0));
27
28
        return new static($value);
29
    }
30
31
    /**
32
     * Returns a Boolean object
33
     *
34
     * @param StringLiteral $boolean
35
     */
36
    public function __construct(StringLiteral $boolean)
37
    {
38
        if (null === \filter_var($boolean, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)) {
39
            throw new InvalidNativeArgumentException($boolean, array('string (boolean value)'));
40
        }
41
42
        $this->boolean = $boolean;
43
    }
44
45
    /**
46
     * Tells whether two booleans are equal by comparing their values
47
     *
48
     * @param  ValueObjectInterface $boolean
49
     *
50
     * @return bool
51
     */
52
    public function sameValueAs(ValueObjectInterface $boolean)
53
    {
54
        if (false === Util::classEquals($this, $boolean)) {
55
            return false;
56
        }
57
58
        return $this->boolean->toNative() == $boolean->toNative();
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 toNative() does only exist in the following implementations of said interface: ValueObjects\BoolLiteral\BoolLiteral, ValueObjects\BoolLiteral\Boolean, ValueObjects\Climate\Celsius, ValueObjects\Climate\Fahrenheit, ValueObjects\Climate\Kelvin, ValueObjects\Climate\RelativeHumidity, ValueObjects\Climate\Temperature, ValueObjects\DateTime\Hour, ValueObjects\DateTime\Minute, ValueObjects\DateTime\Month, ValueObjects\DateTime\MonthDay, ValueObjects\DateTime\Second, ValueObjects\DateTime\WeekDay, ValueObjects\DateTime\Year, ValueObjects\Enum\Enum, ValueObjects\Geography\Continent, ValueObjects\Geography\CountryCode, ValueObjects\Geography\DistanceFormula, ValueObjects\Geography\DistanceUnit, ValueObjects\Geography\Ellipsoid, ValueObjects\Geography\Latitude, ValueObjects\Geography\Longitude, ValueObjects\Identity\UUID, ValueObjects\Money\CurrencyCode, ValueObjects\Number\Complex, ValueObjects\Number\Integer, ValueObjects\Number\Natural, ValueObjects\Number\Real, ValueObjects\Number\RoundingMode, ValueObjects\Person\Age, ValueObjects\Person\Gender, ValueObjects\StringLiteral\StringLiteral, ValueObjects\Web\Domain, ValueObjects\Web\EmailAddress, ValueObjects\Web\FragmentIdentifier, ValueObjects\Web\Hostname, ValueObjects\Web\IPAddress, ValueObjects\Web\IPAddressVersion, ValueObjects\Web\IPv4Address, ValueObjects\Web\IPv6Address, ValueObjects\Web\NullFragmentIdentifier, ValueObjects\Web\NullQueryString, ValueObjects\Web\Path, ValueObjects\Web\PortNumber, ValueObjects\Web\QueryString, ValueObjects\Web\SchemeName.

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...
59
    }
60
61
    /**
62
     * Returns the bool value of the boolean
63
     *
64
     * @return bool
65
     */
66
    public function toBool()
67
    {
68
        return \filter_var($this->boolean, FILTER_VALIDATE_BOOLEAN);
69
    }
70
71
    /**
72
     * Returns the string value of the boolean
73
     *
74
     * @return string
75
     */
76
    public function toNative()
77
    {
78
        return \strval($this->boolean);
79
    }
80
81
    /**
82
     * Returns the string value of the boolean
83
     *
84
     * @return string
85
     */
86
    public function __toString()
87
    {
88
        return $this->toNative();
89
    }
90
}
91