Real   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 102
ccs 30
cts 30
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A fromNative() 0 6 1
A __construct() 0 10 2
A toNative() 0 4 1
A sameValueAs() 0 8 2
A toInteger() 0 12 2
A toNatural() 0 8 1
A __toString() 0 4 1
1
<?php
2
3
namespace ValueObjects\Number;
4
5
use ValueObjects\Exception\InvalidNativeArgumentException;
6
use ValueObjects\Util\Util;
7
use ValueObjects\ValueObjectInterface;
8
9
class Real implements ValueObjectInterface, NumberInterface
10
{
11
    protected $value;
12
13
    /**
14
     * Returns a Real object given a PHP native float as parameter.
15
     *
16
     * @param  float  $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...
17
     * @return static
18
     */
19 4
    public static function fromNative()
20
    {
21 4
        $value = func_get_arg(0);
22
23 4
        return new static($value);
24
    }
25
26
    /**
27
     * Returns a Real object given a PHP native float as parameter.
28
     *
29
     * @param float $value
30
     */
31 128
    public function __construct($value)
32
    {
33 128
        $filteredValue = \filter_var($value, FILTER_VALIDATE_FLOAT);
34
35 128
        if (false === $filteredValue) {
36 1
            throw new InvalidNativeArgumentException($value, array('float'));
37
        }
38
39 127
        $this->value = $filteredValue;
40 127
    }
41
42
    /**
43
     * Returns the native value of the real number
44
     *
45
     * @return float
46
     */
47 113
    public function toNative()
48
    {
49 113
        return $this->value;
50
    }
51
52
    /**
53
     * Tells whether two Real are equal by comparing their values
54
     *
55
     * @param  ValueObjectInterface $real
56
     * @return bool
57
     */
58 11
    public function sameValueAs(ValueObjectInterface $real)
59
    {
60 11
        if (false === Util::classEquals($this, $real)) {
61 1
            return false;
62
        }
63
64 11
        return $this->toNative() === $real->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\Boolean\BoolLiteral, ValueObjects\Boolean\BooleanString, 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\Imei, ValueObjects\Identity\MacAddress, 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...
65
    }
66
67
    /**
68
     * Returns the integer part of the Real number as a Integer
69
     *
70
     * @param  RoundingMode $rounding_mode Rounding mode of the conversion. Defaults to RoundingMode::HALF_UP.
71
     * @return Integer
72
     */
73 2
    public function toInteger(RoundingMode $rounding_mode = null)
74
    {
75 2
        if (null === $rounding_mode) {
76 2
            $rounding_mode = RoundingMode::HALF_UP();
77 2
        }
78
79 2
        $value        = $this->toNative();
80 2
        $integerValue = \round($value, 0, $rounding_mode->toNative());
81 2
        $integer      = new Integer($integerValue);
82
83 2
        return $integer;
84
    }
85
86
    /**
87
     * Returns the absolute integer part of the Real number as a Natural
88
     *
89
     * @param  RoundingMode $rounding_mode Rounding mode of the conversion. Defaults to RoundingMode::HALF_UP.
90
     * @return Natural
91
     */
92 1
    public function toNatural(RoundingMode $rounding_mode = null)
93
    {
94 1
        $integerValue = $this->toInteger($rounding_mode)->toNative();
95 1
        $naturalValue = \abs($integerValue);
96 1
        $natural      = new Natural($naturalValue);
97
98 1
        return $natural;
99
    }
100
101
    /**
102
     * Returns the string representation of the real value
103
     *
104
     * @return string
105
     */
106 4
    public function __toString()
107
    {
108 4
        return \strval($this->toNative());
109
    }
110
}
111