ClassMethodAttributeReader   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 25
rs 10
c 1
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A read() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kafkiansky\SymfonyMiddleware\Attribute\Reader;
6
7
use Kafkiansky\SymfonyMiddleware\Attribute\Middleware;
8
9
final class ClassMethodAttributeReader implements AttributeReader
10
{
11
    /**
12
     * @param object $class
13
     * @param string|null $method
14
     *
15
     * @throws \ReflectionException
16
     *
17
     * @return Middleware[]
18
     */
19
    public function read(object $class, ?string $method = null): array
20
    {
21
        $attributes = (new \ReflectionClass($class))->getAttributes(AttributeReader::ATTRIBUTE);
22
23
        if ($method !== null) {
24
            $attributes = array_merge(
25
                $attributes,
26
                (new \ReflectionMethod($class, $method))->getAttributes(AttributeReader::ATTRIBUTE)
27
            );
28
        }
29
30
        return array_map(static function (\ReflectionAttribute $attribute): object {
31
            /** @var Middleware */
32
            return $attribute->newInstance();
33
        }, $attributes);
34
    }
35
}
36