Passed
Pull Request — master (#526)
by Michael
06:45
created

GeoLocation   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 30.68 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 27
loc 88
rs 10
c 0
b 0
f 0
ccs 0
cts 48
cp 0
wmc 12
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setAddress() 0 3 1
A getData() 0 3 1
A setData() 0 3 1
A save() 0 24 4
A getAddress() 0 3 1
A getByAddress() 0 18 3
A getCreation() 0 3 1

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
/**
4
 * Geolocation data object
5
 *
6
 * This is a cache.
7
 */
8
class GeoLocation extends DataObject
9
{
10
	private $address;
11
	private $data;
12
	private $creation;
13
14
	/**
15
	 * @param string $address
16
	 * @param PdoDatabase $database
17
	 * @param bool $forUpdate
18
	 * @return GeoLocation
19
	 */
20
	public static function getByAddress($address, PdoDatabase $database, $forUpdate = false)
21
	{
22
		$lockMode = $forUpdate ? ' FOR UPDATE' : '';
23
		$sql = "SELECT * FROM geolocation WHERE address = :id LIMIT 1" . $lockMode;
24
25
		$statement = $database->prepare($sql);
26
		$statement->bindValue(":id", $address);
27
28
		$statement->execute();
29
30
		$resultObject = $statement->fetchObject(get_called_class());
31
32
		if ($resultObject != false) {
33
			$resultObject->isNew = false;
34
			$resultObject->setDatabase($database);
35
		}
36
37
		return $resultObject;
38
	}
39
40
	public function save()
41
	{
42
		if ($this->isNew) {
43
// insert
44
			$statement = $this->dbObject->prepare("INSERT INTO `geolocation` (address, data) VALUES (:address, :data) ON DUPLICATE KEY UPDATE address = address;");
45
			$statement->bindValue(":address", $this->address);
46
			$statement->bindValue(":data", $this->data);
47
			if ($statement->execute()) {
48
				$this->isNew = false;
49
				$this->id = $this->dbObject->lastInsertId();
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type integer, but $this->dbObject->lastInsertId() is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
50
			}
51
			else {
52
				throw new Exception($statement->errorInfo());
0 ignored issues
show
Bug introduced by
$statement->errorInfo() of type array is incompatible with the type string expected by parameter $message of Exception::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
				throw new Exception(/** @scrutinizer ignore-type */ $statement->errorInfo());
Loading history...
53
			}
54
		}
55
		else {
56
// update
57
			$statement = $this->dbObject->prepare("UPDATE `geolocation` SET address = :address, data = :data WHERE id = :id;");
58
			$statement->bindValue(":address", $this->address);
59
			$statement->bindValue(":id", $this->id);
60
			$statement->bindValue(":data", $this->data);
61
62
			if (!$statement->execute()) {
63
				throw new Exception($statement->errorInfo());
64
			}
65
		}
66
	}
67
68
	public function getAddress()
69
	{
70
		return $this->address;
71
	}
72
73
	/**
74
	 * @param string $address
75
	 */
76
	public function setAddress($address)
77
	{
78
		$this->address = $address;
79
	}
80
81
	public function getData()
82
	{
83
		return unserialize($this->data);
84
	}
85
86
	public function setData($data)
87
	{
88
		$this->data = serialize($data);
89
	}
90
91
	public function getCreation()
92
	{
93
		return $this->creation;
94
	}
95
}
96