Completed
Push — master ( 56e75a...611edb )
by Mikael
01:56
created

DIFactoryConfig   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 104
Duplicated Lines 11.54 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 12
loc 104
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B loadServices() 12 34 10
A createServicesFromArray() 0 16 4
A createService() 0 16 6

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\DI;
4
5
use Anax\DI\Exception\Exception;
6
use Psr\Container\ContainerInterface;
7
8
/**
9
 * DI factory class creating a set of default services by loading
10
 * them from a configuration array, file and/or directory.
11
 *
12
 * The general configuration array for a service looks like this.
13
 * [
14
 *     "services" => [
15
 *         "request" => [
16
 *             "active" => false,
17
 *             "shared" => false,
18
 *             "callback" => function () {
19
 *                 $object = new \stdClass();
20
 *                 return $object;
21
 *             }
22
 *         ],
23
 *     ],
24
 * ]
25
 */
26
class DIFactoryConfig extends DI implements ContainerInterface
27
{
28
    /**
29
     * Create services by using $item as a reference to find a
30
     * configuration for the services. The $item can be an array,
31
     * a file.php, or an directory containing files named *.php.
32
     *
33
     * @param array|string $item referencing the source for configuration.
34
     *
35
     * @return $this
36
     */
37 7
    public function loadServices($item) : object
38
    {
39 7
        if (is_array($item)) {
40 3
            $this->createServicesFromArray($item, "array");
41 1
            return $this;
42
        }
43
44 4
        if (is_readable($item) && is_file($item)) {
45 1
            $services = require $item;
46 1
            $this->createServicesFromArray($services, $item);
47 1
            return $this;
48
        }
49
50 3
        $found = false;
51 3 View Code Duplication
        if (is_readable("$item.php") && is_file("$item.php")) {
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...
52 2
            $services = require "$item.php";
53 2
            $this->createServicesFromArray($services, $item);
54 2
            $found = true;
55
        }
56
    
57 3 View Code Duplication
        if (is_readable($item) && is_dir($item)) {
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...
58 2
            foreach (glob("$item/*.php") as $file) {
59 2
                $services = require "$file";
60 2
                $this->createServicesFromArray($services, $file);
61 2
                $found = true;
62
            }
63
        }
64
65 3
        if (!$found) {
66 1
            throw new Exception("Item to load configuration from was no file, dir nor an array.");
67
        }
68
69 2
        return $this;
70
    }
71
72
73
74
    /**
75
     * Create services from an array containing a list of services.
76
     *
77
     * @param array  $service details to use when creating the service.
0 ignored issues
show
Documentation introduced by
There is no parameter named $service. Did you maybe mean $services?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
78
     *
79
     * @throws Exception when configuration is corrupt.
80
     *
81
     * @return void
82
     */
83 6
    protected function createServicesFromArray(
84
        array $services,
85
        string $path
86
    ) : void {
87 6
        if (!isset($services["services"])) {
88 1
            throw new Exception("The configuration array is missing the key 'services' in file '$path'.");
89
        }
90
91 5
        foreach ($services["services"] as $name => $service) {
92
            try {
93 5
                $this->createService($name, $service);
94 1
            } catch (Exception $e) {
95 5
                throw new Exception($e->getMessage() . " In configuration file '$path'.");
96
            }
97
        }
98 4
    }
99
100
101
102
    /**
103
     * Create a service from a name and an array containing details on
104
     * how to create it.
105
     *
106
     * @param string $name    of service.
107
     * @param array  $service details to use when creating the service.
108
     *
109
     * @throws Exception when configuration is corrupt.
110
     *
111
     * @return void
112
     */
113 5
    protected function createService(string $name, array $service) : void
114
    {
115 5
        if (!isset($service["callback"])) {
116 1
            throw new Exception("The service '$name' is missing a callback.");
117
        }
118
119 4
        if (isset($service["shared"]) && $service["shared"]) {
120 3
            $this->setShared($name, $service["callback"]);
121
        } else {
122 4
            $this->set($name, $service["callback"]);
123
        }
124
125 4
        if (isset($service["active"]) && $service["active"]) {
126 1
            $this->get($name);
127
        }
128 4
    }
129
}
130