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

Controller::applyFilters()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 32
rs 4.8196
c 1
b 1
f 0
cc 10
eloc 17
nc 10
nop 4

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\Base\IContainer;
7
use Micro\Mvc\Module;
8
use Micro\Web\IResponse;
9
use Micro\Web\Response;
10
11
/**
12
 * Class Controller
13
 *
14
 * @author Oleg Lunegov <[email protected]>
15
 * @link https://github.com/lugnsk/micro
16
 * @copyright Copyright &copy; 2013 Oleg Lunegov
17
 * @license /LICENSE
18
 * @package Micro
19
 * @subpackage Mvc\Controllers
20
 * @version 1.0
21
 * @since 1.0
22
 * @abstract
23
 */
24
abstract class Controller implements IController
25
{
26
    /** @var Module $module */
27
    public $module;
28
    /** @var IResponse $response Response HTTP data */
29
    public $response;
30
    /** @var IContainer $container */
31
    protected $container;
32
33
    /**
34
     * Constructor controller
35
     *
36
     * @access public
37
     *
38
     * @param IContainer $container
39
     * @param string $modules
40
     *
41
     * @result void
42
     */
43
    public function __construct(IContainer $container, $modules = '')
44
    {
45
        $this->container = $container;
46
47
        // if module defined
48
        if ($modules) {
49
            $app = $this->container->kernel->getAppDir();
0 ignored issues
show
Bug introduced by
Accessing kernel 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...
50
51
            $path = $app . str_replace('\\', '/', $modules) . '/' .
52
                ucfirst(basename(str_replace('\\', '/', $modules))) . 'Module.php';
53
54
            // search module class
55
            if (file_exists($path)) {
56
                $path = substr(str_replace('/', '\\', str_replace($app, 'App', $path)), 0, -4);
57
                $this->module = new $path($this->container);
58
            }
59
        }
60
61
        if (!$this->response = $this->container->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...
62
            $this->response = new Response;
63
        }
64
    }
65
66
    /**
67
     * Apply filters
68
     *
69
     * @access public
70
     *
71
     * @param string $action current action name
72
     * @param bool   $isPre is pre or post
73
     * @param array  $filters defined filters
74
     * @param string $data data to parse
75
     *
76
     * @return null|string
77
     * @throws Exception
78
     */
79
    public function applyFilters($action, $isPre = true, array $filters = [], $data = null)
80
    {
81
        if (!$filters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
82
            return $data;
83
        }
84
85
        foreach ($filters AS $filter) {
86
            if (empty($filter['class']) || !class_exists($filter['class'])) {
87
                continue;
88
            }
89
90
            if (empty($filter['actions']) || !in_array($action, $filter['actions'], true)) {
91
                continue;
92
            }
93
94
            /** @var \Micro\Filter\IFilter $_filter */
95
            $_filter = new $filter['class']($action, $this->container);
96
97
            $res = $isPre ? $_filter->pre($filter) : $_filter->post($filter + ['data' => $data]);
98
            if (!$res) {
99
                if (!empty($_filter->result['redirect'])) {
0 ignored issues
show
Bug introduced by
Accessing result on the interface Micro\Filter\IFilter 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...
100
                    header('Location: ' . $_filter->result['redirect']);
0 ignored issues
show
Bug introduced by
Accessing result on the interface Micro\Filter\IFilter 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...
101
102
                    die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method applyFilters() 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...
103
                }
104
                throw new Exception($_filter->result['message']);
0 ignored issues
show
Bug introduced by
Accessing result on the interface Micro\Filter\IFilter 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...
105
            }
106
            $data = $res;
107
        }
108
109
        return $data;
110
    }
111
112
    /**
113
     * Get action class by name
114
     *
115
     * @access public
116
     *
117
     * @param string $name action name
118
     *
119
     * @return bool
120
     */
121
    public function getActionClassByName($name)
122
    {
123
        if (method_exists($this, 'actions')) {
124
            $actions = $this->actions();
0 ignored issues
show
Bug introduced by
The method actions() does not exist on Micro\Mvc\Controllers\Controller. Did you maybe mean action()?

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...
125
126
            if (!empty($actions[$name]) && class_exists($actions[$name])) {
127
                return $actions[$name];
128
            }
129
        }
130
131
        return false;
132
    }
133
}
134