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 ( 400df7...c8c0ea )
by Robert
16:13
created

DataColumn::renderDataCellContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\grid;
9
10
use Closure;
11
use yii\base\Model;
12
use yii\data\ActiveDataProvider;
13
use yii\data\ArrayDataProvider;
14
use yii\db\ActiveQueryInterface;
15
use yii\helpers\ArrayHelper;
16
use yii\helpers\Html;
17
use yii\helpers\Inflector;
18
19
/**
20
 * DataColumn is the default column type for the [[GridView]] widget.
21
 *
22
 * It is used to show data columns and allows [[enableSorting|sorting]] and [[filter|filtering]] them.
23
 *
24
 * A simple data column definition refers to an attribute in the data model of the
25
 * GridView's data provider. The name of the attribute is specified by [[attribute]].
26
 *
27
 * By setting [[value]] and [[label]], the header and cell content can be customized.
28
 *
29
 * A data column differentiates between the [[getDataCellValue|data cell value]] and the
30
 * [[renderDataCellContent|data cell content]]. The cell value is an un-formatted value that
31
 * may be used for calculation, while the actual cell content is a [[format|formatted]] version of that
32
 * value which may contain HTML markup.
33
 *
34
 * For more details and usage information on DataColumn, see the [guide article on data widgets](guide:output-data-widgets).
35
 *
36
 * @author Qiang Xue <[email protected]>
37
 * @since 2.0
38
 */
39
class DataColumn extends Column
40
{
41
    /**
42
     * @var string the attribute name associated with this column. When neither [[content]] nor [[value]]
43
     * is specified, the value of the specified attribute will be retrieved from each data model and displayed.
44
     *
45
     * Also, if [[label]] is not specified, the label associated with the attribute will be displayed.
46
     */
47
    public $attribute;
48
    /**
49
     * @var string|null label to be displayed in the [[header|header cell]] and also to be used as the sorting
50
     * link label when sorting is enabled for this column.
51
     * If it is not set and the models provided by the GridViews data provider are instances
52
     * of [[\yii\db\ActiveRecord]], the label will be determined using [[\yii\db\ActiveRecord::getAttributeLabel()]].
53
     * Otherwise [[\yii\helpers\Inflector::camel2words()]] will be used to get a label.
54
     */
55
    public $label;
56
    /**
57
     * @var bool whether the header label should be HTML-encoded.
58
     * @see label
59
     * @since 2.0.1
60
     */
61
    public $encodeLabel = true;
62
    /**
63
     * @var string|Closure|null an anonymous function or a string that is used to determine the value to display in the current column.
64
     *
65
     * If this is an anonymous function, it will be called for each row and the return value will be used as the value to
66
     * display for every data model. The signature of this function should be: `function ($model, $key, $index, $column)`.
67
     * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
68
     * and `$column` is a reference to the [[DataColumn]] object.
69
     *
70
     * You may also set this property to a string representing the attribute name to be displayed in this column.
71
     * This can be used when the attribute to be displayed is different from the [[attribute]] that is used for
72
     * sorting and filtering.
73
     *
74
     * If this is not set, `$model[$attribute]` will be used to obtain the value, where `$attribute` is the value of [[attribute]].
75
     */
76
    public $value;
77
    /**
78
     * @var string|array|Closure in which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`,
79
     * `['date', 'php:Y-m-d']`). Supported formats are determined by the [[GridView::formatter|formatter]] used by
80
     * the [[GridView]]. Default format is "text" which will format the value as an HTML-encoded plain text when
81
     * [[\yii\i18n\Formatter]] is used as the [[GridView::$formatter|formatter]] of the GridView.
82
     * @see \yii\i18n\Formatter::format()
83
     */
84
    public $format = 'text';
85
    /**
86
     * @var bool whether to allow sorting by this column. If true and [[attribute]] is found in
87
     * the sort definition of [[GridView::dataProvider]], then the header cell of this column
88
     * will contain a link that may trigger the sorting when being clicked.
89
     */
90
    public $enableSorting = true;
91
    /**
92
     * @var array the HTML attributes for the link tag in the header cell
93
     * generated by [[\yii\data\Sort::link]] when sorting is enabled for this column.
94
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
95
     */
96
    public $sortLinkOptions = [];
97
    /**
98
     * @var string|array|null|false the HTML code representing a filter input (e.g. a text field, a dropdown list)
99
     * that is used for this data column. This property is effective only when [[GridView::filterModel]] is set.
100
     *
101
     * - If this property is not set, a text field will be generated as the filter input with attributes defined
102
     *   with [[filterInputOptions]]. See [[\yii\helpers\BaseHtml::activeInput]] for details on how an active
103
     *   input tag is generated.
104
     * - If this property is an array, a dropdown list will be generated that uses this property value as
105
     *   the list options.
106
     * - If you don't want a filter for this data column, set this value to be false.
107
     */
108
    public $filter;
109
    /**
110
     * @var array the HTML attributes for the filter input fields. This property is used in combination with
111
     * the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to
112
     * render the HTML attributes for the generated filter input fields.
113
     *
114
     * Empty `id` in the default value ensures that id would not be obtained from the model attribute thus
115
     * providing better performance.
116
     *
117
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
118
     */
119
    public $filterInputOptions = ['class' => 'form-control', 'id' => null];
120
    /**
121
     * @var string|null the attribute name of the [[GridView::filterModel]] associated with this column. If not set,
122
     * will have the same value as [[attribute]].
123
     * @since 2.0.41
124
     */
125
    public $filterAttribute;
126
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 9
    public function init()
132
    {
133 9
        parent::init();
134 9
        if($this->filterAttribute === null) {
135 8
            $this->filterAttribute = $this->attribute;
136
        }
137 9
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 1
    protected function renderHeaderCellContent()
143
    {
144 1
        if ($this->header !== null || $this->label === null && $this->attribute === null) {
145
            return parent::renderHeaderCellContent();
146
        }
147
148 1
        $label = $this->getHeaderCellLabel();
149 1
        if ($this->encodeLabel) {
150 1
            $label = Html::encode($label);
151
        }
152
153 1
        if ($this->attribute !== null && $this->enableSorting &&
154 1
            ($sort = $this->grid->dataProvider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
155 1
            return $sort->link($this->attribute, array_merge($this->sortLinkOptions, ['label' => $label]));
156
        }
157
158
        return $label;
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     * @since 2.0.8
164
     */
165 3
    protected function getHeaderCellLabel()
166
    {
167 3
        $provider = $this->grid->dataProvider;
168
169 3
        if ($this->label === null) {
170 3
            if ($this->attribute === null) {
171
                $label = '';
172 3
            } elseif ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
173
                /* @var $modelClass Model */
174 1
                $modelClass = $provider->query->modelClass;
0 ignored issues
show
Bug introduced by
Accessing modelClass on the interface yii\db\ActiveQueryInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
175 1
                $model = $modelClass::instance();
176 1
                $label = $model->getAttributeLabel($this->attribute);
177 2
            } elseif ($provider instanceof ArrayDataProvider && $provider->modelClass !== null) {
178
                /* @var $modelClass Model */
179 1
                $modelClass = $provider->modelClass;
180 1
                $model = $modelClass::instance();
181 1
                $label = $model->getAttributeLabel($this->attribute);
182 1
            } elseif ($this->grid->filterModel !== null && $this->grid->filterModel instanceof Model) {
183 1
                $label = $this->grid->filterModel->getAttributeLabel($this->filterAttribute);
184
            } else {
185
                $models = $provider->getModels();
186
                if (($model = reset($models)) instanceof Model) {
187
                    /* @var $model Model */
188
                    $label = $model->getAttributeLabel($this->attribute);
189
                } else {
190 3
                    $label = Inflector::camel2words($this->attribute);
191
                }
192
            }
193
        } else {
194 1
            $label = $this->label;
195
        }
196
197 3
        return $label;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203 5
    protected function renderFilterCellContent()
204
    {
205 5
        if (is_string($this->filter)) {
206 1
            return $this->filter;
207
        }
208
209 4
        $model = $this->grid->filterModel;
210
211 4
        if ($this->filter !== false && $model instanceof Model && $this->filterAttribute !== null && $model->isAttributeActive($this->filterAttribute)) {
212 4
            if ($model->hasErrors($this->filterAttribute)) {
213
                Html::addCssClass($this->filterOptions, 'has-error');
214
                $error = ' ' . Html::error($model, $this->filterAttribute, $this->grid->filterErrorOptions);
215
            } else {
216 4
                $error = '';
217
            }
218 4
            if (is_array($this->filter)) {
219 1
                $options = array_merge(['prompt' => '', 'strict' => true], $this->filterInputOptions);
220 1
                return Html::activeDropDownList($model, $this->filterAttribute, $this->filter, $options) . $error;
221 3
            } elseif ($this->format === 'boolean') {
222 1
                $options = array_merge(['prompt' => '', 'strict' => true], $this->filterInputOptions);
223 1
                return Html::activeDropDownList($model, $this->filterAttribute, [
224 1
                    1 => $this->grid->formatter->booleanFormat[1],
225 1
                    0 => $this->grid->formatter->booleanFormat[0],
226 1
                ], $options) . $error;
227
            }
228 2
            $options = array_merge(['maxlength' => true], $this->filterInputOptions);
229
230 2
            return Html::activeTextInput($model, $this->filterAttribute, $options) . $error;
231
        }
232
233
        return parent::renderFilterCellContent();
234
    }
235
236
    /**
237
     * Returns the data cell value.
238
     * @param mixed $model the data model
239
     * @param mixed $key the key associated with the data model
240
     * @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
241
     * @return string the data cell value
242
     */
243
    public function getDataCellValue($model, $key, $index)
244
    {
245
        if ($this->value !== null) {
246
            if (is_string($this->value)) {
247
                return ArrayHelper::getValue($model, $this->value);
248
            }
249
250
            return call_user_func($this->value, $model, $key, $index, $this);
251
        } elseif ($this->attribute !== null) {
252
            return ArrayHelper::getValue($model, $this->attribute);
253
        }
254
255
        return null;
256
    }
257
258
    /**
259
     * {@inheritdoc}
260
     */
261
    protected function renderDataCellContent($model, $key, $index)
262
    {
263
        if ($this->content === null) {
264
            return $this->grid->formatter->format($this->getDataCellValue($model, $key, $index), $this->format);
0 ignored issues
show
Bug introduced by
The method format() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

264
            return $this->grid->formatter->/** @scrutinizer ignore-call */ format($this->getDataCellValue($model, $key, $index), $this->format);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
265
        }
266
267
        return parent::renderDataCellContent($model, $key, $index);
268
    }
269
}
270