Test Setup Failed
Push — master ( 6d6991...a81a2e )
by Gabriel
02:14
created

HasUniqueIdTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 58
c 0
b 0
f 0
ccs 17
cts 21
cp 0.8095
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getJSID() 0 6 1
A getUniqueId() 0 8 2
A setUniqueID() 0 4 1
A initUniqueId() 0 4 1
A generateUniqueId() 0 12 2
1
<?php
2
3
namespace Nip\Form\Elements\Traits;
4
5
/**
6
 * Trait HasUniqueIdTrait
7
 * @package Nip\Form\Elements\Traits
8
 */
9
trait HasUniqueIdTrait
10
{
11
    /**
12
     * @var null|string
13
     */
14
    protected $_uniqueID = null;
15
16
    /**
17
     * @return string
18
     */
19
    public function getJSID()
20
    {
21
        $name = $this->getUniqueId();
22
23
        return str_replace(['][', '[', ']'], ['-', '-', ''], $name);
24
    }
25
26
    /**
27
     * @return null|string
28
     */
29 1
    public function getUniqueId()
30
    {
31 1
        if (!$this->_uniqueID) {
32 1
            $this->initUniqueId();
33
        }
34
35 1
        return $this->_uniqueID;
36
    }
37
38
    /**
39
     * @param null|string $uniqueID
40
     */
41 1
    public function setUniqueID($uniqueID)
42
    {
43 1
        $this->_uniqueID = $uniqueID;
44 1
    }
45
46 1
    protected function initUniqueId()
47
    {
48 1
        $this->setUniqueID($this->generateUniqueId());
49 1
    }
50
51
    /**
52
     * @return null|string
53
     */
54 1
    protected function generateUniqueId()
55
    {
56 1
        $name = $this->getName();
0 ignored issues
show
Bug introduced by
It seems like getName() 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...
57 1
        $registeredNames = (array)$this->getForm()->getCache('elements_names');
58 1
        if (in_array($name, $registeredNames)) {
59
            $name = uniqid($name);
60
        }
61 1
        $registeredNames[] = $name;
62 1
        $this->getForm()->setCache('elements_names', $registeredNames);
0 ignored issues
show
Bug introduced by
It seems like getForm() 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...
63
64 1
        return $name;
65
    }
66
}
67