Date::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 2
b 0
f 0
cc 3
eloc 8
nc 2
nop 3
crap 3
1
<?php
2
3
namespace ValueObjects\DateTime;
4
5
use ValueObjects\DateTime\Exception\InvalidDateException;
6
use ValueObjects\Util\Util;
7
use ValueObjects\ValueObjectInterface;
8
9
class Date implements ValueObjectInterface
10
{
11
    /** @var Year */
12
    protected $year;
13
14
    /** @var Month */
15
    protected $month;
16
17
    /** @var MonthDay */
18
    protected $day;
19
20
    /**
21
     * Returns a new Date from native year, month and day values
22
     *
23
     * @param  int    $year
0 ignored issues
show
Bug introduced by
There is no parameter named $year. 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...
24
     * @param  string $month
0 ignored issues
show
Bug introduced by
There is no parameter named $month. 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...
25
     * @param  int    $day
0 ignored issues
show
Bug introduced by
There is no parameter named $day. 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...
26
     * @return Date
27
     */
28 3
    public static function fromNative()
29
    {
30 3
        $args = func_get_args();
31
32 3
        $year  = new Year($args[0]);
33 3
        $month = Month::fromNative($args[1]);
34 3
        $day   = new MonthDay($args[2]);
35
36 3
        return new static($year, $month, $day);
37
    }
38
39
    /**
40
     * Returns a new Date from a native PHP \DateTime
41
     *
42
     * @param  \DateTime $date
43
     * @return Date
44
     */
45 3
    public static function fromNativeDateTime(\DateTime $date)
46
    {
47 3
        $year  = \intval($date->format('Y'));
48 3
        $month = Month::fromNativeDateTime($date);
49 3
        $day   = \intval($date->format('d'));
50
51 3
        return new static(new Year($year), $month, new MonthDay($day));
52
    }
53
54
    /**
55
     * Returns current Date
56
     *
57
     * @return Date
58
     */
59 3
    public static function now()
60
    {
61 3
        $date = new static(Year::now(), Month::now(), MonthDay::now());
62
63 3
        return $date;
64
    }
65
66
    /**
67
     * Create a new Date
68
     *
69
     * @param  Year                 $year
70
     * @param  Month                $month
71
     * @param  MonthDay             $day
72
     * @throws InvalidDateException
73
     */
74 28
    public function __construct(Year $year, Month $month, MonthDay $day)
75
    {
76 28
        \DateTime::createFromFormat('Y-F-j', \sprintf('%d-%s-%d', $year->toNative(), $month, $day->toNative()));
77 28
        $nativeDateErrors = \DateTime::getLastErrors();
78
79 28
        if ($nativeDateErrors['warning_count'] > 0 || $nativeDateErrors['error_count'] > 0) {
80 1
            throw new InvalidDateException($year->toNative(), $month->toNative(), $day->toNative());
81
        }
82
83 27
        $this->year  = $year;
84 27
        $this->month = $month;
85 27
        $this->day   = $day;
86 27
    }
87
88
    /**
89
     * Tells whether two Date are equal by comparing their values
90
     *
91
     * @param  ValueObjectInterface $date
92
     * @return bool
93
     */
94 12
    public function sameValueAs(ValueObjectInterface $date)
95
    {
96 12
        if (false === Util::classEquals($this, $date)) {
97 1
            return false;
98
        }
99
100 12
        return $this->getYear()->sameValueAs($date->getYear()) &&
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 getYear() does only exist in the following implementations of said interface: ValueObjects\DateTime\Date.

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...
101 12
               $this->getMonth()->sameValueAs($date->getMonth()) &&
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 getMonth() does only exist in the following implementations of said interface: ValueObjects\DateTime\Date.

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...
102 12
               $this->getDay()->sameValueAs($date->getDay());
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 getDay() does only exist in the following implementations of said interface: ValueObjects\DateTime\Date.

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...
103
    }
104
105
    /**
106
     * Get year
107
     *
108
     * @return Year
109
     */
110 22
    public function getYear()
111
    {
112 22
        return clone $this->year;
113
    }
114
115
    /**
116
     * Get month
117
     *
118
     * @return Month
119
     */
120 22
    public function getMonth()
121
    {
122 22
        return $this->month;
123
    }
124
125
    /**
126
     * Get day
127
     *
128
     * @return MonthDay
129
     */
130 22
    public function getDay()
131
    {
132 22
        return clone $this->day;
133
    }
134
135
    /**
136
     * Returns a native PHP \DateTime version of the current Date
137
     *
138
     * @return \DateTime
139
     */
140 7
    public function toNativeDateTime()
141
    {
142 7
        $year  = $this->getYear()->toNative();
143 7
        $month = $this->getMonth()->getNumericValue();
144 7
        $day   = $this->getDay()->toNative();
145
146 7
        $date = new \DateTime();
147 7
        $date->setDate($year, $month, $day);
148 7
        $date->setTime(0, 0, 0);
149
150 7
        return $date;
151
    }
152
153
    /**
154
     * Returns date as string in format Y-n-j
155
     *
156
     * @return string
157
     */
158 6
    public function __toString()
159
    {
160 6
        return $this->toNativeDateTime()->format('Y-n-j');
161
    }
162
}
163