Test Failed
Push — master ( 0ee32e...41c938 )
by Dan
07:10
created

FileLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 14.08 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 10
loc 71
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A loadFiles() 10 10 3
B loadFile() 0 23 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
 * This file is part of the PSR Http Framework.
4
 *
5
 * (c) Dan Smith <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Ds\Router\Loaders;
11
12
use Ds\Router\Exceptions\RouterException;
13
use Ds\Router\Interfaces\RouteCollectionInterface;
14
use Ds\Router\Interfaces\RouterInterface;
15
16
/**
17
 * Class FileLoader
18
 *
19
 * @package Ds\Router\Loaders
20
 * @author  Dan Smith    <[email protected]>
21
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
22
 * @link    https://github.com/djsmithme/Router
23
 */
24
class FileLoader extends AbstractLoader
25
{
26
    /**
27
     * File Loader Default settings
28
     *
29
     * @var array
30
     */
31
    public static $defaults = [
32
        'vars' => []
33
    ];
34
35
    /**
36
     * File Loader Options
37
     *
38
     * @var array
39
     */
40
    protected $options;
41
42
    /**
43
     * FileLoader constructor.
44
     *
45
     * @param RouterInterface $router Routing Component
46
     * @param array $options File Loader Options.
47
     */
48
    public function __construct(RouterInterface $router, array $options = [])
49
    {
50
        $this->options = \array_replace_recursive(FileLoader::$defaults, $options);
51
        $this->router = $router;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 View Code Duplication
    public function loadFiles(array $files)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
59
        if (!$this->router->isCached()) {
60
            foreach ((array)$files as $filename) {
61
                $collection = $this->loadFile($filename);
62
                $this->router = $this->router->mergeCollection($collection);
63
            }
64
        }
65
        return $this->router;
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function loadFile(string $file)
72
    {
73
        if (!$this->router->isCached()) {
74
            foreach ((array)$this->options['vars'] as $key => $item) {
75
                if (\is_string($key)) {
76
                    ${$key} = $item;
77
                }
78
            }
79
80
            if (!\file_exists($file)) {
81
                throw new RouterException("Could not locate route file.'{$file}'");
82
            }
83
84
            $routeData = require $file;
85
86
            if ($routeData instanceof RouteCollectionInterface === false) {
87
                throw new RouterException("'{$file}' must return an instance of Rs\\Router\\RouteCollectionInterface");
88
            }
89
90
            return $routeData;
91
        }
92
        return $this->router->getCollection();
93
    }
94
}
95