Test Failed
Push — master ( 794710...4acb1e )
by Georgi
03:38
created

ActionBar::addItemButton()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Epesi\Core\Layout\View;
4
5
use atk4\ui\Menu;
6
use atk4\ui\Item;
7
8
class ActionBar extends Menu
9
{
10
    public $ui = 'actionbar menu';
11
    
12
	protected static $buttons = [];
13
	
14
	protected static $menus = [];
15
	
16
	protected static function getPredefined($key)
17
	{
18
		$predefined = [
19
				'back' => [
20
						__('Back'),
21
						'icon' => 'arrow left',
22
						'weight' => 10000,
23
						'attr' => [
24
								'href' => $_SERVER['HTTP_REFERER']?? 'javascript:window.history.back()'
25
						],
26
				],
27
				'save' => [
28
						__('Save'),
29
						'icon' => 'save',
30
				],
31
				'add' => [
32
						__('Add'),
33
						'icon' => 'add',
34
				],
35
				'edit' => [
36
						__('Edit'),
37
						'icon' => 'edit'
38
				],
39
				'delete' => [
40
						__('Delete'),
41
						'icon' => 'trash'
42
				],
43
		];
44
		
45
		return $predefined[$key] ?? ['label' => $key];
46
	}
47
	
48
	public function renderView()
49
    {
50
        $this->elements = collect($this->elements)->sortByDesc(function ($element) {
51
            return $element->weight ?? 10;
52
        })->toArray();
53
54
        return parent::renderView();
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::renderView() targeting atk4\ui\Menu::renderView() 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...
55
	}
56
	
57
	/**
58
	 * Adds a button to the ActionBar
59
	 * 
60
	 * @param string|array|ActionBarItem $button
61
	 * 
62
	 * @return Item
63
	 */
64
	public static function addItemButton($button, $defaults = [])
65
	{
66
		$button = is_string($button)? self::getPredefined($button): $button;
67
		
68
		$button = is_array($button)? new ActionBarItem($button): $button;
69
		
70
		$actionBar = self::instance();
71
		
72
		return $actionBar->addItem($actionBar->mergeSeeds($button, $defaults));
0 ignored issues
show
Bug introduced by
It seems like $actionBar->mergeSeeds($button, $defaults) can also be of type object; however, parameter $item of atk4\ui\Menu::addItem() does only seem to accept array|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

72
		return $actionBar->addItem(/** @scrutinizer ignore-type */ $actionBar->mergeSeeds($button, $defaults));
Loading history...
73
	}
74
	
75
	public static function addButtons($buttons)
76
	{
77
		foreach ((array) $buttons as $button) {
78
			self::addItemButton($button);
79
		}
80
	}
81
	
82
	public static function addMenuButton($menu)
83
	{
84
	    return self::instance()->addMenu($menu);
85
	}
86
87
	public static function clear()
88
	{
89
		self::instance()->elements = null;
90
	}
91
	
92
	/**
93
	 * @return self
94
	 */
95
	public static function instance()
96
	{
97
		return ui()->layout->actionBar;
0 ignored issues
show
Bug introduced by
The property actionBar does not seem to exist on atk4\ui\Layout\Generic.
Loading history...
98
	}
99
}
100