Completed
Pull Request — master (#62)
by Adam
04:18
created

Address::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 2
1
<?php
2
3
namespace ValueObjects\Geography;
4
5
use ValueObjects\Util\Util;
6
use ValueObjects\ValueObjectInterface;
7
use ValueObjects\StringLiteral\StringLiteral;
8
9
class Address implements ValueObjectInterface
10
{
11
    /**
12
     * Name of the addressee (natural person or company)
13
     *
14
     * @var String
15
     */
16
    protected $name;
17
18
    /** @var Street */
19
    protected $street;
20
21
    /**
22
     * District/City area
23
     *
24
     * @var String
25
     */
26
    protected $district;
27
28
    /**
29
     * City/Town/Village
30
     *
31
     * @var String
32
     */
33
    protected $city;
34
35
    /**
36
     * Region/County/State
37
     *
38
     * @var String
39
     */
40
    protected $region;
41
42
    /**
43
     * Postal code/P.O. Box/ZIP code
44
     *
45
     * @var String
46
     */
47
    protected $postalCode;
48
49
    /** @var Country */
50
    protected $country;
51
52
    /**
53
     * Returns a new Address from native PHP arguments
54
     *
55
     * @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...
56
     * @param string $street_name
0 ignored issues
show
Bug introduced by
There is no parameter named $street_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...
57
     * @param string $street_number
0 ignored issues
show
Bug introduced by
There is no parameter named $street_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...
58
     * @param string $district
0 ignored issues
show
Bug introduced by
There is no parameter named $district. 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...
59
     * @param string $city
0 ignored issues
show
Bug introduced by
There is no parameter named $city. 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...
60
     * @param string $region
0 ignored issues
show
Bug introduced by
There is no parameter named $region. 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...
61
     * @param string $postal_code
0 ignored issues
show
Bug introduced by
There is no parameter named $postal_code. 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...
62
     * @param string $country_code
0 ignored issues
show
Bug introduced by
There is no parameter named $country_code. 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...
63
     *
64
     * @return self
65
     * @throws \BadMethodCallException
66
     */
67 2
    public static function fromNative()
68
    {
69 2
        $args = \func_get_args();
70
71 2
        if (\count($args) != 8) {
72 1
            throw new \BadMethodCallException('You must provide exactly 8 arguments: 1) addressee name, 2) street name, 3) street number, 4) district, 5) city, 6) region, 7) postal code, 8) country code.');
73
        }
74
75 1
        $name = new StringLiteral($args[0]);
76 1
        $street = new Street(new StringLiteral($args[1]), new StringLiteral($args[2]));
77 1
        $district = new StringLiteral($args[3]);
78 1
        $city = new StringLiteral($args[4]);
79 1
        $region = new StringLiteral($args[5]);
80 1
        $postalCode = new StringLiteral($args[6]);
81 1
        $country = Country::fromNative($args[7]);
82
83 1
        return new self($name, $street, $district, $city, $region, $postalCode, $country);
84
    }
85
86
    /**
87
     * Returns a new Address object
88
     *
89
     * @param String  $name
90
     * @param Street  $street
91
     * @param String  $district
92
     * @param String  $city
93
     * @param String  $region
94
     * @param String  $postalCode
95
     * @param Country $country
96
     */
97 11
    public function __construct(StringLiteral $name, Street $street, StringLiteral $district, StringLiteral $city, StringLiteral $region, StringLiteral $postalCode, Country $country)
98
    {
99 11
        $this->name = $name;
0 ignored issues
show
Documentation Bug introduced by
It seems like $name of type object<ValueObjects\StringLiteral\StringLiteral> is incompatible with the declared type string of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
100 11
        $this->street = $street;
101 11
        $this->district = $district;
0 ignored issues
show
Documentation Bug introduced by
It seems like $district of type object<ValueObjects\StringLiteral\StringLiteral> is incompatible with the declared type string of property $district.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
102 11
        $this->city = $city;
0 ignored issues
show
Documentation Bug introduced by
It seems like $city of type object<ValueObjects\StringLiteral\StringLiteral> is incompatible with the declared type string of property $city.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
103 11
        $this->region = $region;
0 ignored issues
show
Documentation Bug introduced by
It seems like $region of type object<ValueObjects\StringLiteral\StringLiteral> is incompatible with the declared type string of property $region.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
104 11
        $this->postalCode = $postalCode;
0 ignored issues
show
Documentation Bug introduced by
It seems like $postalCode of type object<ValueObjects\StringLiteral\StringLiteral> is incompatible with the declared type string of property $postalCode.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105 11
        $this->country = $country;
106 11
    }
107
108
    /**
109
     * Tells whether two Address are equal
110
     *
111
     * @param  ValueObjectInterface $address
112
     *
113
     * @return bool
114
     */
115 2
    public function sameValueAs(ValueObjectInterface $address)
116
    {
117 2
        if (FALSE === Util::classEquals($this, $address)) {
118 1
            return FALSE;
119
        }
120
121 2
        return $this->getName()->sameValueAs($address->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...
122 2
        $this->getStreet()->sameValueAs($address->getStreet()) &&
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 getStreet() does only exist in the following implementations of said interface: ValueObjects\Geography\Address.

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...
123 2
        $this->getDistrict()->sameValueAs($address->getDistrict()) &&
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 getDistrict() does only exist in the following implementations of said interface: ValueObjects\Geography\Address.

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...
124 2
        $this->getCity()->sameValueAs($address->getCity()) &&
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 getCity() does only exist in the following implementations of said interface: ValueObjects\Geography\Address.

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...
125 2
        $this->getRegion()->sameValueAs($address->getRegion()) &&
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 getRegion() does only exist in the following implementations of said interface: ValueObjects\Geography\Address.

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...
126 2
        $this->getPostalCode()->sameValueAs($address->getPostalCode()) &&
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 getPostalCode() does only exist in the following implementations of said interface: ValueObjects\Geography\Address.

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...
127 2
        $this->getCountry()->sameValueAs($address->getCountry());
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 getCountry() does only exist in the following implementations of said interface: ValueObjects\Geography\Address.

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...
128
    }
129
130
    /**
131
     * Returns addressee name
132
     *
133
     * @return String
134
     */
135 4
    public function getName()
136
    {
137 4
        return clone $this->name;
138
    }
139
140
    /**
141
     * Returns street
142
     *
143
     * @return Street
144
     */
145 4
    public function getStreet()
146
    {
147 4
        return clone $this->street;
148
    }
149
150
    /**
151
     * Returns district
152
     *
153
     * @return String
154
     */
155 3
    public function getDistrict()
156
    {
157 3
        return clone $this->district;
158
    }
159
160
    /**
161
     * Returns city
162
     *
163
     * @return String
164
     */
165 4
    public function getCity()
166
    {
167 4
        return clone $this->city;
168
    }
169
170
    /**
171
     * Returns region
172
     *
173
     * @return String
174
     */
175 4
    public function getRegion()
176
    {
177 4
        return clone $this->region;
178
    }
179
180
    /**
181
     * Returns postal code
182
     *
183
     * @return String
184
     */
185 4
    public function getPostalCode()
186
    {
187 4
        return clone $this->postalCode;
188
    }
189
190
    /**
191
     * Returns country
192
     *
193
     * @return Country
194
     */
195 4
    public function getCountry()
196
    {
197 4
        return clone $this->country;
198
    }
199
200
    /**
201
     * Returns a string representation of the Address in US standard format.
202
     *
203
     * @return string
204
     */
205 1
    public function __toString()
206
    {
207
        $format = <<<ADDR
208
%s
209
%s
210
%s %s %s
211 1
%s
212
ADDR;
213
214 1
        $addressString = \sprintf($format, $this->getName(), $this->getStreet(), $this->getCity(), $this->getRegion(), $this->getPostalCode(), $this->getCountry());
215
216 1
        return $addressString;
217
    }
218
219
    function jsonSerialize()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for jsonSerialize.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
220
    {
221
        return [
222
            'parts'     => [
223
                'name'       => $this->getName(),
224
                'street'     => $this->getStreet(),
225
                'city'       => $this->getCity(),
226
                'region'     => $this->getRegion(),
227
                'postalCode' => $this->getPostalCode(),
228
                'country'    => $this->getCountry(),
229
            ],
230
            'formatted' => (string)$this,
231
        ];
232
    }
233
234
235
}
236