GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RouteProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 71
c 0
b 0
f 0
ccs 0
cts 33
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getRouteCollectionForRequest() 0 17 3
A getRouteByName() 0 10 2
A getRoutesByNames() 0 8 3
1
<?php
2
3
/*
4
 * This file is part of the Tadcka package.
5
 *
6
 * (c) Tadas Gliaubicas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tadcka\Component\Routing\Provider;
13
14
use Symfony\Cmf\Component\Routing\Candidates\CandidatesInterface;
15
use Symfony\Cmf\Component\Routing\RouteProviderInterface;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\Routing\Exception\RouteNotFoundException;
18
use Symfony\Component\Routing\RouteCollection;
19
use Tadcka\Component\Routing\Model\Manager\RouteManagerInterface;
20
21
/**
22
 * @author Tadas Gliaubicas <[email protected]>
23
 *
24
 * @since 7/1/14 10:36 PM
25
 */
26
class RouteProvider implements RouteProviderInterface
27
{
28
    /**
29
     * @var RouteManagerInterface
30
     */
31
    private $routeManager;
32
33
    /**
34
     * @var CandidatesInterface
35
     */
36
    private $candidatesStrategy;
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param RouteManagerInterface $routeManager
42
     * @param CandidatesInterface $candidatesStrategy
43
     */
44
    public function __construct(RouteManagerInterface $routeManager, CandidatesInterface $candidatesStrategy)
45
    {
46
        $this->routeManager = $routeManager;
47
        $this->candidatesStrategy = $candidatesStrategy;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getRouteCollectionForRequest(Request $request)
54
    {
55
        $collection = new RouteCollection();
56
57
        $candidates = $this->candidatesStrategy->getCandidates($request);
58
59
        if (0 === count($candidates)) {
60
            return $collection;
61
        }
62
63
        $routes = $this->routeManager->findVisibleByRoutePatterns($candidates);
64
        foreach ($routes as $route) {
65
            $collection->add($route->getName(), $route);
66
        }
67
68
        return $collection;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getRouteByName($name)
75
    {
76
        $route = $this->routeManager->findVisibleByName($name);
77
78
        if (null === $route) {
79
            throw new RouteNotFoundException(sprintf("No route found for path %s", $name));
80
        }
81
82
        return $route;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $route; (Tadcka\Component\Routing\Model\RouteInterface) is incompatible with the return type declared by the interface Symfony\Cmf\Component\Ro...terface::getRouteByName of type Symfony\Component\Routing\Route.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getRoutesByNames($names)
89
    {
90
        if (null !== $names && is_array($names)) {
91
            return $this->routeManager->findVisibleByNames($names);
92
        }
93
94
        return array();
95
    }
96
}
97