Completed
Push — master ( 5adf1c...8a340c )
by Lars
03:32
created

Kint_Parsers_Timestamp::_fits()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 33
Code Lines 21

Duplication

Lines 13
Ratio 39.39 %

Code Coverage

Tests 12
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 33
ccs 12
cts 12
cp 1
rs 4.909
cc 9
eloc 21
nc 7
nop 1
crap 9
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
        ||
24
        (
25 1
            (string)$variable !== $variable
26
            &&
27 1
            (int)$variable !== $variable
28
        )
29
    ) {
30 1
      return false;
31
    }
32
33 1
    $len = strlen((int)$variable);
34
35
    return
36
        (
37 1
            $len === 9
38
            ||
39 1
            $len === 10 # a little naive
40
            ||
41
            (
42 1
                $len === 13
43
                &&
44 1
                substr($variable, -3) === '000'
45
            ) # also handles javascript micro timestamps
46
        )
47
        &&
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