Passed
Branch release/v2.0.0 (680694)
by Anatoly
06:01 queued 02:13
created

AbstractAnnotation   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 14
c 2
b 0
f 0
dl 0
loc 38
ccs 0
cts 24
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFields() 0 10 3
A toArray() 0 17 6
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router\Annotation\OpenApi;
13
14
/**
15
 * Import functions
16
 */
17
use function is_array;
18
19
/**
20
 * AbstractAnnotation
21
 */
22
abstract class AbstractAnnotation implements AnnotationInterface
23
{
24
25
    /**
26
     * {@inheritDoc}
27
     */
28
    public function toArray() : array
29
    {
30
        $fields = $this->getFields();
31
32
        $result = [];
33
        foreach ($fields as $field => $value) {
34
            if (!is_array($value)) {
35
                $result[$field] = ($value instanceof AnnotationInterface) ? $value->toArray() : $value;
36
                continue;
37
            }
38
39
            foreach ($value as $key => $item) {
40
                $result[$field][$key] = ($item instanceof AnnotationInterface) ? $item->toArray() : $item;
41
            }
42
        }
43
44
        return $result;
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    private function getFields() : array
51
    {
52
        $result = [];
53
        foreach ($this as $field => $value) {
54
            if (isset($value)) {
55
                $result[$field] = $value;
56
            }
57
        }
58
59
        return $result;
60
    }
61
}
62