Completed
Push — master ( 02819f...5052b0 )
by Gabriel
03:56
created

HasDisplayGroupsTrait::newDisplayGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Nip\Form\Traits;
4
5
use Nip_Form_DisplayGroup;
6
7
/**
8
 * Trait HasDisplayGroupsTrait
9
 * @package Nip\Form\Traits
10
 */
11
trait HasDisplayGroupsTrait
12
{
13
    protected $_displayGroups = [];
14
15
    /**
16
     * Add a display group
17
     * Groups named elements for display purposes.
18
     * @param array $elements
19
     * @param $name
20
     * @return $this
21
     */
22 1
    public function addDisplayGroup(array $elements, $name)
23
    {
24 1
        $group = $this->newDisplayGroup();
25 1
        foreach ($elements as $element) {
26 1
            if (isset($this->_elements[$element])) {
0 ignored issues
show
Bug introduced by
The property _elements 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...
27 1
                $add = $this->getElement($element);
0 ignored issues
show
Bug introduced by
It seems like getElement() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
28 1
                if (null !== $add) {
29 1
                    $group->addElement($add);
30
                }
31
            }
32
        }
33 1
        if (empty($group)) {
34
            trigger_error('No valid elements specified for display group');
35
        }
36
37 1
        $name = (string)$name;
38 1
        $group->setLegend($name);
39
40 1
        $this->_displayGroups[$name] = $group;
41
42 1
        return $this;
43
    }
44
45
    /**
46
     * @return Nip_Form_DisplayGroup
47
     */
48 1
    public function newDisplayGroup()
49
    {
50 1
        $group = new Nip_Form_DisplayGroup();
51 1
        $group->setForm($this);
52
53 1
        return $group;
54
    }
55
56
    /**
57
     * @param string $name
58
     * @return Nip_Form_DisplayGroup
59
     */
60 1
    public function getDisplayGroup($name)
61
    {
62 1
        if (array_key_exists($name, $this->_displayGroups)) {
63 1
            return $this->_displayGroups[$name];
64
        }
65
66
        return null;
67
    }
68
69
    /**
70
     * @return Nip_Form_DisplayGroup[]
71
     */
72
    public function getDisplayGroups()
73
    {
74
        return $this->_displayGroups;
75
    }
76
}
77