DateColumn   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 38
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A cast() 0 17 3
A validate() 0 8 4
1
<?php
2
3
namespace FForattini\Btrieve\Column;
4
5
use Datetime;
6
use Exception;
7
use FForattini\Btrieve\Hex;
8
9
class DateColumn extends ColumnRule implements ColumnInterface
10
{
11
    const DEFAULT_LENGTH = 8;
12
13
    public function __construct($title, $length = self::DEFAULT_LENGTH)
14
    {
15
        $this->setTitle($title);
16
        $this->setType(self::TYPE_DATE);
17
        $this->setLength($length);
18
    }
19
20
    public static function cast($content)
21
    {
22
        if (self::validate($content)) {
23
            return;
24
        }
25
26
        $content = Hex::toStr($content);
27
28
        try {
29
            $content = DateTime::createFromFormat('Ymd', $content);
30
            $content->setTime(0, 0, 0);
31
        } catch (Exception $e) {
32
            $content = null;
33
        }
34
35
        return $content;
36
    }
37
38
    public static function validate($content)
39
    {
40
        if (intval($content) == 0 or strlen($content) < 8 or $content == '2020202020202020') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
41
            return false;
42
        }
43
44
        return true;
45
    }
46
}
47