Passed
Push — master ( b3365b...65b7f9 )
by Chris
16:01
created

TypedClassPrinterTrait::matchTraitsToType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Leonidas\Console\Library\Abstracts;
4
5
use ReflectionClass;
6
7
trait TypedClassPrinterTrait
8
{
9
    protected string $type;
10
11
    protected bool $isDoingTypeMatch = false;
12
13
    public function printFromType(): string
14
    {
15
        $this->isDoingTypeMatch = true;
16
17
        $output = $this->print($this->getSignaturesFromType());
18
19
        $this->isDoingTypeMatch = false;
20
21
        return $output;
22
    }
23
24
    protected function getSignaturesFromType(): array
25
    {
26
        $reflection = new ReflectionClass($this->type);
27
        $methods = $reflection->getMethods();
28
        $templates = $this->getDefaultSignatures();
29
30
        $signatures = [];
31
32
        foreach ($methods as $method) {
33
            if ($templates[$method = $method->getName()] ?? false) {
34
                $signatures[$method] = $templates[$method];
35
            }
36
        }
37
38
        return $signatures;
39
    }
40
41
    protected function matchTraitsToType(array $traits, array $map)
42
    {
43
        $extensions = array_values(class_implements($this->type));
44
45
        return array_filter(
46
            $traits,
47
            fn ($partial) => in_array($map[$partial], $extensions)
48
        );
49
    }
50
51
    protected function isDoingTypeMatch(): bool
52
    {
53
        return $this->isDoingTypeMatch;
54
    }
55
56
    abstract protected function print(array $methods): string;
57
58
    abstract protected function getDefaultSignatures(): array;
59
}
60