Task   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 116
rs 10
c 1
b 0
f 0
wmc 24

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A match() 0 11 3
A setCommand() 0 7 4
A __construct() 0 11 1
A setPatterns() 0 9 3
A getHeaders() 0 13 3
A getOutput() 0 3 1
A toArray() 0 16 3
A setName() 0 3 1
A run() 0 14 4
1
<?php
2
3
namespace Bdelespierre\GitStats;
4
5
use Bdelespierre\GitStats\Interfaces\ProcessServiceInterface;
6
7
class Task
8
{
9
    protected ProcessServiceInterface $process;
10
    protected string $name;
11
    protected $command;
12
    protected array $patterns = [];
13
    protected string $output;
14
15
    public function __construct(
16
        ProcessServiceInterface $process,
17
        string $name,
18
        $command,
19
        array $patterns
20
    ) {
21
        $this->process = $process;
22
23
        $this->setName($name);
24
        $this->setCommand($command);
25
        $this->setPatterns($patterns);
26
    }
27
28
    public function getName(): string
29
    {
30
        return $this->name;
31
    }
32
33
    public function getHeaders(): array
34
    {
35
        if (empty($this->patterns)) {
36
            return [$this->getName()];
37
        }
38
39
        $headers = [];
40
41
        foreach ($this->patterns as $name => $regex) {
42
            $headers[] = "{$this->getName()}:{$name}";
43
        }
44
45
        return $headers;
46
    }
47
48
    public function run(...$args): self
49
    {
50
        if (is_array($this->command)) {
51
            $output = $this->process->run($this->command)->getOutput();
52
        } elseif (is_callable($this->command)) {
53
            $command = $this->command;
54
            $output = $command(...$args);
55
        } elseif (is_string($this->command)) {
56
            $output = $this->process->exec($this->command);
0 ignored issues
show
Bug introduced by
The method exec() does not exist on Bdelespierre\GitStats\In...ProcessServiceInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdelespierre\GitStats\In...ProcessServiceInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
            /** @scrutinizer ignore-call */ 
57
            $output = $this->process->exec($this->command);
Loading history...
57
        }
58
59
        $this->output = trim($output);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $output does not seem to be defined for all execution paths leading up to this point.
Loading history...
60
61
        return $this;
62
    }
63
64
    public function getOutput(): ?string
65
    {
66
        return $this->output;
67
    }
68
69
    public function toArray()
70
    {
71
72
        if (empty($this->patterns)) {
73
            return [
74
                $this->getName() => $this->getOutput()
75
            ];
76
        }
77
78
        $array = [];
79
80
        foreach ($this->patterns as $name => $pattern) {
81
            $array["{$this->getName()}:{$name}"] = $this->match($pattern);
82
        }
83
84
        return $array;
85
    }
86
87
    private function setName(string $name)
88
    {
89
        $this->name = $name;
90
    }
91
92
    private function setCommand($command)
93
    {
94
        if (! is_array($command) && ! is_callable($command) && ! is_string($command)) {
95
            throw new \InvalidArgumentException("Invalid command: '{$command}'.");
96
        }
97
98
        $this->command = $command;
99
    }
100
101
    private function setPatterns(array $patterns)
102
    {
103
        foreach ($patterns as $regex) {
104
            if (false === @preg_match($regex, null)) {
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $subject of preg_match(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
            if (false === @preg_match($regex, /** @scrutinizer ignore-type */ null)) {
Loading history...
105
                throw new \InvalidArgumentException("Invalid regex: '{$regex}'.");
106
            }
107
        }
108
109
        $this->patterns = $patterns;
110
    }
111
112
    private function match(string $regex)
113
    {
114
        $match = preg_match($regex, $this->getOutput(), $matches);
115
116
        // if the pattern contains a capturing group
117
        if (isset($matches[1])) {
118
            return $matches[1];
119
        }
120
121
        // did the regex match output?
122
        return $match ? true : false;
123
    }
124
}
125