Completed
Push — master ( 5591c4...e530a3 )
by Mikael
02:52
created

CDIFactoryDefault   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 192
Duplicated Lines 10.42 %

Coupling/Cohesion

Components 0
Dependencies 15

Test Coverage

Coverage 26.67%

Importance

Changes 13
Bugs 1 Features 2
Metric Value
c 13
b 1
f 2
dl 20
loc 192
ccs 32
cts 120
cp 0.2667
rs 9.1666
wmc 4
lcom 0
cbo 15

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadFile() 0 15 3
B __construct() 20 158 1

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
/**
6
 * Anax base class implementing Dependency Injection / Service Locator
7
 * of the services used by the framework, using lazy loading.
8
 */
9
class CDIFactoryDefault extends CDI
10
{
11
    /**
12
     * Load config file from ANAX_APP_PATH or ANAX_INSTALL_PATH.
13
     *
14
     * @param string $filename to load.
15
     *
16
     * @throws Exception when $filenam is not found.
17
     *
18
     * @return void
19
     */
20 1
    public function loadFile($filename)
21
    {
22 1
        $pathInstall = ANAX_INSTALL_PATH . "/config/$filename";
23 1
        $pathApp = ANAX_APP_PATH . "/config/$filename";
24
        
25 1
        if (is_readable($pathApp)) {
26
            require $pathApp;
27
            return;
28 1
        } elseif (is_readable($pathInstall)) {
29 1
            require $pathInstall;
30 1
            return;
31
        }
32
33
        throw new Exception("Configure item '$filename' is not a readable file.");
34
    }
35
36
37
38
   /**
39
     * Construct.
40
     *
41
     */
42 1
    public function __construct()
43
    {
44 1
        parent::__construct();
45
46 1
        $this->loadFile("error_reporting.php");
47
48 1
        $this->setShared("response", "\Anax\Response\CResponseBasic");
49 1
        $this->setShared("validate", "\Anax\Validate\CValidate");
50 1
        $this->setShared("flash", "\Anax\Flash\CFlashBasic");
51
        
52 1
        $this->set("route", "\Anax\Route\CRouteBasic");
53 1
        $this->set("view", "\Anax\View\CView");
54
55
        $this->set("ErrorController", function () {
56
            $controller = new \Anax\MVC\ErrorController();
57
            $controller->setDI($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Anax\DI\CDIFactoryDefault>, but the function expects a object<Anax\DI\class>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
            return $controller;
59 1
        });
60
61
        $this->setShared("log", function () {
62
            $log = new \Anax\Log\CLogger();
63
            $log->setContext("development");
64
            return $log;
65 1
        });
66
67
        $this->setShared("cache", function () {
68
            $cache = new \Anax\Cache\CFileCache();
69
            $cache->configure("cache.php");
70
            return $cache;
71 1
        });
72
73
        $this->setShared("request", function () {
74
            $request = new \Anax\Request\CRequestBasic();
75
            $request->init();
76
            return $request;
77 1
        });
78
79
        $this->setShared("url", function () {
80
            $url = new \Anax\Url\CUrl();
81
            $url->setSiteUrl($this->request->getSiteUrl());
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Anax\DI\CDIFactoryDefault>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
82
            $url->setBaseUrl($this->request->getBaseUrl());
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Anax\DI\CDIFactoryDefault>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
83
            $url->setStaticSiteUrl($this->request->getSiteUrl());
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Anax\DI\CDIFactoryDefault>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
84
            $url->setStaticBaseUrl($this->request->getBaseUrl());
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Anax\DI\CDIFactoryDefault>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
85
            $url->setScriptName($this->request->getScriptName());
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Anax\DI\CDIFactoryDefault>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
86
            $url->setUrlType($url::URL_APPEND);
87
            return $url;
88 1
        });
89
90
        $this->setShared("views", function () {
91
            $views = new \Anax\View\CViewContainer();
92
            $views->configure("views.php");
93
            $views->setDI($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Anax\DI\CDIFactoryDefault>, but the function expects a object<Anax\DI\class>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
            return $views;
95 1
        });
96
97
        $this->setShared("router", function () {
98
            
99
            $router = new \Anax\Route\CRouterBasic();
100
            $router->setDI($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Anax\DI\CDIFactoryDefault>, but the function expects a object<Anax\DI\class>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
102 View Code Duplication
            $router->addInternal("403", function () {
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...
103
                $this->dispatcher->forward([
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Anax\DI\CDIFactoryDefault>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104
                    "controller" => "error",
105
                    "action" => "statusCode",
106
                    "params" => [
107
                        "code" => 403,
108
                        "message" => "HTTP Status Code 403: This is a forbidden route.",
109
                    ],
110
                ]);
111
            })->setName("403");
112
            
113
            $router->addInternal("404", function () {
114
                $this->dispatcher->forward([
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Anax\DI\CDIFactoryDefault>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
115
                    "controller" => "error",
116
                    "action" => "statusCode",
117
                    "params" => [
118
                        "code" => 404,
119
                        "message" => "HTTP Status Code 404: This route is not found.",
120
                    ],
121
                ]);
122
                $this->dispatcher->forward([
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Anax\DI\CDIFactoryDefault>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
123
                    "controller" => "error",
124
                    "action" => "displayValidRoutes",
125
                ]);
126
            })->setName("404");
127
            
128 View Code Duplication
            $router->addInternal("500", function () {
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...
129
                $this->dispatcher->forward([
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Anax\DI\CDIFactoryDefault>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
130
                    "controller" => "error",
131
                    "action" => "statusCode",
132
                    "params" => [
133
                        "code" => 500,
134
                        "message" => "HTTP Status Code 500: There was an internal server or processing error.",
135
                    ],
136
                ]);
137
            })->setName("500");
138
            
139
            return $router;
140 1
        });
141
142
        $this->setShared("dispatcher", function () {
143
            $dispatcher = new \Anax\MVC\CDispatcherBasic();
144
            $dispatcher->setDI($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Anax\DI\CDIFactoryDefault>, but the function expects a object<Anax\DI\class>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
145
            return $dispatcher;
146 1
        });
147
148
        $this->setShared("session", function () {
149
            $session = new \Anax\Session\CSession();
150
            $session->configure("session.php");
151
            $session->name();
152
            $session->start();
153
            return $session;
154 1
        });
155
156
        $this->setShared("theme", function () {
157
            $themeEngine = new \Anax\ThemeEngine\CThemeEngine();
158
            $themeEngine->setDI($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Anax\DI\CDIFactoryDefault>, but the function expects a object<Anax\DI\class>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
159
            $themeEngine->configure("theme.php");
160
            return $themeEngine;
161 1
        });
162
163
        $this->setShared("navbar", function () {
164
            $navbar = new \Anax\Navigation\CNavbar();
165
            $navbar->setDI($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Anax\DI\CDIFactoryDefault>, but the function expects a object<Anax\DI\class>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
166
            $navbar->configure("navbar.php");
167
            return $navbar;
168 1
        });
169
170
        $this->set("fileContent", function () {
171
            $fc = new \Anax\Content\CFileContent();
172
            $fc->setDI($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Anax\DI\CDIFactoryDefault>, but the function expects a object<Anax\DI\class>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
173
            $fc->configure("file_content.php");
174
            return $fc;
175 1
        });
176
177
        $this->set("pageContent", function () {
178
            $pc = new \Anax\Content\CPageContent();
179
            $pc->setDI($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Anax\DI\CDIFactoryDefault>, but the function expects a object<Anax\DI\class>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
180
            $pc->configure("page_content.php");
181
            return $pc;
182 1
        });
183
184
        $this->setShared("content", function () {
185
            $content = new \Anax\Content\CFileBasedContent();
186
            $content->setDI($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Anax\DI\CDIFactoryDefault>, but the function expects a object<Anax\DI\class>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
187
            $content->configure("content.php");
188
            $content->setDefaultsFromConfiguration();
189 1
            return $content;
190
        });
191 1
192
        $this->setShared("textFilter", function () {
193
            //$filter = new \Anax\Content\CTextFilter();
194
            $filter = new \Mos\TextFilter\CTextFilter();
195
            //$filter->setDI($this);
196
            //$filter->configure(ANAX_APP_PATH . "/config/text_filter.php");
197 1
            return $filter;
198 1
        });
199
    }
200
}
201