Completed
Pull Request — master (#494)
by Thomas Mauro
56:38 queued 44:43
created

YumlController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 40
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 17 2
A __construct() 0 4 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModule\Yuml;
21
22
use Zend\Mvc\Controller\AbstractActionController;
23
use Zend\Http\Client;
24
use Zend\Http\Request;
25
26
/**
27
 * Utility to generate Yuml compatible strings from metadata graphs
28
 *
29
 * @license MIT
30
 * @link    http://www.doctrine-project.org/
31
 * @author  Marco Pivetta <[email protected]>
32
 */
33
class YumlController extends AbstractActionController
34
{
35
    /**
36
     * @var Client
37
     */
38
    protected $httpClient;
39
40
    /**
41
     * @param Client $httpClient
42
     */
43 1
    public function __construct(Client $httpClient)
44
    {
45 1
        $this->httpClient = $httpClient;
46 1
    }
47
48
    /**
49
     * Redirects the user to a YUML graph drawn with the provided `dsl_text`
50
     *
51
     * @return \Zend\Http\Response
52
     *
53
     * @throws \UnexpectedValueException if the YUML service answered incorrectly
54
     */
55 2
    public function indexAction()
56
    {
57
        /* @var $request \Zend\Http\Request */
58 2
        $request = $this->getRequest();
59 2
        $this->httpClient->setMethod(Request::METHOD_POST);
60 2
        $this->httpClient->setParameterPost(array('dsl_text' => $request->getPost('dsl_text')));
61 2
        $response = $this->httpClient->send();
62
63 2
        if (!$response->isSuccess()) {
64 1
            throw new \UnexpectedValueException('HTTP Request failed');
65
        }
66
67
        /* @var $redirect \Zend\Mvc\Controller\Plugin\Redirect */
68 1
        $redirect = $this->plugin('redirect');
69
70 1
        return $redirect->toUrl('http://yuml.me/' . $response->getBody());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $redirect->toUrl(... $response->getBody()); (Zend\Http\Response) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...Controller::indexAction of type Zend\View\Model\ViewModel.

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...
71
    }
72
}
73