Completed
Push — master ( 1a504b...b14f85 )
by Pol
12:27
created

src/Traits/ConfigurationTokensTrait.php (1 issue)

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
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 1
    private function extractRawTokens($text): array
40
    {
41 1
        \preg_match_all('/\$\{(([A-Za-z_\-]+\.?)+)\}/', $text, $matches);
42
43 1
        if (isset($matches[0]) && !empty($matches[0]) && \is_array($matches[0])) {
44 1
            if (false !== $return = \array_combine($matches[0], $matches[1])) {
45 1
                return $return;
46
            }
47
        }
48
49
        return [];
50
    }
51
}
52