Street::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 4
crap 3
1
<?php
2
3
namespace ValueObjects\Geography;
4
5
use ValueObjects\StringLiteral\StringLiteral;
6
use ValueObjects\Util\Util;
7
use ValueObjects\ValueObjectInterface;
8
9
class Street implements ValueObjectInterface
10
{
11
    /** @var StringLiteral */
12
    protected $name;
13
14
    /** @var StringLiteral */
15
    protected $number;
16
17
    /** @var StringLiteral Building, floor and unit */
18
    protected $elements;
19
20
    /**
21
     * @var StringLiteral __toString() format
22
     * Use properties corresponding placeholders: %name%, %number%, %elements%
23
     */
24
    protected $format;
25
26
    /**
27
     * Returns a new Street from native PHP string name and number.
28
     *
29
     * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. 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...
30
     * @param string $number
0 ignored issues
show
Bug introduced by
There is no parameter named $number. 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...
31
     * @param string $elements
0 ignored issues
show
Bug introduced by
There is no parameter named $elements. 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...
32
     * @return Street
33
     * @throws \BadFunctionCallException
34
     */
35 2
    public static function fromNative()
36
    {
37 2
        $args = func_get_args();
38
39 2
        if (\count($args) < 2) {
40 1
            throw new \BadMethodCallException('You must provide from 2 to 4 arguments: 1) street name, 2) street number, 3) elements, 4) format (optional)');
41
        }
42
43 1
        $nameString     = $args[0];
44 1
        $numberString   = $args[1];
45 1
        $elementsString = isset($args[2]) ? $args[2] : null;
46 1
        $formatString   = isset($args[3]) ? $args[3] : null;
47
48 1
        $name     = new StringLiteral($nameString);
49 1
        $number   = new StringLiteral($numberString);
50 1
        $elements = $elementsString ? new StringLiteral($elementsString) : null;
51 1
        $format   = $formatString ? new StringLiteral($formatString) : null;
52
53 1
        return new static($name, $number, $elements, $format);
54
    }
55
56
    /**
57
     * Returns a new Street object
58
     *
59
     * @param StringLiteral $name
60
     * @param StringLiteral $number
61
     */
62 18
    public function __construct(StringLiteral $name, StringLiteral $number, StringLiteral $elements = null, StringLiteral $format = null)
63
    {
64 18
        $this->name     = $name;
65 18
        $this->number   = $number;
66
67 18
        if ($elements === null) {
68 12
            $elements = new StringLiteral('');
69 12
        }
70 18
        $this->elements = $elements;
71
72 18
        if ($format === null) {
73 13
            $format = new StringLiteral('%number% %name%');
74 13
        }
75 18
        $this->format   = $format;
76 18
    }
77
78
    /**
79
     * Tells whether two Street objects are equal
80
     * @param  ValueObjectInterface $street
81
     * @return bool
82
     */
83 5
    public function sameValueAs(ValueObjectInterface $street)
84
    {
85 5
        if (false === Util::classEquals($this, $street)) {
86 1
            return false;
87
        }
88
89 5
        return $this->getName()->sameValueAs($street->getName()) &&
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 getName() does only exist in the following implementations of said interface: ValueObjects\DateTime\Month, ValueObjects\DateTime\TimeZone, ValueObjects\DateTime\WeekDay, ValueObjects\Enum\Enum, ValueObjects\Geography\Address, ValueObjects\Geography\Continent, ValueObjects\Geography\Country, ValueObjects\Geography\CountryCode, ValueObjects\Geography\DistanceFormula, ValueObjects\Geography\DistanceUnit, ValueObjects\Geography\Ellipsoid, ValueObjects\Geography\Street, ValueObjects\Money\CurrencyCode, ValueObjects\Number\RoundingMode, ValueObjects\Person\Gender, 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...
90 5
               $this->getNumber()->sameValueAs($street->getNumber()) &&
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 getNumber() does only exist in the following implementations of said interface: ValueObjects\Geography\Street.

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...
91 5
               $this->getElements()->sameValueAs($street->getElements())
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 getElements() does only exist in the following implementations of said interface: ValueObjects\Geography\Street.

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...
92 5
        ;
93
    }
94
95
    /**
96
     * Returns street name
97
     *
98
     * @return StringLiteral
99
     */
100 8
    public function getName()
101
    {
102 8
        return clone $this->name;
103
    }
104
105
    /**
106
     * Returns street number
107
     *
108
     * @return StringLiteral
109
     */
110 8
    public function getNumber()
111
    {
112 8
        return clone $this->number;
113
    }
114
115
    /**
116
     * Returns street elements
117
     * @return StringLiteral
118
     */
119 8
    public function getElements()
120
    {
121 8
        return clone $this->elements;
122
    }
123
124
    /**
125
     * Returns a string representation of the StringLiteral in the format defined in the constructor
126
     *
127
     * @return string
128
     */
129 2
    public function __toString()
130
    {
131
        $replacements = array(
132 2
            "%name%"     => $this->getName(),
133 2
            "%number%"   => $this->getNumber(),
134 2
            "%elements%" => $this->getElements()
135 2
        );
136
137 2
        $streetString = str_replace(array_keys($replacements), array_values($replacements), $this->format);
138
139 2
        return $streetString;
140
    }
141
}
142