Url   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setOption() 0 5 1
A html() 0 4 1
A __toString() 0 4 1
1
<?php
2
/**
3
 * Webino™ (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino for the canonical source repository
6
 * @copyright   Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoAppLib\Router;
12
13
use WebinoHtmlLib\Html;
14
use Zend\Mvc\Router\RouteStackInterface;
15
16
/**
17
 * Class Url
18
 */
19
final class Url implements UrlInterface
20
{
21
    /**
22
     * @var RouteStackInterface
23
     */
24
    private $router;
25
26
    /**
27
     * @var array
28
     */
29
    private $params = [];
30
31
    /**
32
     * @var array
33
     */
34
    private $options = [];
35
36
    /**
37
     * @param RouteStackInterface $router
38
     * @param array $params
39
     * @param array $options
40
     */
41
    public function __construct(RouteStackInterface $router, array $params, array $options)
42
    {
43
        $this->router  = $router;
44
        $this->params  = $params;
45
        $this->options = $options;
46
    }
47
48
    /**
49
     * @param string $name
50
     * @param string $value
51
     * @return $this
52
     */
53
    public function setOption($name, $value)
54
    {
55
        $this->options[$name] = $value;
56
        return $this;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function html($label = '')
63
    {
64
        return new Html\Url($this->__toString(), $label);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \WebinoHtmlLi...>__toString(), $label); (WebinoHtmlLib\Html\Url) is incompatible with the return type declared by the interface WebinoAppLib\Router\UrlInterface::html of type WebinoHtmlLib\Html\UrlInterface.

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...
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function __toString()
71
    {
72
        return $this->router->assemble($this->params, $this->options);
73
    }
74
}
75