Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
created

AdminBundle/Helper/AdminPanel/AdminPanel.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Helper\AdminPanel;
4
5
class AdminPanel
6
{
7
    /**
8
     * @var AdminPanelAdaptorInterface[]
9
     */
10
    private $adaptors = array();
11
12
    /**
13
     * @var AdminPanelAdaptorInterface[]
14
     */
15
    private $sorted = array();
16
17
    /**
18
     * @var AdminPanelActionInterface[]
19
     */
20
    private $actions = null;
21
22
    /**
23
     * Add admin panel adaptor
24
     *
25
     * @param AdminPanelAdaptorInterface $adaptor
26
     */
27 1
    public function addAdminPanelAdaptor(AdminPanelAdaptorInterface $adaptor, $priority = 0)
28
    {
29 1
        $this->adaptors[$priority][] = $adaptor;
30 1
        unset($this->sorted);
31 1
    }
32
33
    /**
34
     * Return current admin panel actions
35
     */
36 1
    public function getAdminPanelActions()
37
    {
38 1
        if (!$this->actions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->actions of type Kunstmaan\AdminBundle\He...nPanelActionInterface[] 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...
39 1
            $this->actions = array();
40 1
            $adaptors = $this->getAdaptors();
41 1
            foreach ($adaptors as $adaptor) {
0 ignored issues
show
The expression $adaptors of type null|array<integer,objec...PanelAdaptorInterface>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
42 1
                $this->actions = array_merge($this->actions, $adaptor->getAdminPanelActions());
43
            }
44
        }
45
46 1
        return $this->actions;
47
    }
48
49
    /**
50
     * Get adaptors sorted by priority.
51
     *
52
     * @return \Kunstmaan\AdminBundle\Helper\AdminPanel\AdminPanelAdaptorInterface[]
53
     */
54 1
    private function getAdaptors()
55
    {
56 1
        if (!isset($this->sorted)) {
57 1
            $this->sortAdaptors();
58
        }
59
60 1
        return $this->sorted;
61
    }
62
63
    /**
64
     * Sorts the internal list of adaptors by priority.
65
     */
66 1 View Code Duplication
    private function sortAdaptors()
67
    {
68 1
        $this->sorted = array();
69
70 1
        if (isset($this->adaptors)) {
71 1
            krsort($this->adaptors);
72 1
            $this->sorted = call_user_func_array('array_merge', $this->adaptors);
73
        }
74 1
    }
75
}
76