TimeZone::fromDefault()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ValueObjects\DateTime;
4
5
use ValueObjects\DateTime\Exception\InvalidTimeZoneException;
6
use ValueObjects\StringLiteral\StringLiteral;
7
use ValueObjects\Util\Util;
8
use ValueObjects\ValueObjectInterface;
9
10
class TimeZone implements ValueObjectInterface
11
{
12
    /** @var StringLiteral */
13
    protected $name;
14
15
    /**
16
     * Returns a new Time object from native timezone name
17
     *
18
     * @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...
19
     * @return self
20
     */
21 4
    public static function fromNative()
22
    {
23 4
        $args = func_get_args();
24
25 4
        $name = new StringLiteral($args[0]);
26
27 4
        return new static($name);
28
    }
29
30
    /**
31
     * Returns a new Time from a native PHP \DateTime
32
     *
33
     * @param  \DateTimeZone $timezone
34
     * @return self
35
     */
36 2
    public static function fromNativeDateTimeZone(\DateTimeZone $timezone)
37
    {
38 2
        return static::fromNative($timezone->getName());
39
    }
40
41
    /**
42
     * Returns default TimeZone
43
     *
44
     * @return self
45
     */
46 2
    public static function fromDefault()
47
    {
48 2
        return new static(new StringLiteral(date_default_timezone_get()));
49
    }
50
51
    /**
52
     * Returns a new TimeZone object
53
     *
54
     * @param StringLiteral $name
55
     * @throws InvalidTimeZoneException
56
     */
57 17
    public function __construct(StringLiteral $name)
58
    {
59 17
        if (!in_array($name->toNative(), timezone_identifiers_list())) {
60 1
            throw new InvalidTimeZoneException($name);
61
        }
62
63 16
        $this->name = $name;
64 16
    }
65
66
    /**
67
     * Returns a native PHP \DateTimeZone version of the current TimeZone.
68
     *
69
     * @return \DateTimeZone
70
     */
71 3
    public function toNativeDateTimeZone()
72
    {
73 3
        return new \DateTimeZone($this->getName()->toNative());
74
    }
75
76
    /**
77
     * Tells whether two DateTimeZone are equal by comparing their names
78
     *
79
     * @param  ValueObjectInterface $timezone
80
     * @return bool
81
     */
82 7
    public function sameValueAs(ValueObjectInterface $timezone)
83
    {
84 7
        if (false === Util::classEquals($this, $timezone)) {
85 1
            return false;
86
        }
87
88 7
        return $this->getName()->sameValueAs($timezone->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...
89
    }
90
91
    /**
92
     * Returns timezone name
93
     *
94
     * @return StringLiteral
95
     */
96 15
    public function getName()
97
    {
98 15
        return clone $this->name;
99
    }
100
101
    /**
102
     * Returns timezone name as string
103
     *
104
     * @return string
105
     */
106 4
    public function __toString()
107
    {
108 4
        return $this->getName()->__toString();
109
    }
110
}
111