Passed
Push — master ( 4b500f...63b2b9 )
by Jerome
10:05 queued 13s
created

Elgg/Database/Clauses/EntitySortByClause.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Elgg\Database\Clauses;
4
5
use Elgg\Database\AnnotationsTable;
6
use Elgg\Database\EntityTable;
7
use Elgg\Database\MetadataTable;
8
use Elgg\Database\QueryBuilder;
9
use Elgg\Database\RelationshipsTable;
10
use Elgg\Exceptions\DomainException;
11
12
/**
13
 * Extends QueryBuilder with clauses necessary to sort entity lists by entity properties
14
 */
15
class EntitySortByClause extends OrderByClause {
16
17
	/**
18
	 * @var string
19
	 */
20
	public $property;
21
22
	/**
23
	 * @var bool
24
	 */
25
	public $signed;
26
27
	/**
28
	 * @var string
29
	 */
30
	public $join_type;
31
32
	/**
33
	 * @var string
34
	 */
35
	public $property_type;
36
	
37
	/**
38
	 * @var bool
39
	 */
40
	public $inverse_relationship;
41
	
42
	/**
43
	 * @var int
44
	 */
45
	public $relationship_guid;
46
47
	/**
48
	 * {@inheritdoc}
49
	 *
50
	 * @throws DomainException
51
	 */
52 23
	public function prepare(QueryBuilder $qb, $table_alias = null) {
53
54 23
		if (!isset($this->property_type)) {
55 14
			if (in_array($this->property, \ElggEntity::PRIMARY_ATTR_NAMES)) {
56 3
				$this->property_type = 'attribute';
57
			} else {
58 11
				$this->property_type = 'metadata';
59
			}
60
		}
61
		
62
		// get correct base GUID column
63
		// default assumes the main table is 'entities'
64 23
		$from_column = 'guid';
65 23
		switch ($qb->getTableName()) {
66
			case AnnotationsTable::TABLE_NAME:
67
			case MetadataTable::TABLE_NAME:
68 2
				$from_column = 'entity_guid';
69 2
				break;
70
			case RelationshipsTable::TABLE_NAME:
71 1
				$from_column = 'guid_one';
72 1
				if ((bool) $this->inverse_relationship) {
73
					$from_column = 'guid_two';
74
				}
75 1
				break;
76
		}
77
78 23
		switch ($this->property_type) {
79 23
			case 'metadata':
80 11
				$md_alias = $qb->joinMetadataTable($table_alias, $from_column, $this->property, $this->join_type);
81 11
				$column = "{$md_alias}.value";
82 11
				break;
83
84 12
			case 'attribute':
85 9
				if (!in_array($this->property, \ElggEntity::PRIMARY_ATTR_NAMES)) {
86 1
					throw new DomainException("'{$this->property}' is not a valid entity attribute");
87
				}
88
				
89 8
				if ($qb->getTableName() !== EntityTable::TABLE_NAME) {
90 4
					$e_alias = $qb->joinEntitiesTable($table_alias, $from_column, $this->join_type);
91
				} else {
92 4
					$e_alias = $table_alias;
93
				}
94
				
95 8
				$column = "{$e_alias}.{$this->property}";
96 8
				break;
97
98 3
			case 'annotation':
99 2
				$an_alias = $qb->joinAnnotationTable($table_alias, $from_column, $this->property, $this->join_type);
100 2
				$column = "{$an_alias}.value";
101 2
				break;
102
103 1
			case 'relationship':
104
				if ($qb->getTableName() !== RelationshipsTable::TABLE_NAME) {
105
					$er_alias = $qb->joinRelationshipTable($table_alias, $from_column, $this->property, $this->inverse_relationship, $this->join_type);
106
					if (!empty($this->relationship_guid)) {
107
						$guid_column = $this->inverse_relationship ? 'guid_two' : 'guid_one';
108
						$qb->andWhere($qb->compare("{$er_alias}.{$guid_column}", '=', $this->relationship_guid, ELGG_VALUE_GUID));
1 ignored issue
show
It seems like $qb->compare($er_alias.'...lauses\ELGG_VALUE_GUID) can also be of type null; however, parameter $predicate of Doctrine\DBAL\Query\QueryBuilder::andWhere() does only seem to accept Doctrine\DBAL\Query\Expr...positeExpression|string, 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

108
						$qb->andWhere(/** @scrutinizer ignore-type */ $qb->compare("{$er_alias}.{$guid_column}", '=', $this->relationship_guid, ELGG_VALUE_GUID));
Loading history...
109
					}
110
				} else {
111
					$er_alias = $table_alias;
112
				}
113
				
114
				$column = "{$er_alias}.time_created";
115
				
116
				break;
117
118
			default:
119 1
				elgg_log("'{$this->property_type}' is not a valid entity property type. Sorting ignored.");
120 1
				return null;
121
		}
122
123 21
		if ($this->signed) {
124 8
			$column = "CAST({$column} AS SIGNED)";
125
		}
126
127 21
		$this->expr = $column;
128
129 21
		parent::prepare($qb, $table_alias);
130
	}
131
}
132