Completed
Push — master ( a44dc4...3d9fad )
by Derek Stephen
49:27 queued 30:15
created

IndexController::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace App\Controller;
4
5
use Bone\Mvc\Controller;
6
use Bone\Mvc\Registry;
7
use Zend\Diactoros\Response;
8
9
10
class IndexController extends Controller
11
{
12
    private $locale;
13
14 1
    public function init()
15
    {
16 1
        $this->locale = $this->view->locale = $this->getParam('locale', Registry::ahoy()->get('i18n')['default_locale']);
17 1
        $this->getTranslator()->setLocale($this->locale);
0 ignored issues
show
Bug introduced by
The method getTranslator() does not seem to exist on object<App\Controller\IndexController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
18 1
    }
19
20 2
    public function indexAction()
21
    {
22 2
        if (!$this->getParam('locale')) {
23 1
            return new Response\RedirectResponse('/' . $this->locale);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Zend\Diactor...e('/' . $this->locale); (Zend\Diactoros\Response\RedirectResponse) is incompatible with the return type of the parent method Bone\Mvc\Controller::indexAction of type array<string,string>.

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...
24
        }
25 2
    }
26
27 1
    public function learnAction()
28
    {
29
30 1
    }
31
32 1
    public function jsonAction()
33
    {
34
        // example of a Json page
35
        $array = array(
36 1
          'Rum' => 'tasty',
37
          'Grog' => 'the best!',
38
        );
39 1
        $this->sendJsonResponse($array);
40 1
    }
41
}
42