ViewController::action()   D
last analyzed

Complexity

Conditions 11
Paths 170

Size

Total Lines 49
Code Lines 29

Duplication

Lines 7
Ratio 14.29 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 7
loc 49
c 1
b 0
f 0
rs 4.9629
cc 11
eloc 29
nc 170
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php /** MicroController */
2
3
namespace Micro\Mvc\Controllers;
4
5
use Micro\Base\Exception;
6
use Micro\Web\ResponseInjector;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Class Controller
11
 *
12
 * @author Oleg Lunegov <[email protected]>
13
 * @link https://github.com/linpax/microphp-framework
14
 * @copyright Copyright (c) 2013 Oleg Lunegov
15
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
16
 * @package Micro
17
 * @subpackage Mvc\Controllers
18
 * @version 1.0
19
 * @since 1.0
20
 */
21
abstract class ViewController extends Controller
22
{
23
    /** @var string $layout */
24
    public $layout;
25
    /** @var bool $asWidget */
26
    public $asWidget = false;
27
28
29
    /**
30
     * @inheritdoc
31
     * @throws Exception
32
     */
33
    public function action($name = 'index')
0 ignored issues
show
Coding Style introduced by
action uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
34
    {
35
        // Set widgetStack for widgets
36
        if (empty($GLOBALS['widgetStack'])) {
37
            $GLOBALS['widgetStack'] = [];
38
        }
39
40
        $actionClass = false;
41
42
        if (!method_exists($this, 'action'.ucfirst($name))) {
43
            $actionClass = $this->getActionClassByName($name);
44
45
            if (!$actionClass) {
46
                throw new Exception('Action `'.$name.'` not found into '.get_class($this));
47
            }
48
        }
49
50
        $filters = method_exists($this, 'filters') ? $this->filters() : [];
0 ignored issues
show
Bug introduced by
The method filters() does not exist on Micro\Mvc\Controllers\ViewController. Did you maybe mean applyFilters()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
51
        $result = $this->applyFilters($name, true, $filters, null);
52
53
        if ($result instanceof ResponseInterface) {
54
            return $result;
55
        }
56
57 View Code Duplication
        if ($actionClass) {
0 ignored issues
show
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...
58
            /** @var \Micro\mvc\Action $cl */
59
            $cl = new $actionClass();
60
            $view = $cl->run();
61
        } else {
62
            $view = $this->{'action'.ucfirst($name)}();
63
        }
64
65
        if (is_object($view)) {
66
            $view->module = get_class($this->module);
67
            $view->layout = $view->layout ?: $this->layout;
68
            $view->view = $view->view ?: $name;
69
            $view->path = get_called_class();
70
            $view = $view->render();
71
        }
72
73
        $response = (new ResponseInjector)->build();
74
        if (!$response) {
75
            throw new Exception('Component `response` not configured');
76
        }
77
        $stream = $response->getBody();
78
        $stream->write($this->applyFilters($name, false, $filters, $view));
0 ignored issues
show
Bug introduced by
It seems like $this->applyFilters($nam...false, $filters, $view) targeting Micro\Mvc\Controllers\Controller::applyFilters() can also be of type null or object<Psr\Http\Message\ResponseInterface>; however, Psr\Http\Message\StreamInterface::write() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
79
80
        return $response->withBody($stream);
81
    }
82
83
    /**
84
     * Redirect user to path
85
     *
86
     * @access public
87
     *
88
     * @param string $path path to redirect
89
     * @param integer $status status for redirect
90
     *
91
     * @return false|ResponseInterface
92
     * @throws Exception|\InvalidArgumentException
93
     */
94
    public function redirect($path, $status = 301)
95
    {
96
        if (!$this->asWidget) {
97
            /** @var ResponseInterface $response */
98
            $response = (new ResponseInjector)->build();
99
            if (!$response) {
100
                throw new Exception('Component `response` not configured');
101
            }
102
103
            $response = $response->withStatus($status);
104
            $response = $response->getHeaderLine('Location: '.$path);
105
106
            return $response;
107
        }
108
109
        return false;
110
    }
111
}
112