AppController::initialize()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 58
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 58
rs 9.639
cc 2
eloc 40
nc 1
nop 0

How to fix   Long Method   

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
2
namespace App\Controller;
3
4
use App\I18n\Language;
5
use Cake\Controller\Controller;
6
use Cake\Core\Configure;
7
use Cake\Event\Event;
8
9
class AppController extends Controller
10
{
11
12
    /**
13
     * Initialization hook method.
14
     *
15
     * @return void
16
     */
17
    public function initialize()
18
    {
19
        parent::initialize();
20
21
        //Components.
22
        $this->loadComponent('Flash');
23
        $this->loadComponent('Cookie');
24
        $this->loadComponent('Acl.Acl');
25
        $this->loadComponent('Maintenance');
26
        $this->loadComponent('CookieLogin');
27
        $this->loadComponent('SessionsActivity');
28
        $this->loadComponent('Auth', [
29
            'className' => 'AclAuth',
30
            'allowedActionsForBanned' => [
31
                'Pages' => [
32
                    'home'
33
                ]
34
            ],
35
            'authenticate' => [
36
                'Form',
37
                'Xety/Cake3CookieAuth.Cookie'
38
            ],
39
            'flash' => [
40
                'element' => 'error',
41
                'key' => 'flash',
42
                'params' => [
43
                    'class' => 'error'
44
                ]
45
            ],
46
            'authorize' => [
47
                'Acl.Actions' => [
48
                    'actionPath' => 'app/'
49
                ]
50
            ],
51
            'loginAction' => [
52
                'controller' => 'users',
53
                'action' => 'login',
54
                'prefix' => false
55
            ],
56
            'unauthorizedRedirect' => [
57
                'controller' => 'pages',
58
                'action' => 'home',
59
                'prefix' => false
60
            ],
61
            'loginRedirect' => [
62
                'controller' => 'pages',
63
                'action' => 'home'
64
            ],
65
            'logoutRedirect' => [
66
                'controller' => 'pages',
67
                'action' => 'home'
68
            ]
69
        ]);
70
71
        $this->loadComponent('Csrf', [
72
            'secure' => env('HTTPS') ? true : false
73
        ]);
74
    }
75
76
    /**
77
     * beforeFilter handle.
78
     *
79
     * @param Event $event The beforeFilter event that was fired.
80
     *
81
     * @return void
82
     */
83
    public function beforeFilter(Event $event)
84
    {
85
        $this->loadModel('Settings');
86
        $this->Settings->setSettings();
0 ignored issues
show
Documentation introduced by
The property Settings does not exist on object<App\Controller\AppController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
87
88
        $this->Auth->config('authError', __('You need to be logged in or you are not authorized to access that location !'));
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Core\InstanceConfigTrait::config() has been deprecated with message: 3.4.0 use setConfig()/getConfig() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
89
90
        // Define the language
91
        $language = new Language($this);
92
        $language->setLanguage();
93
94
        // Set trustProxy to get the original visitor IP
95
        $this->request->trustProxy = true;
96
97
        // Cookie Login (Automatically Login)
98
        $this->CookieLogin->handle();
0 ignored issues
show
Bug introduced by
The property CookieLogin does not seem to exist. Did you mean Cookie?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
99
100
        // Layouts
101
        if (!is_null($this->request->getParam('prefix'))) {
102
            $prefix = explode('/', $this->request->getParam('prefix'))[0];
103
104
            switch ($prefix) {
105
                case 'admin':
106
                    $this->viewBuilder()->layout('admin');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\View\ViewBuilder::layout() has been deprecated with message: 3.4.0 Use setLayout()/getLayout() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
107
                    break;
108
            }
109
        }
110
111
        // Cookies
112
        $allowCookies = $this->Cookie->check('allowCookies');
113
        $this->set(compact('allowCookies'));
114
115
        // Maintenance
116
        $this->Maintenance->handle();
0 ignored issues
show
Documentation introduced by
The property Maintenance does not exist on object<App\Controller\AppController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117
118
        // JavaScript Notifications
119
        if ($this->request->session()->read('Notification') && !empty($this->request->session()->read('Notification'))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->request->session()->read('Notification') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
120
            $notification = $this->request->session()->read('Notification');
121
            $this->request->session()->delete('Notification');
122
123
            $this->set(compact('notification'));
124
        }
125
    }
126
127
    /**
128
     * beforeRender hook method.
129
     *
130
     * @param Event $event The beforeRender event that was fired.
131
     *
132
     * @return void
133
     */
134
    public function beforeRender(Event $event)
135
    {
136
        parent::beforeRender($event);
137
138
        if ($this->components()->has('Auth')) {
139
            $builder = $this->viewBuilder();
140
            $builder->helpers([
0 ignored issues
show
Deprecated Code introduced by
The method Cake\View\ViewBuilder::helpers() has been deprecated with message: 3.4.0 Use setHelpers()/getHelpers() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
141
                'Acl' => $this->Auth->config('authorize')['Acl.Actions']
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Core\InstanceConfigTrait::config() has been deprecated with message: 3.4.0 use setConfig()/getConfig() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
142
            ]);
143
        }
144
    }
145
}
146