Completed
Push — master ( 416cb1...222de5 )
by Anton
03:44
created

Listview::getItemsBlock()   C

Complexity

Conditions 8
Paths 17

Size

Total Lines 31
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 31
rs 5.3846
cc 8
eloc 11
nc 17
nop 1
1
<?php
2
3
namespace Modules\Entitizer\Utils {
4
5
	use Modules\Entitizer, Utils\Pagination, Utils\View, Ajax, Number, Request, Template, Url;
6
7
	abstract class Listview {
8
9
		protected $index = 0, $parent = null, $items = [];
10
11
		# Process parent block
12
13
		private function processParent(Template\Asset\Block $parent) {
14
15
			# Set parent id
16
17
			$parent->id = $this->parent->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Template\Asset\Block>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
18
19
			# Set create button
20
21
			$parent->block('create')->id = $this->parent->id;
22
23
			# Set edit button
24
25
			if (0 === $this->parent->id) $parent->block('edit')->disable();
26
27
			else $parent->block('edit')->id = $this->parent->id;
28
		}
29
30
		# Get items block
31
32
		private function getItemsBlock(bool $ajax = false) {
33
34
			$items = Template::group();
35
36
			foreach ($this->items['list'] as $item) {
37
38
				$items->add($view = View::get($ajax ? static::$view_ajax_item : static::$view_item));
39
40
				# Set data
41
42
				$view->id = $item['entity']->id;
43
44
				$view->set(static::$naming, $item['entity']->__get(static::$naming));
45
46
				# Set remove button
47
48
				$super = (static::$super && ($item['entity']->id === 1));
49
50
				$has_children = (static::$nesting && ($item['children'] > 0));
51
52
				$view->block('remove')->class = ((!$super && !$has_children) ? 'negative' : 'disabled');
53
54
				# Add item additional data
55
56
				$this->processItem($view, $item['entity'], (static::$nesting ? $item['children'] : 0));
57
			}
58
59
			# ------------------------
60
61
			return $items;
62
		}
63
64
		# Get pagination block
65
66
		private function getPaginationBlock() {
67
68
			$query = (static::$nesting ? ('?parent_id=' . $this->parent->id) : '');
69
70
			$url = new Url(INSTALL_PATH . static::$link . $query);
71
72
			# ------------------------
73
74
			return Pagination::block($this->index, static::$display, $this->items['total'], $url);
75
		}
76
77
		# Get contents
78
79
		private function getContents(bool $ajax = false) {
80
81
			$contents = View::get($ajax ? static::$view_ajax_main : static::$view_main);
82
83
			# Set path
84
85
			if (static::$nesting) $contents->path = $this->parent->path;
86
87
			# Process parent block
88
89
			if (static::$nesting && !$ajax) $this->processParent($contents->block('parent'));
90
91
			# Set items
92
93
			$items = $this->getItemsBlock($ajax);
94
95
			if ($items->count() > 0) $contents->items = $items;
96
97
			# Set pagination
98
99
			if (!$ajax) $contents->pagination = $this->getPaginationBlock();
100
101
			# Add additional data for specific entity
102
103
			$this->processEntity($contents);
104
105
			# ------------------------
106
107
			return $contents;
108
		}
109
110
		# Handle ajax request
111
112
		private function handleAjax() {
113
114
			$ajax = Ajax::response();
115
116
			# Create parent entity
117
118
			if (static::$nesting) $this->parent = Entitizer::get(static::$type, Number::format(Request::get('parent_id')));
119
120
			# Get children items
121
122
			$lister = new static::$lister(); $parent_id = (static::$nesting ? $this->parent->id : 0);
123
124
			$this->items = $lister->select(0, 0, $parent_id, Number::format(Request::post('id')));
125
126
			# ------------------------
127
128
			return $ajax->set('contents', $this->getContents(true)->contents(true));
129
		}
130
131
		# Handle common request
132
133
		public function handle() {
134
135
			if (Request::isAjax()) return $this->handleAjax();
136
137
			$this->index = Number::format(Request::get('index'), 1, 999999);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Number::format(\Request...et('index'), 1, 999999) can also be of type double or string. However, the property $index is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
138
139
			# Create parent entity
140
141
			if (static::$nesting) $this->parent = Entitizer::get(static::$type, Number::format(Request::get('parent_id')));
142
143
			# Get children items
144
145
			$lister = new static::$lister(); $parent_id = (static::$nesting ? $this->parent->id : 0);
146
147
			$this->items = $lister->select($this->index, static::$display, $parent_id);
148
149
			# ------------------------
150
151
			return $this->getContents();
152
		}
153
	}
154
}
155