Code Duplication    Length = 34-36 lines in 2 locations

src/annotation/AnnotationValueTrait.php 1 location

@@ 13-48 (lines=36) @@
10
/**
11
 *  Annotation with value trait.
12
 */
13
trait AnnotationValueTrait
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
    public function parseAnnotationValue(array $valueOrValues) : array
25
    {
26
        if ($this->checkValuePresence($valueOrValues)) {
27
            // Convert empty values to null
28
            $value = $valueOrValues['value'] ?? null;
29
30
            // Always store array
31
            return is_array($value) ? $value : [$value];
32
        } else {
33
            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
    protected function checkValuePresence(array $valueOrValues)
45
    {
46
        return count(array_filter(array_keys($valueOrValues)));
47
    }
48
}
49

src/annotation/AnnotationWithValue.php 1 location

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