Completed
Push — master ( a5e931...d15f5b )
by Gilmar
28:45 queued 03:51
created

DateTimeTrait::dateFormat()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 8.7972
cc 4
eloc 13
nc 5
nop 1
crap 4
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 <http://www.g1mr.com/>.
13
 */
14
15
namespace Gpupo\NetshoesSdk\Traits;
16
17
use DateTime;
18
use DateTimeZone;
19
20
trait DateTimeTrait
21
{
22 24
    protected function dateFormat($string)
23
    {
24 24
        $zone = 'America/Sao_Paulo';
25
26 24
        if (intval($string) === $string) {
27 2
            $df = date_default_timezone_get();
28 2
            if ($df !== $zone) {
29 2
                date_default_timezone_set($zone);
30
            }
31 2
            $date =  date('c', $string / 1000);
32
33 2
            if ($df !== $zone) {
34 2
                date_default_timezone_set($df);
35
            }
36
37 2
            return $date;
38
        }
39
40 22
        $timezone = new DateTimeZone($zone);
41 22
        $datetime = new DateTime($string, $timezone);
42
43 22
        return $datetime->format('c');
44
    }
45
46 9
    protected function dateGet($key)
47
    {
48 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...
49
50 9
        if (!empty($value)) {
51 9
            $string = $this->dateFormat($value);
52
53 9
            return str_replace('-03:00', '.000-03:00', $string);
54
        }
55 1
    }
56
}
57