Passed
Push — master ( 9a486a...292ab7 )
by Alain
03:26
created

ConfigFactory::createFromFile()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.5069

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
ccs 8
cts 11
cp 0.7272
rs 8.5125
cc 5
eloc 12
nc 12
nop 1
crap 5.5069
1
<?php
2
/**
3
 * Bright Nucleus Config Component.
4
 *
5
 * @package   BrightNucleus\Config
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\Config;
13
14
use Exception;
15
16
/**
17
 * Create new object instances that implement ConfigInterface.
18
 *
19
 * @since   0.3.0
20
 *
21
 * @package BrightNucleus\Config
22
 * @author  Alain Schlesser <[email protected]>
23
 */
24
class ConfigFactory
25
{
26
27
    /**
28
     * Create a new ConfigInterface object from a file.
29
     *
30
     * If a comma-separated list of files is provided, they are checked in sequence until the first one could be loaded
31
     * successfully.
32
     *
33
     * @since 0.3.0
34
     *
35
     * @param string|array $_ List of files.
36
     *
37
     * @return ConfigInterface Instance of a ConfigInterface implementation.
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Config.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
38
     */
39 1
    public static function createFromFile($_)
40
    {
41 1
        $files = array_reverse(func_get_args());
42
43 1
        if (is_array($files[0])) {
44
            $files = $files[0];
45
        }
46
47 1
        while (count($files) > 0) {
48
            try {
49 1
                $file = array_pop($files);
50
51 1
                if (! file_exists($file)) {
52 1
                    continue;
53
                }
54
55 1
                return new Config(include $file);
56
            } catch (Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
57
58
            }
59
        }
60
61
        return new Config([]);
62
    }
63
64
    /**
65
     * Create a new ConfigInterface object from an array.
66
     *
67
     * @since 0.3.0
68
     *
69
     * @param array $array Array with configuration values.
70
     *
71
     * @return ConfigInterface Instance of a ConfigInterface implementation.
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Config.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
72
     */
73 1
    public static function createFromArray(array $array)
74
    {
75 1
        return new Config($array);
76
    }
77
78
    /**
79
     * Create a new ConfigInterface object.
80
     *
81
     * Tries to deduce the correct creation method by inspecting the provided arguments.
82
     *
83
     * @since 0.3.0
84
     *
85
     * @param mixed $_ Array with configuration values.
86
     *
87
     * @return ConfigInterface Instance of a ConfigInterface implementation.
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Config.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
88
     */
89 3
    public static function create($_)
0 ignored issues
show
Unused Code introduced by
The parameter $_ is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91 3
        if (func_num_args() < 1) {
92
            return new Config([]);
93
        }
94
95 3
        $arguments = func_get_args();
96
97 3
        if (is_array($arguments[0]) && func_num_args() === 1) {
98 1
            return self::createFromArray($arguments[0]);
99
        }
100
101 2
        return self::createFromFile($arguments);
102
    }
103
}
104