DateWriteEsSanitizer::read()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 15.7829

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 9
cts 16
cp 0.5625
rs 8.0555
c 0
b 0
f 0
cc 9
nc 9
nop 2
crap 15.7829
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL-3.0-only, proprietary` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL-3.0-only, proprietary
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link https://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel\Sanitizers;
14
15
use DateTime;
16
use Maslosoft\Mangan\Sanitizers\DateSanitizer;
17
use MongoDate;
18
19
/**
20
 * UnixDateSanitizer
21
 *
22
 * This sanitizer allow storing date in elasticsearch in string format,
23
 * while reading it as MongoDate object.
24
 *
25
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
26
 */
27
class DateWriteEsSanitizer extends DateSanitizer
28
{
29
30
	const ISODate = 'Y-m-d*H:i:s*';
31
32 4
	public function read($model, $dbValue)
33
	{
34
		// Don't touch
35 4
		if ($dbValue instanceof MongoDate)
36
		{
37
			return $dbValue;
38
		}
39
		// Assume current time
40 4
		if ((int) $dbValue === 0 || empty($dbValue))
41
		{
42
			$dbValue = time();
43
		}
44
		// Assume timestamp
45 4
		if (is_int($dbValue) || (is_string($dbValue) && preg_match('~^\d+$~', $dbValue)))
46
		{
47
			return new MongoDate((int) $dbValue);
48
		}
49 4
		elseif (is_string($dbValue))
50
		{
51
			// Assume ISO date
52 4
			$dt = DateTime::createFromFormat(DateTime::ISO8601, $dbValue);
53 4
			if (empty($dt))
54
			{
55
				return new MongoDate(time());
56
			}
57 4
			$time = $dt->format('U');
58 4
			return new MongoDate($time);
59
		}
60
61
		// Create from date time string
62
		$dt = new DateTime($dbValue);
63
		$time = $dt->format('U');
64
		return new MongoDate($time);
65
	}
66
67 5
	public function write($model, $dbValue)
68
	{
69 5
		if ($dbValue instanceof MongoDate)
70
		{
71 3
			$time = $dbValue->sec;
72
		}
73 2
		elseif (!empty($time))
0 ignored issues
show
Bug introduced by
The variable $time seems only to be defined at a later point. As such the call to empty() seems to always evaluate to true.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
74
		{
75
			$time = $dbValue;
76
		}
77
		else
78
		{
79 2
			$time = time();
80
		}
81 5
		if (is_int($time))
82
		{
83 4
			$dt = DateTime::createFromFormat('U', $time);
0 ignored issues
show
Unused Code introduced by
$dt is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84
		}
85
		else
86
		{
87 1
			$dt = DateTime::createFromFormat('c', $time);
0 ignored issues
show
Unused Code introduced by
$dt is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88
		}
89 5
		return date('c', (int) (new MongoDate((int) $time))->sec);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return date('c', (int) (...te((int) $time))->sec); (string) is incompatible with the return type of the parent method Maslosoft\Mangan\Sanitizers\DateSanitizer::write of type MongoDate.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
90
	}
91
92
}
93