Completed
Pull Request — master (#1)
by Michael
02:12
created

UnorderedListPresenter::render()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 23
rs 8.5906
cc 5
eloc 13
nc 4
nop 1
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
     * Render the given menu as an unordered list.
20
     *
21
     * @param  \Iatstuti\SimpleMenu\Menu $menu
22
     *
23
     * @return string
24
     */
25
    public function render(Menu $menu)
26
    {
27
        $output  = '<ul>';
28
29
        foreach ($menu->items() as $item) {
30
            if ($item instanceof Menu) {
31
                // Have a new instance of this presenter render the nested/submenu item
32
                $output .= sprintf('<li>%s%s</li>', $item->label, (new static)->render($item));
0 ignored issues
show
Documentation introduced by
The property $label is declared protected in Iatstuti\SimpleMenu\Menu. 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...
33
            } else if ($item instanceof MenuItem) {
34
                // Render the link as-is
35
                $output .= sprintf(
36
                    '<li%1$s><a href="%2$s" title="%3$s">%3$s</a></li>',
37
                    $item->options('class') ? sprintf(' class="%s"', $item->options('class')) : null,
38
                    $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...
39
                    $item->label
0 ignored issues
show
Documentation introduced by
The property $label 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...
40
                );
41
            }
42
        }
43
44
        $output .= '</ul>';
45
46
        return $output;
47
    }
48
}
49