ConfigurationBuilder::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace PhpDocReader;
4
5
/**
6
 * Class ConfigurationBuilder
7
 * @author  Lucas A. de Araújo <[email protected]>
8
 * @package PhpDocReader
9
 */
10
class ConfigurationBuilder
11
{
12
    /**
13
     * Cria um arquivo de configuração a
14
     * partir de um comentário.
15
     * @param $comment
16
     * @return array
17
     */
18
    public static function build($comment)
19
    {
20
        $json = self::getJson($comment);
21
        $config = self::compile($json);
22
        return $config;
23
    }
24
25
    /**
26
     * @param $comment
27
     * @return array
28
     */
29
    private static function getJson($comment)
30
    {
31
        preg_match_all("/@configure (.*?)\n/", $comment, $matches);
32
        return $matches[1];
33
    }
34
35
    /**
36
     * Junta todas configurações em uma array
37
     * @param array $config
38
     * @return array
39
     */
40
    private static function compile(array $config)
41
    {
42
        $fullConfig = [];
43
44
        foreach ($config as $json) {
45
            $jsonConfig = (array)json_decode($json);
46
            $fullConfig = array_merge($fullConfig, $jsonConfig);
47
        }
48
49
        return $fullConfig;
50
    }
51
}
52