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

TypedClassPrinterTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 52
ccs 0
cts 20
cp 0
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A printFromType() 0 9 1
A getSignaturesFromType() 0 15 3
A isDoingTypeMatch() 0 3 1
A matchTraitsToType() 0 7 1
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