Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

EntitySortByClause   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
dl 0
loc 74
ccs 24
cts 28
cp 0.8571
rs 10
c 0
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
D prepare() 0 44 9
1
<?php
2
3
namespace Elgg\Database\Clauses;
4
5
use Elgg\Database\QueryBuilder;
6
use ElggEntity;
7
8
/**
9
 * Extends QueryBuilder with clauses necesary to sort entity lists by entity properties
10
 */
11
class EntitySortByClause extends OrderByClause {
12
13
	/**
14
	 * @var string[]
15
	 */
16
	public $property;
17
18
	/**
19
	 * @var string
20
	 */
21
	public $direction;
22
23
	/**
24
	 * @var bool
25
	 */
26
	public $signed;
27
28
	/**
29
	 * @var string
30
	 */
31
	public $join_type;
32
33
	/**
34
	 * @var string
35
	 */
36
	public $property_type;
37
38
	/**
39
	 * {@inheritdoc}
40
	 */
41 27
	public function prepare(QueryBuilder $qb, $table_alias = null) {
42
43 27
		if (!isset($this->property_type)) {
44 4
			if (in_array($this->property, ElggEntity::$primary_attr_names)) {
45 2
				$this->property_type = 'attribute';
46
			} else {
47 2
				$this->property_type = 'metadata';
48
			}
49
		}
50
51 27
		switch ($this->property_type) {
52
			case 'metadata' :
53 8
				$md_alias = $qb->joinMetadataTable($table_alias, 'guid', $this->property, $this->join_type);
54 8
				$column = "$md_alias.value";
55 8
				break;
56
57
			case 'attribute' :
58 14
				if (!in_array($this->property, ElggEntity::$primary_attr_names)) {
59 1
					throw new \InvalidParameterException("'$this->property' is not a valid entity attribute");
60
				}
61 13
				$column = "$table_alias.$this->property";
62 13
				break;
63
64
			case 'private_setting' :
65 2
				$ps_alias = $qb->joinPrivateSettingsTable($table_alias, 'guid', $this->property, $this->join_type);
66 2
				$column = "$ps_alias.value";
67 2
				break;
68
69
			case 'annotation' :
70 2
				$an_alias = $qb->joinAnnotationTable($table_alias, 'guid', $this->property, $this->join_type);
71 2
				$column = "$an_alias.value";
72 2
				break;
73
74
			default :
75 1
				throw new \InvalidParameterException("'$this->property_type' is not a valid entity property type");
76
		}
77
78 25
		if ($this->signed) {
79 9
			$column = "CAST($column AS SIGNED)";
80
		}
81
82 25
		$this->expr = $column;
83
84 25
		return parent::prepare($qb, $table_alias);
1 ignored issue
show
Bug introduced by Ismayil Khayredinov
Are you sure the usage of parent::prepare($qb, $table_alias) targeting Elgg\Database\Clauses\OrderByClause::prepare() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
	}
86
}
87