ConfigureTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 14
loc 70
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 6 2
B configure() 14 30 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Anax\Configure;
4
5
/**
6
 * Trait implementing reading from config-file and storing options in
7
 * $this->config.
8
 */
9
trait ConfigureTrait
10
{
11
    /**
12
     * @var [] $config store the configuration in this array.
0 ignored issues
show
Documentation introduced by
The doc-type [] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
13
     */
14
    protected $config = [];
15
16
17
18
    /**
19
     * Read configuration from file or array, if a file, first check in
20
     * ANAX_APP_PATH/config and then in ANAX_INSTALL_PATH/config.
21
     *
22
     * @param array|string $what is an array with key/value config options
23
     *                           or a file to be included which returns such
24
     *                           an array.
25
     *
26
     * @throws Exception when argument if not a file nor an array.
27
     *
28
     * @return self for chaining.
29
     */
30
    public function configure($what)
31
    {
32
        if (is_array($what)) {
33
            $this->config = $what;
34
            return $this;
35
        }
36
37 View Code Duplication
        if (defined("ANAX_APP_PATH")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
            $path = ANAX_APP_PATH . "/config/$what";
39
            if (is_readable($path)) {
40
                $this->config = require $path;
41
                return $this;
42
            }
43
        }
44
45 View Code Duplication
        if (defined("ANAX_INSTALL_PATH")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
            $path = ANAX_INSTALL_PATH . "/config/$what";
47
            if (is_readable($path)) {
48
                $this->config = require $path;
49
                return $this;
50
            }
51
        }
52
53
        if (is_readable($what)) {
54
            $this->config = require $what;
55
            return $this;
56
        }
57
58
        throw new Exception("Configure item '$what' is not an array nor a readable file.");
59
    }
60
61
62
63
    /**
64
     * Helper function for reading values from the configuration and appy
65
     * default values where configuration item is missing.
66
     *
67
     * @param string $key     matching a key in the config array.
68
     * @param string $default value returned when config item is not found.
69
     *
70
     * @return mixed or null if key does not exists.
71
     */
72
    public function getConfig($key, $default = null)
73
    {
74
        return isset($this->config[$key])
75
            ? $this->config[$key]
76
            : $default;
77
    }
78
}
79