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

AnnotationValueTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 36
loc 36
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseAnnotationValue() 12 12 3
A checkValuePresence() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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