AnnotationManager::buildPropertyInfo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace Saxulum\AnnotationManager\Manager;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Saxulum\AnnotationManager\Helper\ClassInfo;
7
use Saxulum\AnnotationManager\Helper\MethodInfo;
8
use Saxulum\AnnotationManager\Helper\PropertyInfo;
9
use Saxulum\ClassFinder\ClassFinder;
10
use Symfony\Component\Finder\Finder;
11
use Symfony\Component\Finder\SplFileInfo;
12
13
class AnnotationManager
14
{
15
    /**
16
     * @var Reader
17
     */
18
    protected $annotationReader;
19
20
    public function __construct(Reader $annotationReader)
21
    {
22
        $this->annotationReader = $annotationReader;
23
    }
24
25
    /**
26
     * @param  array       $paths
27
     * @return ClassInfo[]
28
     */
29
    public function buildClassInfosBasedOnPaths(array $paths = array())
30
    {
31
        $classInfos = array();
32
        foreach ($paths as $path) {
33
            $classInfos = array_merge($classInfos, $this->buildClassInfosBasedOnPath($path));
34
        }
35
36
        return $classInfos;
37
    }
38
39
    /**
40
     * @param  string      $path
41
     * @return ClassInfo[]
42
     */
43
    public function buildClassInfosBasedOnPath($path)
44
    {
45
        return $this->buildClassInfos(self::getReflectionClasses($path));
46
    }
47
48
    /**
49
     * @param  \ReflectionClass[] $reflectionClasses
50
     * @return ClassInfo[]
51
     */
52
    public function buildClassInfos(array $reflectionClasses)
53
    {
54
        $classInfos = array();
55
        foreach ($reflectionClasses as $reflectionClass) {
56
            $classInfos[] = $this->buildClassInfo($reflectionClass);
57
        }
58
59
        return $classInfos;
60
    }
61
62
    /**
63
     * @param  \ReflectionClass $reflectionClass
64
     * @return ClassInfo
65
     */
66
    public function buildClassInfo(\ReflectionClass $reflectionClass)
67
    {
68
        $classAnnotations = $this
69
            ->annotationReader
70
            ->getClassAnnotations($reflectionClass)
71
        ;
72
73
        return new ClassInfo(
74
            $reflectionClass->getName(),
75
            $classAnnotations,
76
            $this->buildPropertyInfos($reflectionClass),
77
            $this->buildMethodInfos($reflectionClass)
78
        );
79
    }
80
81
    /**
82
     * @param  \ReflectionClass $reflectionClass
83
     * @return PropertyInfo[]
84
     */
85
    protected function buildPropertyInfos(\ReflectionClass $reflectionClass)
86
    {
87
        $propertyInfos = array();
88
        foreach ($reflectionClass->getProperties() as $reflectionProperty) {
89
            $propertyInfos[] = $this->buildPropertyInfo($reflectionProperty);
90
        }
91
92
        return $propertyInfos;
93
    }
94
95
    /**
96
     * @param  \ReflectionProperty $reflectionProperty
97
     * @return PropertyInfo
98
     */
99
    protected function buildPropertyInfo(\ReflectionProperty $reflectionProperty)
100
    {
101
        $propertyAnnotations = $this
102
            ->annotationReader
103
            ->getPropertyAnnotations($reflectionProperty)
104
        ;
105
106
        return new PropertyInfo(
107
            $reflectionProperty->getName(),
108
            $propertyAnnotations
109
        );
110
    }
111
112
    /**
113
     * @param  \ReflectionClass $reflectionClass
114
     * @return MethodInfo[]
115
     */
116
    protected function buildMethodInfos(\ReflectionClass $reflectionClass)
117
    {
118
        $methodInfos = array();
119
        foreach ($reflectionClass->getMethods() as $reflectionMethod) {
120
            $methodInfos[] = $this->buildMethodInfo($reflectionMethod);
121
        }
122
123
        return $methodInfos;
124
    }
125
126
    /**
127
     * @param  \ReflectionMethod $reflectionMethod
128
     * @return MethodInfo
129
     */
130
    protected function buildMethodInfo(\ReflectionMethod $reflectionMethod)
131
    {
132
        $methodAnnotations = $this
133
            ->annotationReader
134
            ->getMethodAnnotations($reflectionMethod)
135
        ;
136
137
        return new MethodInfo(
138
            $reflectionMethod->getName(),
139
            $methodAnnotations
140
        );
141
    }
142
143
    /**
144
     * @param  string                    $path
145
     * @return \ReflectionClass[]
146
     * @throws \InvalidArgumentException
147
     */
148
    public static function getReflectionClasses($path)
149
    {
150
        $classReflections = array();
151
152
        if (!is_dir($path)) {
153
            throw new \InvalidArgumentException('Path is not a directory');
154
        }
155
156
        foreach (Finder::create()->files()->name('*.php')->in($path)->sortByName() as $file) {
157
            $namespaces = self::findClassesWithinAFile($file);
158
            foreach ($namespaces as $classNamespace) {
159
                $reflectionClass = new \ReflectionClass($classNamespace);
160
                if ($reflectionClass->isInstantiable()) {
161
                    $classReflections[] = $reflectionClass;
162
                }
163
            }
164
        }
165
166
        return $classReflections;
167
    }
168
169
    /**
170
     * @param  SplFileInfo $file
171
     * @return array
172
     */
173
    public static function findClassesWithinAFile(SplFileInfo $file)
174
    {
175
        return ClassFinder::findClasses($file->getContents());
176
    }
177
}
178