Completed
Push — 1.x ( c86fd3...4b8bd5 )
by Akihito
14s queued 12s
created

CompileClassMetaInfo   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 27
c 1
b 0
f 0
dl 0
loc 60
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 28 5
A saveNamedParam() 0 16 4
A isMagicMethod() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Compiler;
6
7
use BEAR\Resource\Exception\ParameterException;
8
use BEAR\Resource\NamedParameterInterface;
9
use Doctrine\Common\Annotations\Reader;
10
use ReflectionClass;
11
12
use function in_array;
13
use function is_callable;
14
use function sprintf;
15
use function substr;
16
17
final class CompileClassMetaInfo
18
{
19
    /**
20
     * Save annotation and method meta information
21
     *
22
     * @param class-string<T> $className
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
23
     *
24
     * @template T
25
     */
26
    public function __invoke(Reader $reader, NamedParameterInterface $namedParams, string $className): void
27
    {
28
        $class = new ReflectionClass($className);
29
        $instance = $class->newInstanceWithoutConstructor();
30
        if (! $instance instanceof $className) {
31
            return; // @codeCoverageIgnore
32
        }
33
34
        $reader->getClassAnnotations($class);
35
        $methods = $class->getMethods();
36
        $log = sprintf('M %s:', $className);
37
        foreach ($methods as $method) {
38
            $methodName = $method->getName();
39
            if ($this->isMagicMethod($methodName)) {
40
                continue;
41
            }
42
43
            if (substr($methodName, 0, 2) === 'on') {
44
                $log .= sprintf(' %s', $methodName);
45
                $this->saveNamedParam($namedParams, $instance, $methodName);
46
            }
47
48
            // method annotation
49
            $reader->getMethodAnnotations($method);
50
            $log .= sprintf('@ %s', $methodName);
51
        }
52
53
        unset($log); // break here to see the $log
54
    }
55
56
    private function isMagicMethod(string $method): bool
57
    {
58
        return in_array($method, ['__sleep', '__wakeup', 'offsetGet', 'offsetSet', 'offsetExists', 'offsetUnset', 'count', 'ksort', 'asort', 'jsonSerialize'], true);
59
    }
60
61
    private function saveNamedParam(NamedParameterInterface $namedParameter, object $instance, string $method): void
62
    {
63
        // named parameter
64
        if (! in_array($method, ['onGet', 'onPost', 'onPut', 'onPatch', 'onDelete', 'onHead'], true)) {
65
            return;  // @codeCoverageIgnore
66
        }
67
68
        $callable = [$instance, $method];
69
        if (! is_callable($callable)) {
70
            return;  // @codeCoverageIgnore
71
        }
72
73
        try {
74
            $namedParameter->getParameters($callable, []);
75
        } catch (ParameterException $e) {
76
            return;
77
        }
78
    }
79
}
80