Completed
Pull Request — master (#4)
by Michael
01:50
created

FetchesWeight::weight()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Iatstuti\SimpleMenu\Traits;
4
5
/**
6
 * As the functionality is repeated, use a trait to
7
 * fetch the weight from the class' options array.
8
 *
9
 * @package    Iatstuti\SimpleMenu
10
 * @copyright  2016 IATSTUTI
11
 * @author     Michael Dyrynda <[email protected]>
12
 */
13
trait FetchesWeight
14
{
15
16
    abstract public function options($key = null);
17
18
    /**
19
     * If not null, set this menu's weight, else return the current value.
20
     *
21
     * @param  int|null $weight
22
     *
23
     * @return mixed
24
     */
25
    public function weight($weight = null)
26
    {
27
        if (is_null($weight)) {
28
            return (int) $this->options('weight');
29
        }
30
31
        $this->options['weight'] = (int) $weight;
1 ignored issue
show
Bug introduced by
The property options does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
33
        return $this;
34
    }
35
}
36