Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

ViewController::redirect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
1
<?php /** MicroController */
2
3
namespace Micro\Mvc\Controllers;
4
5
use Micro\Base\Exception;
6
use Micro\Web\Response;
7
8
/**
9
 * Class Controller
10
 *
11
 * @author Oleg Lunegov <[email protected]>
12
 * @link https://github.com/lugnsk/micro
13
 * @copyright Copyright &copy; 2013 Oleg Lunegov
14
 * @license /LICENSE
15
 * @package Micro
16
 * @subpackage Mvc\Controllers
17
 * @version 1.0
18
 * @since 1.0
19
 */
20
abstract class ViewController extends Controller
21
{
22
    /** @var string $layout */
23
    public $layout;
24
    /** @var bool $asWidget */
25
    public $asWidget = false;
26
27
28
    /**
29
     * @inheritdoc
30
     * @throws Exception
31
     */
32
    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...
33
    {
34
        // Set widgetStack for widgets
35
        if (empty($GLOBALS['widgetStack'])) {
36
            $GLOBALS['widgetStack'] = [];
37
        }
38
39
        $view = null;
0 ignored issues
show
Unused Code introduced by
$view is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
        $actionClass = false;
41
42
43
        if (!method_exists($this, 'action' . ucfirst($name))) {
44
            $actionClass = $this->getActionClassByName($name);
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
52
        $this->applyFilters($name, true, $filters, null);
53
54 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...
55
            /** @var \Micro\mvc\Action $cl */
56
            $cl = new $actionClass($this->container);
57
            $view = $cl->run();
58
        } else {
59
            $view = $this->{'action' . ucfirst($name)}();
60
        }
61
62
        if (is_object($view)) {
63
            $view->module = get_class($this->module);
64
            $view->layout = $view->layout ?: $this->layout;
65
            $view->view = $view->view ?: $name;
66
            $view->path = get_called_class();
67
            $view = (string)$view;
68
        }
69
70
        $response = $this->container->response ?: new Response;
0 ignored issues
show
Bug introduced by
Accessing response on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
71
        $response->setBody($this->applyFilters($name, false, $filters, $view));
72
73
        return $response;
74
    }
75
76
    /**
77
     * Redirect user to path
78
     *
79
     * @access public
80
     *
81
     * @param string $path path to redirect
82
     *
83
     * @return void|bool
84
     */
85
    public function redirect($path)
86
    {
87
        if (!$this->asWidget) {
88
            header('Location: ' . $path);
89
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method redirect() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
90
        }
91
92
        return false;
93
    }
94
}
95