Passed
Push — master ( a228a3...2f45dd )
by Chris
14:02
created

TypedClassPrinterTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 42
ccs 0
cts 16
cp 0
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A printFromType() 0 9 1
A isDoingTypeMatch() 0 3 1
A getSignaturesFromType() 0 15 3
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 isDoingTypeMatch(): bool
42
    {
43
        return $this->isDoingTypeMatch;
44
    }
45
46
    abstract protected function print(array $methods): string;
47
48
    abstract protected function getDefaultSignatures(): array;
49
}
50