Test Failed
Pull Request — master (#96)
by Jean-Christophe
13:01
created

DAOPreparedQueryById::execute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 16
rs 9.8333
cc 4
nc 3
nop 2
1
<?php
2
3
namespace Ubiquity\orm\core\prepared;
4
5
use Ubiquity\db\SqlUtils;
6
use Ubiquity\events\DAOEvents;
7
use Ubiquity\events\EventsManager;
8
use Ubiquity\orm\DAO;
9
use Ubiquity\orm\OrmUtils;
10
11
/**
12
 * Ubiquity\orm\core\prepared$DAOPreparedQueryOne
13
 * This class is part of Ubiquity
14
 *
15
 * @author jcheron <[email protected]>
16
 * @version 1.0.0
17
 *
18
 */
19
class DAOPreparedQueryById extends DAOPreparedQuery {
20
21
	public function __construct($className, $included = false) {
22
		parent::__construct ( $className, '', $included );
23
	}
24
25
	protected function prepare() {
26
		parent::prepare ();
27
		$keys = OrmUtils::getKeyFields ( $this->className );
28
		$this->conditionParser->addKeyValues ( \array_fill ( 0, \count ( $keys ), '' ), $this->className );
1 ignored issue
show
Bug introduced by
It seems like $keys can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

28
		$this->conditionParser->addKeyValues ( \array_fill ( 0, \count ( /** @scrutinizer ignore-type */ $keys ), '' ), $this->className );
Loading history...
29
		$this->conditionParser->limitOne ();
30
	}
31
32
	public function execute($params = [], $useCache = false) {
33
		$cp = $this->conditionParser;
34
		$cp->setKeyValues ( $params );
35
		$query = $this->db->prepareAndExecute ( $this->tableName, SqlUtils::checkWhere ( $cp->getCondition () ), $this->fieldList, $cp->getParams (), $useCache );
36
		if ($query && \sizeof ( $query ) > 0) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $query 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...
37
			$oneToManyQueries = [ ];
38
			$manyToOneQueries = [ ];
39
			$manyToManyParsers = [ ];
40
			$className = $this->className;
41
			$object = DAO::_loadObjectFromRow ( \current ( $query ), $className, $this->invertedJoinColumns, $manyToOneQueries, $this->oneToManyFields, $this->manyToManyFields, $oneToManyQueries, $manyToManyParsers, $this->accessors, $this->transformers );
42
			if ($this->hasIncluded) {
43
				DAO::_affectsRelationObjects ( $className, $this->firstPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, [ $object ], $this->included, $useCache );
44
			}
45
			EventsManager::trigger ( DAOEvents::GET_ONE, $object, $className );
46
		}
47
		return $object;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $object does not seem to be defined for all execution paths leading up to this point.
Loading history...
48
	}
49
}
50
51