GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( b678f0...05ab1f )
by Ivan
10:26
created

PageController::actionEdit()   D

Complexity

Conditions 16
Paths 61

Size

Total Lines 89
Code Lines 57

Duplication

Lines 9
Ratio 10.11 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 9
loc 89
rs 4.8736
cc 16
eloc 57
nc 61
nop 2

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
namespace app\modules\page\backend;
4
5
use app\backend\actions\PropertyHandler;
6
use app\backend\events\BackendEntityEditEvent;
7
use app\models\Object;
8
use app\models\ObjectPropertyGroup;
9
use app\models\Property;
10
use app\modules\image\models\Image;
11
use app\modules\image\widgets\views\AddImageAction;
12
use app\modules\page\models\Page;
13
use app\models\ViewObject;
14
use app\properties\HasProperties;
15
use app\modules\image\widgets\RemoveAction;
16
use app\modules\image\widgets\SaveInfoAction;
17
use app\modules\image\widgets\UploadAction;
18
use devgroup\JsTreeWidget\AdjacencyFullTreeDataAction;
19
use devgroup\JsTreeWidget\TreeNodeMoveAction;
20
use devgroup\JsTreeWidget\TreeNodesReorderAction;
21
use Yii;
22
use yii\db\Query;
23
use yii\filters\AccessControl;
24
use yii\helpers\ArrayHelper;
25
use yii\helpers\Url;
26
use yii\web\NotFoundHttpException;
27
use app\backend\actions\MassPublishAction;
28
29
class PageController extends \app\backend\components\BackendController
30
{
31
32
    const BACKEND_PAGE_EDIT = 'backend-page-edit';
33
    const BACKEND_PAGE_EDIT_SAVE = 'backend-page-edit-save';
34
    const BACKEND_PAGE_EDIT_FORM = 'backend-page-edit-form';
35
    const BACKEND_PAGE_AFTER_SAVE = 'backend-page-after-save';
36
37
    public function behaviors()
38
    {
39
        return [
40
            'access' => [
41
                'class' => AccessControl::className(),
42
                'rules' => [
43
                    [
44
                        'allow' => true,
45
                        'roles' => ['content manage'],
46
                    ],
47
                ],
48
            ],
49
        ];
50
    }
51
52
    public function actions()
53
    {
54
        return [
55
            'getTree' => [
56
                'class' => AdjacencyFullTreeDataAction::className(),
57
                'class_name' => Page::className(),
58
                'model_label_attribute' => 'name',
59
            ],
60
            'addImage' => [
61
                'class' => AddImageAction::className(),
62
            ],
63
            'upload' => [
64
                'class' => UploadAction::className(),
65
                'upload' => 'theme/resources/product-images',
66
            ],
67
            'remove' => [
68
                'class' => RemoveAction::className(),
69
                'uploadDir' => 'theme/resources/product-images',
70
            ],
71
            'save-info' => [
72
                'class' => SaveInfoAction::className(),
73
            ],
74
            'property-handler' => [
75
                'class' => PropertyHandler::className(),
76
                'modelName' => Page::className()
77
            ],
78
            'move' => [
79
                'class' => TreeNodeMoveAction::className(),
80
                'className' => Page::className(),
81
                'saveAttributes' => ['slug_compiled'],
82
            ],
83
            'reorder' => [
84
                'class' => TreeNodesReorderAction::className(),
85
                'className' => Page::className(),
86
            ],
87
            'publish-switch' => [
88
                'class' => MassPublishAction::className(),
89
                'modelName' => Page::className(),
90
                'attribute' => 'published',
91
            ]
92
        ];
93
    }
94
95 View Code Duplication
    public function actionIndex($parent_id = 1)
96
    {
97
        $searchModel = new Page();
98
        $searchModel->parent_id = $parent_id;
99
100
        $params = Yii::$app->request->get();
101
        $dataProvider = $searchModel->search($params);
102
103
        $model = null;
104
        if ($parent_id > 0) {
105
            $model = Page::findOne($parent_id);
106
        }
107
108
        return $this->render(
109
            'index',
110
            [
111
                'dataProvider' => $dataProvider,
112
                'searchModel' => $searchModel,
113
                'model' => $model,
114
            ]
115
        );
116
    }
117
118
    public function actionEdit($parent_id, $id = null)
119
    {
120
        $object = Object::getForClass(Page::className());
121
122
        /** @var null|Page|HasProperties $model */
123
        $model = new Page;
124
        $model->published = 1;
125
        if ($id !== null) {
126
            $model = Page::findOne($id);
127
            if ($model === null) {
128
                throw new NotFoundHttpException;
129
            }
130
        }
131
        $model->parent_id = $parent_id;
132
133
        $event = new BackendEntityEditEvent($model);
0 ignored issues
show
Bug introduced by
It seems like $model defined by \app\modules\page\models\Page::findOne($id) on line 126 can also be of type object<app\properties\HasProperties>; however, app\backend\events\Backe...ditEvent::__construct() does only seem to accept object<yii\base\Model>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
134
        $this->trigger(self::BACKEND_PAGE_EDIT, $event);
135
136
        $post = \Yii::$app->request->post();
137
        if ($event->isValid && $model->load($post)) {
0 ignored issues
show
Bug introduced by
The method load does only exist in app\modules\page\models\Page, but not in app\properties\HasProperties.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
138
            $saveStateEvent = new BackendEntityEditEvent($model);
0 ignored issues
show
Bug introduced by
It seems like $model defined by \app\modules\page\models\Page::findOne($id) on line 126 can also be of type object<app\properties\HasProperties>; however, app\backend\events\Backe...ditEvent::__construct() does only seem to accept object<yii\base\Model>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
139
            $this->trigger(self::BACKEND_PAGE_EDIT_SAVE, $saveStateEvent);
140
141
            if ($saveStateEvent->isValid && $model->validate()) {
0 ignored issues
show
Bug introduced by
The method validate does only exist in app\modules\page\models\Page, but not in app\properties\HasProperties.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
142
                $save_result = $model->save();
0 ignored issues
show
Bug introduced by
The method save does only exist in app\modules\page\models\Page, but not in app\properties\HasProperties.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
143
                $model->saveProperties($post);
0 ignored issues
show
Bug introduced by
The method saveProperties does only exist in app\properties\HasProperties, but not in app\modules\page\models\Page.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
144
145 View Code Duplication
                if (null !== $view_object = ViewObject::getByModel($model, true)) {
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type object<app\properties\HasProperties>; however, app\models\ViewObject::getByModel() does only seem to accept object<yii\db\ActiveRecord>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
146
                    if ($view_object->load($post, 'ViewObject')) {
147
                        if ($view_object->view_id <= 0) {
148
                            $view_object->delete();
149
                        } else {
150
                            $view_object->save();
151
                        }
152
                    }
153
                }
154
155
                if ($save_result) {
156
                    if (
157
                        array_key_exists('date_added', $post[$model->formName()])
158
                        && !empty($post[$model->formName()]['date_added'])
159
                        && $post[$model->formName()]['date_added'] != $model->date_added
160
                    ) {
161
                        $model->date_added = $post[$model->formName()]['date_added'];
0 ignored issues
show
Bug introduced by
The method formName does only exist in app\modules\page\models\Page, but not in app\properties\HasProperties.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
162
                        $model->save(false);
163
                    }
164
                    $modelAfterSaveEvent = new BackendEntityEditEvent($model);
0 ignored issues
show
Bug introduced by
It seems like $model defined by \app\modules\page\models\Page::findOne($id) on line 126 can also be of type object<app\properties\HasProperties>; however, app\backend\events\Backe...ditEvent::__construct() does only seem to accept object<yii\base\Model>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
165
                    $this->trigger(self::BACKEND_PAGE_AFTER_SAVE, $modelAfterSaveEvent);
166
167
                    $this->runAction('save-info', ['model_id' => $model->id]);
168
                    Yii::$app->session->setFlash('info', Yii::t('app', 'Object saved'));
169
                    $returnUrl = Yii::$app->request->get('returnUrl', ['/page/backend/index']);
170
                    switch (Yii::$app->request->post('action', 'save')) {
171
                        case 'next':
172
                            return $this->redirect(
173
                                [
174
                                    '/page/backend/edit',
175
                                    'returnUrl' => $returnUrl,
176
                                    'parent_id' => Yii::$app->request->get('parent_id', null)
177
                                ]
178
                            );
179
                        case 'back':
180
                            return $this->redirect($returnUrl);
181
                        default:
182
                            return $this->redirect(
183
                                Url::toRoute(
184
                                    [
185
                                        '/page/backend/edit',
186
                                        'id' => $model->id,
187
                                        'returnUrl' => $returnUrl,
188
                                        'parent_id' => $model->parent_id
189
                                    ]
190
                                )
191
                            );
192
                    }
193
                } else {
194
                    \Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot update data'));
195
                }
196
            }
197
        }
198
199
        return $this->render(
200
            'page-form',
201
            [
202
                'model' => $model,
203
                'object' => $object,
204
            ]
205
        );
206
    }
207
208
    /*
209
     *
210
     */
211
    public function actionDelete($id = null)
212
    {
213
        if ((null === $id) || (null === $model = Page::findOne($id))) {
214
            throw new NotFoundHttpException;
215
        }
216
217
        if (!$model->delete()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $model->delete() of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
218
            Yii::$app->session->setFlash('info', Yii::t('app', 'The object is placed in the cart'));
219
        } else {
220
            Yii::$app->session->setFlash('info', Yii::t('app', 'Object removed'));
221
        }
222
223
        return $this->redirect(
224
            Yii::$app->request->get(
225
                'returnUrl',
226
                Url::toRoute(['index', 'parent_id' => $model->parent_id])
227
            )
228
        );
229
    }
230
231 View Code Duplication
    public function actionRemoveAll($parent_id)
232
    {
233
        $items = Yii::$app->request->post('items', []);
234
        if (!empty($items)) {
235
            $items = Page::find()->where(['in', 'id', $items])->all();
236
            foreach ($items as $item) {
237
                $item->delete();
238
            }
239
        }
240
241
        return $this->redirect(['index', 'parent_id' => $parent_id]);
242
    }
243
244
    /*
245
     *
246
     */
247
    public function actionRestore($id = null, $parent_id = null)
248
    {
249
        if (null === $id) {
250
            new NotFoundHttpException();
251
        }
252
253
        if (null === $model = Page::findOne(['id' => $id])) {
254
            new NotFoundHttpException();
255
        }
256
257
        $model->restoreFromTrash();
0 ignored issues
show
Documentation Bug introduced by
The method restoreFromTrash does not exist on object<app\modules\page\models\Page>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
258
259
        Yii::$app->session->setFlash('success', Yii::t('app', 'Object successfully restored'));
260
261
        return $this->redirect(
262
            Yii::$app->request->get(
263
                'returnUrl',
264
                Url::toRoute(['edit', 'id' => $id, 'parent_id' => $parent_id])
265
            )
266
        );
267
    }
268
269
270
    /**
271
     * Clone page action.
272
     * @param integer $id
273
     * @param array|string $returnUrl
274
     * @throws \yii\web\NotFoundHttpException
275
     */
276
    public function actionClone($id, $returnUrl = ['index'])
277
    {
278
        /** @var Page|HasProperties $model */
279
        $model = Page::findOne($id);
280
        if ($model === null) {
281
            throw new NotFoundHttpException;
282
        }
283
284
        /** @var Page|HasProperties $newModel */
285
        $newModel = new Page;
286
        $newModel->setAttributes($model->attributes, false);
0 ignored issues
show
Bug introduced by
The method setAttributes does only exist in app\modules\page\models\Page, but not in app\properties\HasProperties.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
287
        $time = time();
288
        $newModel->name .= ' (copy ' . date('Y-m-d h:i:s', $time) . ')';
289
        $newModel->slug .= '-copy-' . date('Ymdhis', $time);
290
        $newModel->title .= '-copy-' . date('Ymdhis', $time);
291
        $newModel->id = null;
292 View Code Duplication
        if ($newModel->validate() === false) {
0 ignored issues
show
Bug introduced by
The method validate does only exist in app\modules\page\models\Page, but not in app\properties\HasProperties.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
293
            $newModel->slug = substr(uniqid() . "-" . $model->slug, 0, 80);
294
        }
295
        if ($newModel->save()) {
0 ignored issues
show
Bug introduced by
The method save does only exist in app\modules\page\models\Page, but not in app\properties\HasProperties.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
296
            $object = Object::getForClass(get_class($newModel));
297
            $query = new Query();
298
299
            // save images bindings
300
            $params = $query->select(
301
                ['object_id', 'filename', 'image_title', 'image_alt', 'sort_order']
302
            )->from(Image::tableName())->where(
303
                [
304
                    'object_id' => $object->id,
305
                    'object_model_id' => $model->id
306
                ]
307
            )->all();
308 View Code Duplication
            if (!empty($params)) {
309
                $rows = [];
310
                foreach ($params as $param) {
311
                    $rows[] = [
312
                        $param['object_id'],
313
                        $newModel->id,
314
                        $param['filename'],
315
                        $param['image_title'],
316
                        $param['image_alt'],
317
                        $param['sort_order'],
318
                    ];
319
                }
320
                Yii::$app->db->createCommand()->batchInsert(
321
                    Image::tableName(),
322
                    [
323
                        'object_id',
324
                        'object_model_id',
325
                        'filename',
326
                        'image_title',
327
                        'image_alt',
328
                        'sort_order',
329
                    ],
330
                    $rows
331
                )->execute();
332
            }
333
            $newModelProps = [];
334 View Code Duplication
            foreach (array_keys($model->propertyGroups) as $key) {
335
                $opg = new ObjectPropertyGroup();
336
                $opg->attributes = [
337
                    'object_id' => $object->id,
338
                    'object_model_id' => $newModel->id,
339
                    'property_group_id' => $key,
340
                ];
341
                $opg->save();
342
                $props = Property::getForGroupId($key);
343
                foreach ($props as $prop) {
344
                    $propValues = $model->getPropertyValuesByPropertyId($prop->id);
0 ignored issues
show
Bug introduced by
The method getPropertyValuesByPropertyId does only exist in app\properties\HasProperties, but not in app\modules\page\models\Page.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
345
                    if ($propValues !== null) {
346
                        foreach ($propValues->values as $val) {
347
                            $valueToSave = ArrayHelper::getValue($val, 'psv_id', $val['value']);
348
                            $newModelProps[$prop->key][] = $valueToSave;
349
                        }
350
                    }
351
                }
352
            }
353
            $newModel->saveProperties(
0 ignored issues
show
Bug introduced by
The method saveProperties does only exist in app\properties\HasProperties, but not in app\modules\page\models\Page.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
354
                [
355
                    'Properties_Page_' . $newModel->id => $newModelProps,
356
                ]
357
            );
358
            $view = ViewObject::findOne(['object_id' => $model->object->id, 'object_model_id' => $model->id]);
359
            if ($view !== null) {
360
                $newView = new ViewObject;
361
                $newView->setAttributes($view->attributes, false);
362
                $newView->id = null;
363
                $newView->object_model_id = $newModel->id;
364
                $newView->save();
365
            }
366
            Yii::$app->session->setFlash('success', Yii::t('app', 'Page has been cloned successfully.'));
367
            $this->redirect(
368
                ['edit', 'id' => $newModel->id, 'parent_id' => $newModel->parent_id, 'returnUrl' => $returnUrl]
369
            );
370
        }
371
    }
372
}
373