Completed
Push — code-coverage ( dff651 )
by Shawn
05:03
created

DateFormat::dateFormat()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.972

Importance

Changes 2
Bugs 2 Features 0
Metric Value
dl 0
loc 22
ccs 7
cts 10
cp 0.7
rs 8.6737
c 2
b 2
f 0
cc 6
eloc 10
nc 5
nop 1
crap 6.972
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sdibble
5
 * Date: 12/12/2016
6
 * Time: 12:11 PM.
7
 */
8
9
namespace SET\Handlers;
10
11
use Carbon\Carbon;
12
13
trait DateFormat
14
{
15 25
    public function dateFormat($date)
16
    {
17 25
        if (preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $date)) {
18 9
            return $date;
19
        }
20
21 19
        if ($date instanceof Carbon or $date instanceof \DateTime) {
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...
22 15
            return $date->format('Y-m-d');
23
        }
24
25
        // sqlite
26 4
        if ($carbonDate = Carbon::createFromFormat('Y-m-d H:i:s', $date)) {
27 3
            return $carbonDate->format('Y-m-d');
28
        }
29
30
        // JPAS
31
        if ($carbonDate = \DateTime::createFromFormat('n/j/Y G:i', $date)) {
32
            return $carbonDate->format('Y-m-d');
33
        }
34
35
        return null;
36
    }
37
}
38