Completed
Push — master ( b27204...a2c64d )
by Nate
05:15 queued 02:48
created

settings/view/OrganizationTypesController.php (1 issue)

the variable you call a method on is always an object.

Bug Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\cp\controllers\settings\view;
10
11
use Craft;
12
use craft\helpers\UrlHelper as UrlHelper;
13
use flipbox\organizations\records\OrganizationType;
14
use yii\web\Response;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class OrganizationTypesController extends AbstractSettingsController
21
{
22
    /**
23
     * The index view template path
24
     */
25
    const TEMPLATE_INDEX = parent::TEMPLATE_BASE . '/organizationTypes';
26
27
    /**
28
     * The insert/update view template path
29
     */
30
    const TEMPLATE_UPSERT = self::TEMPLATE_INDEX . '/upsert';
31
32
    /**
33
     * @return Response
34
     */
35
    public function actionIndex()
36
    {
37
        $variables = [];
38
        $this->baseVariables($variables);
39
40
        $variables['types'] = OrganizationType::findAll([]);
41
42
        return $this->renderTemplate(static::TEMPLATE_INDEX, $variables);
43
    }
44
45
    /**
46
     * @param null $identifier
47
     * @param OrganizationType|null $type
48
     * @return Response
49
     * @throws \flipbox\craft\ember\exceptions\RecordNotFoundException
50
     */
51
    public function actionUpsert($identifier = null, OrganizationType $type = null)
52
    {
53
        if (null === $type) {
54
            if (null === $identifier) {
55
                $type = new OrganizationType();
56
            } else {
57
                $type = OrganizationType::getOne($identifier);
58
            }
59
        }
60
61
        $variables = [];
62
        if ($type->getIsNewRecord()) {
0 ignored issues
show
It seems like $type is not always an object, but can also be of type array. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
63
            $this->insertVariables($variables);
64
        } else {
65
            $this->updateVariables($variables, $type);
66
        }
67
68
        $variables['type'] = $type;
69
        $variables['fullPageForm'] = true;
70
        $variables['tabs'] = $this->getTabs();
71
72
        return $this->renderTemplate(static::TEMPLATE_UPSERT, $variables);
73
    }
74
75
    /*******************************************
76
     * TABS
77
     *******************************************/
78
79
    /**
80
     * @return array|null
81
     */
82
    protected function getTabs(): array
83
    {
84
        return [
85
            'type' => [
86
                'label' => Craft::t('organizations', 'Type'),
87
                'url' => '#type'
88
            ],
89
            'layout' => [
90
                'label' => Craft::t('organizations', 'Layout'),
91
                'url' => '#layout'
92
            ]
93
        ];
94
    }
95
96
    /*******************************************
97
     * BASE PATHS
98
     *******************************************/
99
100
    /**
101
     * @return string
102
     */
103
    protected function getBaseActionPath(): string
104
    {
105
        return parent::getBaseActionPath() . '/organization-types';
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    protected function getBaseCpPath(): string
112
    {
113
        return parent::getBaseCpPath() . '/organization-types';
114
    }
115
116
    /*******************************************
117
     * INSERT VARIABLES
118
     *******************************************/
119
120
    /**
121
     * @param array $variables
122
     */
123
    protected function insertVariables(array &$variables)
124
    {
125
        parent::insertVariables($variables);
126
        $variables['continueEditingUrl'] = $this->getBaseContinueEditingUrl('/{id}');
127
    }
128
129
    /*******************************************
130
     * UPDATE VARIABLES
131
     *******************************************/
132
133
    /**
134
     * @param array $variables
135
     * @param OrganizationType $type
136
     */
137
    protected function updateVariables(array &$variables, OrganizationType $type)
138
    {
139
        $this->baseVariables($variables);
140
        $variables['title'] .= ' - ' . Craft::t('organizations', 'Edit') . ' ' . $type->name;
141
        $variables['continueEditingUrl'] = $this->getBaseContinueEditingUrl('/' . $type->getId());
142
        $variables['saveShortcutRedirect'] = $variables['continueEditingUrl'];
143
        $variables['crumbs'][] = [
144
            'label' => $type->name,
145
            'url' => UrlHelper::url($variables['continueEditingUrl'])
146
        ];
147
    }
148
149
    /*******************************************
150
     * VARIABLES
151
     *******************************************/
152
153
    /**
154
     * @inheritdoc
155
     */
156
    protected function baseVariables(array &$variables = [])
157
    {
158
        parent::baseVariables($variables);
159
        $variables['title'] .= ': Types';
160
        $variables['crumbs'][] = [
161
            'label' => Craft::t('organizations', 'Types'),
162
            'url' => UrlHelper::url($variables['baseCpPath'])
163
        ];
164
    }
165
}
166