Passed
Push — master ( 9e0b2c...e0b23c )
by Pol
03:34
created

ConfigurationTokensTrait::extractProcessedTokens()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core\Traits;
6
7
/**
8
 * Class ConfigurationTokensTrait.
9
 */
10
trait ConfigurationTokensTrait
11
{
12
    /**
13
     * Extract tokens and replace their values with current configuration.
14
     *
15
     * @param string $text
16
     *
17
     * @return array
18
     */
19
    public function extractProcessedTokens($text): array
20
    {
21
        /** @var \Robo\Config\Config $config */
22
        $config = $this->getConfig();
0 ignored issues
show
Bug introduced by
It seems like getConfig() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

22
        /** @scrutinizer ignore-call */ 
23
        $config = $this->getConfig();
Loading history...
23
24
        return \array_map(
25
            static function ($key) use ($config) {
26
                return $config->get($key);
27
            },
28
            $this->extractRawTokens($text)
29
        );
30
    }
31
32
    /**
33
     * Extract token in given text.
34
     *
35
     * @param string $text
36
     *
37
     * @return array
38
     */
39
    private function extractRawTokens($text): array
40
    {
41
        \preg_match_all('/\$\{(([A-Za-z_\-]+\.?)+)\}/', $text, $matches);
42
43
        if (isset($matches[0]) && !empty($matches[0]) && \is_array($matches[0])) {
44
            if (false !== $return = \array_combine($matches[0], $matches[1])) {
45
                return $return;
46
            }
47
        }
48
49
        return [];
50
    }
51
}
52