Passed
Push — master ( 7fe763...baf245 )
by Iman
02:20
created

CacheNormalizer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 24
ccs 8
cts 10
cp 0.8
rs 10
c 0
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B normalize() 0 16 7
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils\Normalizers;
4
5
use Imanghafoori\Widgets\Utils\NormalizerContract;
6
7
class CacheNormalizer implements NormalizerContract
8
{
9
    /**
10
     * ّFigures out how long the cache life time should be.
11
     *
12
     * @param object $widget
13
     * @return void
14
     */
15 16
    public function normalize($widget): void
16
    {
17 16
        if (! property_exists($widget, 'cacheLifeTime')) {
18 13
            $M = (int) (config('widgetize.default_cache_lifetime', 0));
19 13
            $widget->cacheLifeTime = new \DateInterval('PT'.$M.'M');
20
        }
21
22 16
        if(is_object($widget->cacheLifeTime) or $widget->cacheLifeTime === 0){
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...
23 14
            return ;
24
        }
25
26 2
        if ($widget->cacheLifeTime === 'forever' || $widget->cacheLifeTime < 0) {
27
            // 2 weeks which is long enough !
28 2
            $widget->cacheLifeTime = new \DateInterval('P2W');
29
        } elseif (is_numeric($widget->cacheLifeTime)) {
30
            $widget->cacheLifeTime = new \DateInterval('PT'.(string) $widget->cacheLifeTime.'M');
31
        }
32 2
    }
33
}
34