Completed
Pull Request — master (#46)
by Jitendra
01:49
created

Metadata::forReflectionMethod()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 20
nc 4
nop 1
dl 0
loc 29
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the PHINT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Phint\Util;
13
14
use CrazyFactory\DocBlocks\DocBlock;
15
16
class Metadata
17
{
18
    public function forClass(string $classFqcn): array
19
    {
20
        return $this->forReflectionClass(new \ReflectionClass($classFqcn));
21
    }
22
23
    public function forReflectionClass(\ReflectionClass $class): array
24
    {
25
        $texts = (new DocBlock($class))->texts();
26
        $title = \array_shift($texts);
27
28
        $metadata = [
29
            'classFqcn'   => $class->name,
30
            'classPath'   => $class->getFileName(),
31
            'name'        => $class->getShortName(),
32
            'isTrait'     => $class->isTrait(),
33
            'isAbstract'  => $class->isAbstract(),
34
            'isInterface' => $class->isInterface(),
35
            'newable'     => $class->isInstantiable(),
36
            'title'       => $title,
37
            'texts'       => $texts,
38
            'methods'     => [],
39
        ];
40
41
        foreach ($class->getMethods() as $method) {
42
            if ($method->class !== $class->name) {
43
                continue;
44
            }
45
46
            $metadata['methods'][$method->name] = $this->forReflectionMethod($method);
47
        }
48
49
        return $metadata;
50
    }
51
52
    public function forMethod(string $classFqcn, string $method): array
53
    {
54
        $reflMethod = (new \ReflectionClass($classFqcn))->getMethod($method);
55
56
        return $this->forReflectionMethod($reflMethod);
57
    }
58
59
    public function forReflectionMethod(\ReflectionMethod $method): array
60
    {
61
        $parser = new DocBlock($method);
62
        $texts  = $parser->texts();
63
        $title  = \array_shift($texts);
64
65
        $metadata = [
66
            'name'       => $method->name,
67
            'inClass'    => $method->getDeclaringClass()->name,
68
            'isStatic'   => $method->isStatic(),
69
            'isFinal'    => $method->isFinal(),
70
            'isPublic'   => $method->isPublic(),
71
            'isAbstract' => $method->isAbstract(),
72
            'maybeMagic' => \substr($method->name, 0, 2) === '__',
73
            'title'      => $title,
74
            'texts'      => $texts,
75
            'return'     => 'void',
76
            'params'     => [],
77
        ];
78
79
        foreach ($parser->find('param') as $param) {
80
            $metadata['params'][] = \preg_replace(['/(.*\$\w+)(.*)/', '/ +/'], ['$1', ' '], $param->getValue());
81
        }
82
83
        if (null !== $return = $parser->first('return')) {
84
            $metadata['return'] = \preg_replace('/(\S+)(.*)/', '$1', $return->getValue());
85
        }
86
87
        return $metadata;
88
    }
89
}
90