Completed
Push — master ( e0ab14...6e0d04 )
by Wanderson
02:19
created

UserDAO::fetchLast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace User;
4
5
use Win\DesignPattern\DAO;
6
7
/**
8
 * User DAO
9
 */
10
class UserDAO extends DAO implements UserDAOInterface {
11
12
	const TABLE = 'user';
13
	const ALIAS = 'Usuário';
14
15
	/** @var User */
16
	protected $obj;
17
18
	/**
19
	 * @param array $row
20
	 * @return User
21
	 */
22
	protected function mapObject($row) {
23
		$obj = new User();
24
		if ($row) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $row of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
25
			$obj->setId($row['id']);
26
			$obj->setEmail($row['email']);
27
			$obj->setPass($row['pass']);
28
		}
29
		return $obj;
30
	}
31
32
	/**
33
	 * @param User $obj
34
	 * @return mixed[]
35
	 */
36
	protected function mapRow($obj) {
37
		$row['id'] = $obj->getId();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$row was never initialized. Although not strictly required by PHP, it is generally a good practice to add $row = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
38
		$row['email'] = $obj->getEmail();
39
		$row['pass'] = $obj->getPass();
40
		return $row;
41
	}
42
43
	public function fetchLast() {
44
		return $this->fetch([], 'ORDER BY id DESC');
45
	}
46
47
}
48