EnumDefinition   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A values() 0 12 2
A instantiateNode() 0 4 1
1
<?php
2
/*
3
 * This file is part of the Borobudur-Config package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Config\Definition\Builder;
12
13
use Borobudur\Config\Definition\EnumNode;
14
use Borobudur\Config\Exception\InvalidArgumentException;
15
16
/**
17
 * @author      Iqbal Maulana <[email protected]>
18
 * @created     8/10/15
19
 */
20
class EnumDefinition extends ScalarDefinition
21
{
22
    /**
23
     * @var array
24
     */
25
    private $values = array();
26
27
    /**
28
     * Define enum values.
29
     *
30
     * @param array $values
31
     *
32
     * @return $this
33
     */
34
    public function values(array $values)
35
    {
36
        $values = array_unique($values);
37
38
        if (count($values) < 2) {
39
            throw new InvalidArgumentException(sprintf('Minimum enum values is 2, "%d" given.', count($values)));
40
        }
41
42
        $this->values = $values;
43
44
        return $this;
45
    }
46
47
    /**
48
     * Instantiate enum node.
49
     *
50
     * @return EnumNode
51
     */
52
    protected function instantiateNode()
53
    {
54
        return new EnumNode($this->name, $this->parent, $this->values);
0 ignored issues
show
Bug introduced by
It seems like $this->parent can also be of type object<Borobudur\Config\...er\DefinitionInterface> or object<Borobudur\Config\...odeDefinitionInterface>; however, Borobudur\Config\Definit...EnumNode::__construct() does only seem to accept null|object<Borobudur\Co...finition\NodeInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
55
    }
56
}
57