Completed
Push — master ( eb7e99...da98d9 )
by Lars
10:24 queued 07:42
created

Kint_Parsers_Timestamp   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 20.31 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 75%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 1
dl 13
loc 64
ccs 15
cts 20
cp 0.75
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
D _fits() 13 33 9
A _parse() 0 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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