Passed
Push — master ( e7d3cc...21de9b )
by Maxim
03:42
created

ClassData::getAllMethods()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is a part of "Axessors" library.
4
 *
5
 * @author <[email protected]>
6
 * @license GPL
7
 */
8
9
namespace NoOne4rever\Axessors;
10
11
use NoOne4rever\Axessors\Exceptions\InternalError;
12
13
/**
14
 * Class ClassData.
15
 *
16
 * Stores the reflection of a class and Axessors properties.
17
 *
18
 * @package NoOne4rever\Axessors
19
 */
20
class ClassData
21
{
22
    /** @var \ReflectionClass class data */
23
    public $reflection;
24
25
    /** @var PropertyData[] Axessors properties */
26
    private $properties = [];
27
28
    /**
29
     * ClassData constructor.
30
     *
31
     * @param \ReflectionClass $reflection reflection of the class
32
     */
33
    public function __construct(\ReflectionClass $reflection)
34
    {
35
        $this->reflection = $reflection;
36
    }
37
38
    /**
39
     * Adds property information to class data.
40
     *
41
     * @param string $name name of the property
42
     * @param PropertyData $propertyData property data
43
     */
44
    public function addProperty(string $name, PropertyData $propertyData)
45
    {
46
        $this->properties[$name] = $propertyData;
47
    }
48
49
    /**
50
     * Returns property by name.
51
     *
52
     * @param string $name property name
53
     * @return PropertyData information about property
54
     * @throws InternalError if property not found
55
     */
56
    public function getProperty(string $name): PropertyData
57
    {
58
        if (isset($this->properties[$name])) {
59
            return $this->properties[$name];
60
        } else {
61
            throw new InternalError("property {$this->reflection->name}::\$$name not found in Axessors\\ClassData");
62
        }
63
    }
64
65
    /**
66
     * Returns all Axessors properties.
67
     *
68
     * @return PropertyData[] class' properties
69
     */
70 2
    public function getAllProperties(): array
71
    {
72 2
        return $this->properties;
73
    }
74
75
    /**
76
     * Returns all Axessors methods names.
77
     *
78
     * @param bool $skipPrivate a flag; indicates if private methods are skipped
79
     * @return string[] Axessors methods' names
80
     */
81
    public function getAllMethods(bool $skipPrivate = false): array
82
    {
83
        $methods = [];
84
        foreach ($this->properties as $propertyData) {
85
            $propertyMethods = $propertyData->getMethods();
86
            if ($skipPrivate) {
87
                unset($propertyMethods['private']);
88
            }
89
            $methods = array_merge_recursive($methods, $propertyMethods);
90
        }
91
        return $methods;
92
    }
93
}
94