Completed
Push — master ( a4e176...8c9478 )
by Adam
02:50
created

Key::keyExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace BestServedCold\PhalueObjects\VOArray;
4
5
/**
6
 * Class KeyTrait
7
 * @package BestServedCold\PhalueObjects
8
 */
9
trait Key
10
{
11
    /**
12
     * @param  string|int $key
13
     * @return bool|mixed
14
     */
15 3
    public function getKey($key)
16
    {
17 3
        return $this->keyExists($key) ? $this->getValue()[$key] : false;
0 ignored issues
show
Bug introduced by
It seems like getValue() 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...
18
    }
19
20
    /**
21
     * @return array
22
     */
23 5
    public function getKeys()
24
    {
25 5
        return array_keys($this->getValue());
0 ignored issues
show
Bug introduced by
It seems like getValue() 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...
26
    }
27
28
    /**
29
     * @return mixed
30
     */
31 1
    public function getFirstKey()
32
    {
33 1
        $keys = $this->getKeys();
34 1
        return array_shift($keys);
35
    }
36
37
    /**
38
     * @return mixed
39
     */
40 4
    public function getLastKey()
41
    {
42 4
        $keys = $this->getKeys();
43 4
        return end($keys);
44
    }
45
46
    /**
47
     * @param  $key
48
     * @return bool
49
     */
50 3
    public function keyExists($key)
0 ignored issues
show
Coding Style introduced by
function keyExists() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
51
    {
52 3
        return array_key_exists($key, $this->getValue());
0 ignored issues
show
Bug introduced by
It seems like getValue() 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...
53
    }
54
55
}
56