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

HasUniqueIdTrait::setUniqueID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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