DateTimeTrait::fromString()   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 1
crap 1
1
<?php
2
3
namespace BestServedCold\PhalueObjects\DateTime;
4
5
use BestServedCold\PhalueObjects\Mathematical\Operator\ArithmeticTrait;
6
use BestServedCold\PhalueObjects\Mathematical\Operator\ComparisonTrait;
7
use BestServedCold\PhalueObjects\Mathematical\Operator\TypeTrait;
8
use BestServedCold\PhalueObjects\Contract\DateTime as DateTimeInterface;
9
10
/**
11
 * Class DateTimeTrait
12
 *
13
 * @package BestServedCold\PhalueObjects\DateTime
14
 * @author    Adam Lewis <[email protected]>
15
 * @copyright Copyright (c) 2015 Best Served Cold Media Limited
16
 * @license   http://http://opensource.org/licenses/GPL-3.0 GPL License
17
 * @link      http://bestservedcold.com
18
 * @since     0.0.1-alpha
19
 * @version   0.0.2-alpha
20
 */
21
trait DateTimeTrait
22
{
23
    use ArithmeticTrait, ComparisonTrait, TypeTrait;
24
25
    /**
26
     * @var \DateTime
27
     */
28
    protected $native;
29
30
    /**
31
     * @var int
32
     */
33
    protected $timestamp;
34
35
    /**
36
     * @param $string
37
     * @return mixed
38
     */
39 16
    public static function fromString($string)
40
    {
41 16
        return static::fromNative(new \DateTime($string));
42
    }
43
44
    /**
45
     * @param $timestamp
46
     * @return mixed
47
     */
48 1
    public static function fromTimestamp($timestamp)
49
    {
50 1
        return static::fromNative(static::getNowDateTime()->setTimestamp($timestamp));
51
    }
52
53
    /**
54
     * @param string $format
55
     * @return int
56
     */
57 33
    public static function getNowDateTimeFormat($format)
58
    {
59 33
        return (int) static::getNowDateTime()->format($format);
60
    }
61
62
    /**
63
     * @param string $string
0 ignored issues
show
Documentation introduced by
Should the type for parameter $string not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
64
     * @return \DateTime
65
     */
66 44
    public static function getDateTime($string = null)
67
    {
68 44
        return new \DateTime($string);
69
    }
70
71
    /**
72
     * @return \DateTime
73
     */
74 44
    public static function getNowDateTime()
75 2
    {
76 44
        return static::getDateTime('now');
77
    }
78
79
    /**
80
     * @return \DateTime
81
     */
82 8
    public function getNative()
83
    {
84 8
        return $this->native;
85
    }
86
87
    /**
88
     * @return int
89
     */
90 11
    public function getTimestamp()
91
    {
92 11
        return $this->timestamp;
93
    }
94
95
    /**
96
     * @param DateTimeInterface $object
97
     * @return bool
98
     */
99 1
    public function isBefore(DateTimeInterface $object)
100
    {
101 1
        return $this->isLessThan($object);
0 ignored issues
show
Documentation introduced by
$object is of type object<BestServedCold\Ph...ects\Contract\DateTime>, but the function expects a object<BestServedCold\Ph...s\Contract\ValueObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
    }
103
104
    /**
105
     * @param DateTimeInterface $object
106
     * @return bool
107
     */
108 1
    public function isAfter(DateTimeInterface $object)
109
    {
110 1
        return $this->isGreaterThan($object);
0 ignored issues
show
Documentation introduced by
$object is of type object<BestServedCold\Ph...ects\Contract\DateTime>, but the function expects a object<BestServedCold\Ph...s\Contract\ValueObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
    }
112
113
    /**
114
     * @param DateTimeInterface $object
115
     * @return bool
116
     */
117 1
    public function isAfterOrIs(DateTimeInterface $object)
118
    {
119 1
        return $this->isLessThanOrEqualTo($object);
0 ignored issues
show
Documentation introduced by
$object is of type object<BestServedCold\Ph...ects\Contract\DateTime>, but the function expects a object<BestServedCold\Ph...s\Contract\ValueObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
    }
121
122
    /**
123
     * @param DateTimeInterface $object
124
     * @return bool
125
     */
126 1
    public function isBeforeOrIs(DateTimeInterface $object)
127
    {
128 1
        return $this->isGreaterThanOrEqualTo($object);
0 ignored issues
show
Documentation introduced by
$object is of type object<BestServedCold\Ph...ects\Contract\DateTime>, but the function expects a object<BestServedCold\Ph...s\Contract\ValueObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
    }
130
}
131