Completed
Push — master ( a5657d...420c94 )
by Mark
30s queued 11s
created

ViewVarsTrait::createView()   C

Complexity

Conditions 14
Paths 216

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 216
nop 1
dl 0
loc 54
rs 5.2333
c 0
b 0
f 0

How to fix   Long Method    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
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright     Copyright (c), Cake Software Foundation, Inc. (https://cakefoundation.org)
10
 * @link          https://cakephp.org CakePHP(tm) Project
11
 * @since         3.0.0
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
namespace Cake\View;
15
16
use Cake\Event\EventDispatcherInterface;
17
18
/**
19
 * Provides the set() method for collecting template context.
20
 *
21
 * Once collected context data can be passed to another object.
22
 * This is done in Controller, TemplateTask and View for example.
23
 *
24
 * @property array $_validViewOptions
25
 */
26
trait ViewVarsTrait
27
{
28
29
    /**
30
     * The name of default View class.
31
     *
32
     * @var string|null
33
     * @deprecated 3.1.0 Use `$this->viewBuilder()->getClassName()`/`$this->viewBuilder()->setClassName()` instead.
34
     */
35
    public $viewClass;
36
37
    /**
38
     * Variables for the view
39
     *
40
     * @var array
41
     * @deprecated 3.7.0 Use `$this->set()` instead, also see `$this->viewBuilder()->getVar()`.
42
     */
43
    public $viewVars = [];
44
45
    /**
46
     * The view builder instance being used.
47
     *
48
     * @var \Cake\View\ViewBuilder
49
     */
50
    protected $_viewBuilder;
51
52
    /**
53
     * Get the view builder being used.
54
     *
55
     * @return \Cake\View\ViewBuilder
56
     */
57
    public function viewBuilder()
58
    {
59
        if (!isset($this->_viewBuilder)) {
60
            $this->_viewBuilder = new ViewBuilder();
61
        }
62
63
        return $this->_viewBuilder;
64
    }
65
66
    /**
67
     * Constructs the view class instance based on the current configuration.
68
     *
69
     * @param string|null $viewClass Optional namespaced class name of the View class to instantiate.
70
     * @return \Cake\View\View
71
     * @throws \Cake\View\Exception\MissingViewException If view class was not found.
72
     */
73
    public function createView($viewClass = null)
74
    {
75
        $builder = $this->viewBuilder();
76
        if ($viewClass === null && $builder->getClassName() === null) {
77
            $builder->setClassName($this->viewClass);
0 ignored issues
show
Deprecated Code introduced by
The property Cake\View\ViewVarsTrait::$viewClass has been deprecated with message: 3.1.0 Use `$this->viewBuilder()->getClassName()`/`$this->viewBuilder()->setClassName()` instead.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
78
            $this->viewClass = null;
0 ignored issues
show
Deprecated Code introduced by
The property Cake\View\ViewVarsTrait::$viewClass has been deprecated with message: 3.1.0 Use `$this->viewBuilder()->getClassName()`/`$this->viewBuilder()->setClassName()` instead.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
79
        }
80
        if ($viewClass) {
81
            $builder->setClassName($viewClass);
82
        }
83
84
        $validViewOptions = isset($this->_validViewOptions) ? $this->_validViewOptions : [];
85
        $viewOptions = [];
86
        foreach ($validViewOptions as $option) {
87
            if (property_exists($this, $option)) {
88
                $viewOptions[$option] = $this->{$option};
89
            }
90
        }
91
92
        $deprecatedOptions = [
93
            'layout' => 'setLayout',
94
            'view' => 'setTemplate',
95
            'theme' => 'setTheme',
96
            'autoLayout' => 'enableAutoLayout',
97
            'viewPath' => 'setTemplatePath',
98
            'layoutPath' => 'setLayoutPath',
99
        ];
100
        foreach ($deprecatedOptions as $old => $new) {
101
            if (property_exists($this, $old)) {
102
                $builder->{$new}($this->{$old});
103
                $message = sprintf(
104
                    'Property $%s is deprecated. Use $this->viewBuilder()->%s() instead in beforeRender().',
105
                    $old,
106
                    $new
107
                );
108
                deprecationWarning($message);
109
            }
110
        }
111
112
        foreach (['name', 'helpers', 'plugin'] as $prop) {
113
            if (isset($this->{$prop})) {
114
                $method = 'set' . ucfirst($prop);
115
                $builder->{$method}($this->{$prop});
116
            }
117
        }
118
        $builder->setOptions($viewOptions);
119
120
        return $builder->build(
121
            $this->viewVars,
0 ignored issues
show
Deprecated Code introduced by
The property Cake\View\ViewVarsTrait::$viewVars has been deprecated with message: 3.7.0 Use `$this->set()` instead, also see `$this->viewBuilder()->getVar()`.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
122
            isset($this->request) ? $this->request : null,
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
123
            isset($this->response) ? $this->response : null,
0 ignored issues
show
Bug introduced by
The property response does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
124
            $this instanceof EventDispatcherInterface ? $this->getEventManager() : null
0 ignored issues
show
Bug introduced by
The method getEventManager() does not exist on Cake\Event\EventDispatcherInterface. Did you maybe mean eventManager()?

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
    }
127
128
    /**
129
     * Saves a variable or an associative array of variables for use inside a template.
130
     *
131
     * @param string|array $name A string or an array of data.
132
     * @param mixed $value Value in case $name is a string (which then works as the key).
133
     *   Unused if $name is an associative array, otherwise serves as the values to $name's keys.
134
     * @return $this
135
     */
136
    public function set($name, $value = null)
137
    {
138
        if (is_array($name)) {
139
            if (is_array($value)) {
140
                $data = array_combine($name, $value);
141
            } else {
142
                $data = $name;
143
            }
144
        } else {
145
            $data = [$name => $value];
146
        }
147
        $this->viewVars = $data + $this->viewVars;
0 ignored issues
show
Deprecated Code introduced by
The property Cake\View\ViewVarsTrait::$viewVars has been deprecated with message: 3.7.0 Use `$this->set()` instead, also see `$this->viewBuilder()->getVar()`.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
148
149
        return $this;
150
    }
151
152
    /**
153
     * Get/Set valid view options in the object's _validViewOptions property. The property is
154
     * created as an empty array if it is not set. If called without any parameters it will
155
     * return the current list of valid view options. See `createView()`.
156
     *
157
     * @param string|array|null $options string or array of string to be appended to _validViewOptions.
158
     * @param bool $merge Whether to merge with or override existing valid View options.
159
     *   Defaults to `true`.
160
     * @return array The updated view options as an array.
161
     * @deprecated 3.7.0 Use ViewBuilder::setOptions() or any one of it's setter methods instead.
162
     */
163
    public function viewOptions($options = null, $merge = true)
164
    {
165
        deprecationWarning(
166
            'ViewVarsTrait::viewOptions() is deprecated, used ViewBuilder::setOptions() instead.'
167
        );
168
169
        if (!isset($this->_validViewOptions)) {
170
            $this->_validViewOptions = [];
171
        }
172
173
        if ($options === null) {
174
            return $this->_validViewOptions;
175
        }
176
177
        if (!$merge) {
178
            return $this->_validViewOptions = (array)$options;
179
        }
180
181
        return $this->_validViewOptions = array_merge($this->_validViewOptions, (array)$options);
182
    }
183
}
184