Completed
Pull Request — master (#165)
by Paul
03:21
created

LaravelRoutesLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 14.63 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 4
c 4
b 0
f 0
lcom 1
cbo 0
dl 6
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 6 21 3

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 PPI Framework.
4
 *
5
 * @copyright  Copyright (c) 2011-2016 Paul Dragoonis <[email protected]>
6
 * @license    http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 * @link       http://www.ppi.io
9
 */
10
11
namespace PPI\Framework\Router\Loader;
12
13
use Illuminate\Routing\Router;
14
use Illuminate\Routing\Router as LaravelRouter;
15
16
/**
17
 * LaravelRoutesLoader class.
18
 *
19
 * @author Paul Dragoonis <[email protected]>
20
 */
21
class LaravelRoutesLoader
22
{
23
    /**
24
     * @var LaravelRouter
25
     */
26
    protected $router;
27
28
    public function __construct(LaravelRouter $router)
29
    {
30
        $this->router = $router;
31
    }
32
33
    /**
34
     * @param string $path
35
     *
36
     * @throws \Exception
37
     *
38
     * @return LaravelRouter
39
     */
40
    public function load($path)
41
    {
42
        if (!is_readable($path)) {
43
            throw new \InvalidArgumentException('Invalid laravel routes path found: ' . $path);
44
        }
45
46
        // localising the object so the $path file can reference $router;
47
        $router = $this->router;
48
49
        // The included file must return the laravel router
50
        include $path;
51
52 View Code Duplication
        if (!($router instanceof LaravelRouter)) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Routing\Router does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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...
53
            throw new \Exception('Invalid return value from '
54
                . pathinfo($path, PATHINFO_FILENAME)
55
                . ' expected instance of LaravelRouter'
56
            );
57
        }
58
59
        return $router;
60
    }
61
}
62