Completed
Push — master ( 8a340c...157d96 )
by Lars
03:22
created

Kint_Parsers_Timestamp::_parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5.667

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
ccs 3
cts 9
cp 0.3333
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 5.667
1
<?php
2
3
namespace kint\parsers\custom;
4
5
use kint\inc\KintParser;
6
7
/**
8
 * Class Kint_Parsers_Timestamp
9
 */
10
class Kint_Parsers_Timestamp extends KintParser
11
{
12
  /**
13
   * @param $variable
14
   *
15
   * @return bool
16
   */
17 1
  private static function _fits(&$variable)
18
  {
19 View Code Duplication
    if (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20 1
        is_object($variable)
21
        ||
22 1
        is_array($variable)
23 1
        ||
24
        (
25
            (string)$variable !== $variable
26 1
            &&
27
            (int)$variable !== $variable
28 1
        )
29 1
    ) {
30 1
      return false;
31
    }
32
33 1
    $len = strlen((int)$variable);
34
35
    return
36
        (
37
            $len === 9
38 1
            ||
39
            $len === 10 # a little naive
40 1
            ||
41
            (
42
                $len === 13
43 1
                &&
44
                substr($variable, -3) === '000'
45
            ) # also handles javascript micro timestamps
46 1
        )
47 1
        &&
48 1
        (string)(int)$variable == $variable;
49
  }
50
51
  /**
52
   * @param mixed $variable
53
   *
54
   * @return bool
55
   */
56 1
  protected function _parse(&$variable)
57
  {
58 1
    if (!self::_fits($variable)) {
59 1
      return false;
60
    }
61
62
    if (strlen($variable) === 13) {
63
      $variable = substr($variable, 0, -3);
64
    }
65
66
    $this->type = 'timestamp';
67
    # avoid dreaded "Timezone must be set" error
68
    /** @noinspection PhpUsageOfSilenceOperatorInspection */
69
    $this->value = @date('Y-m-d H:i:s', $variable);
70
71
    return true;
72
  }
73
}
74