UnorderedListPresenter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 6
c 3
b 0
f 1
lcom 1
cbo 2
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B render() 0 23 5
1
<?php
2
3
namespace Iatstuti\SimpleMenu\Presenters;
4
5
use Iatstuti\SimpleMenu\Menu;
6
use Iatstuti\SimpleMenu\MenuItem;
7
8
/**
9
 * Unordered list presenter for menu objects
10
 *
11
 * @package    Iatstuti\SimpleMenu
12
 * @copyright  2016 IATSTUTI
13
 * @author     Michael Dyrynda <[email protected]>
14
 */
15
class UnorderedListPresenter implements MenuPresenter
16
{
17
18
    /**
19
     * @var \Iatstuti\SimpleMenu\Menu
20
     */
21
    protected $menu;
22
23
24
    /**
25
     * UnorderedListPresenter constructor.
26
     *
27
     * @param \Iatstuti\SimpleMenu\Menu $menu
28
     */
29
    public function __construct(Menu $menu)
30
    {
31
        $this->menu = $menu;
32
    }
33
34
    /**
35
     * Render the given menu as an unordered list.
36
     *
37
     * @return string
38
     */
39
    public function render()
40
    {
41
        $output  = '<ul>';
42
43
        foreach ($this->menu->items() as $item) {
44
            if ($item instanceof Menu) {
45
                // Have a new instance of this presenter render the nested/submenu item
46
                $output .= sprintf('<li>%s%s</li>', $item->label, (new static($item))->render());
47
            } else if ($item instanceof MenuItem) {
48
                // Render the link as-is
49
                $output .= sprintf(
50
                    '<li%1$s><a href="%2$s" title="%3$s">%3$s</a></li>',
51
                    $item->options('class') ? sprintf(' class="%s"', $item->options('class')) : null,
52
                    $item->link,
0 ignored issues
show
Documentation introduced by
The property $link is declared protected in Iatstuti\SimpleMenu\MenuItem. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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...
53
                    $item->label
54
                );
55
            }
56
        }
57
58
        $output .= '</ul>';
59
60
        return $output;
61
    }
62
}
63