Passed
Push — master ( 2ae3d8...1a6d7e )
by Gilmar
24:28
created

DateTimeTrait::dateMove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of gpupo/netshoes-sdk
5
 * Created by Gilmar Pupo <[email protected]>
6
 * For the information of copyright and license you should read the file
7
 * LICENSE which is distributed with this source code.
8
 * Para a informação dos direitos autorais e de licença você deve ler o arquivo
9
 * LICENSE que é distribuído com este código-fonte.
10
 * Para obtener la información de los derechos de autor y la licencia debe leer
11
 * el archivo LICENSE que se distribuye con el código fuente.
12
 * For more information, see <https://www.gpupo.com/>.
13
 */
14
15
namespace Gpupo\NetshoesSdk\Traits;
16
17
use DateTime;
18
use DateTimeZone;
19
use DateInterval;
20
21
trait DateTimeTrait
22
{
23 22
    protected function dateFormat($string)
24
    {
25 22
        $zone = 'America/Sao_Paulo';
26
27 22
        if (intval($string) === $string) {
28 2
            $df = date_default_timezone_get();
29 2
            if ($df !== $zone) {
30 2
                date_default_timezone_set($zone);
31
            }
32 2
            $date = date('c', $string / 1000);
33
34 2
            if ($df !== $zone) {
35 2
                date_default_timezone_set($df);
36
            }
37
38 2
            return $date;
39
        }
40
41 20
        $timezone = new DateTimeZone($zone);
42 20
        $datetime = new DateTime($string, $timezone);
43
44 20
        return $datetime->format('c');
45
    }
46
47 9
    protected function dateGet($key)
48
    {
49 9
        $value = $this->get($key);
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
50
51 9
        if (!empty($value)) {
52 9
            $string = $this->dateFormat($value);
53
54 9
            return str_replace('-03:00', '.000-03:00', $string);
55
        }
56 1
    }
57
58 1
    protected function dateMove($move)
59
    {
60 1
        $date = new DateTime();
61 1
        $date->sub(new DateInterval($move));
62
63 1
        return $date->format('c');
64
    }
65
}
66