Completed
Push — master ( 692093...daa27a )
by Vitaly
02:57
created

AnnotationValueTrait::parseAnnotationValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types = 1);
3
4
/**
5
 * Created by Vitaly Iegorov <[email protected]>.
6
 * on 06.08.16 at 13:44
7
 */
8
namespace samsonframework\container\annotation;
9
10
/**
11
 *  Annotation with value trait.
12
 */
13 View Code Duplication
trait AnnotationValueTrait
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
    /**
16
     * Parse annotation value.
17
     *
18
     * @param array $valueOrValues Class collection
19
     *
20
     * @return array Parsed annotation key=>value collection
21
     *
22
     * @throws \InvalidArgumentException Thrown when neither string nor string[] is passed
23
     */
24 16
    public function parseAnnotationValue(array $valueOrValues) : array
25
    {
26 16
        if ($this->checkValuePresence($valueOrValues)) {
27
            // Convert empty values to null
28 15
            $value = $valueOrValues['value'] ?? null;
29
30
            // Always store array
31 15
            return is_array($value) ? $value : [$value];
32
        } else {
33 1
            throw new \InvalidArgumentException('Only string or array is allowed');
34
        }
35
    }
36
37
    /**
38
     * Check if we received an array with keys.
39
     *
40
     * @param array $valueOrValues
41
     *
42
     * @return bool True if value key exists
43
     */
44 16
    protected function checkValuePresence(array $valueOrValues)
45
    {
46 16
        return count(array_filter(array_keys($valueOrValues)));
47
    }
48
}
49