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.
Completed
Push — master ( 560332...66e815 )
by Robert
11:59
created

BaseListView::renderSummary()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 51
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 10.5

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 22
cts 44
cp 0.5
rs 8.6588
c 0
b 0
f 0
cc 6
eloc 43
nc 7
nop 0
crap 10.5

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
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\widgets;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\base\Widget;
13
use yii\helpers\ArrayHelper;
14
use yii\helpers\Html;
15
16
/**
17
 * BaseListView is a base class for widgets displaying data from data provider
18
 * such as ListView and GridView.
19
 *
20
 * It provides features like sorting, paging and also filtering the data.
21
 *
22
 * For more details and usage information on BaseListView, see the [guide article on data widgets](guide:output-data-widgets).
23
 *
24
 * @author Qiang Xue <[email protected]>
25
 * @since 2.0
26
 */
27
abstract class BaseListView extends Widget
28
{
29
    /**
30
     * @var array the HTML attributes for the container tag of the list view.
31
     * The "tag" element specifies the tag name of the container element and defaults to "div".
32
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
33
     */
34
    public $options = [];
35
    /**
36
     * @var \yii\data\DataProviderInterface the data provider for the view. This property is required.
37
     */
38
    public $dataProvider;
39
    /**
40
     * @var array the configuration for the pager widget. By default, [[LinkPager]] will be
41
     * used to render the pager. You can use a different widget class by configuring the "class" element.
42
     * Note that the widget must support the `pagination` property which will be populated with the
43
     * [[\yii\data\BaseDataProvider::pagination|pagination]] value of the [[dataProvider]].
44
     */
45
    public $pager = [];
46
    /**
47
     * @var array the configuration for the sorter widget. By default, [[LinkSorter]] will be
48
     * used to render the sorter. You can use a different widget class by configuring the "class" element.
49
     * Note that the widget must support the `sort` property which will be populated with the
50
     * [[\yii\data\BaseDataProvider::sort|sort]] value of the [[dataProvider]].
51
     */
52
    public $sorter = [];
53
    /**
54
     * @var string the HTML content to be displayed as the summary of the list view.
55
     * If you do not want to show the summary, you may set it with an empty string.
56
     *
57
     * The following tokens will be replaced with the corresponding values:
58
     *
59
     * - `{begin}`: the starting row number (1-based) currently being displayed
60
     * - `{end}`: the ending row number (1-based) currently being displayed
61
     * - `{count}`: the number of rows currently being displayed
62
     * - `{totalCount}`: the total number of rows available
63
     * - `{page}`: the page number (1-based) current being displayed
64
     * - `{pageCount}`: the number of pages available
65
     */
66
    public $summary;
67
    /**
68
     * @var array the HTML attributes for the summary of the list view.
69
     * The "tag" element specifies the tag name of the summary element and defaults to "div".
70
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
71
     */
72
    public $summaryOptions = ['class' => 'summary'];
73
    /**
74
     * @var bool whether to show an empty list view if [[dataProvider]] returns no data.
75
     * The default value is false which displays an element according to the `emptyText`
76
     * and `emptyTextOptions` properties.
77
     */
78
    public $showOnEmpty = false;
79
    /**
80
     * @var string the HTML content to be displayed when [[dataProvider]] does not have any data.
81
     */
82
    public $emptyText;
83
    /**
84
     * @var array the HTML attributes for the emptyText of the list view.
85
     * The "tag" element specifies the tag name of the emptyText element and defaults to "div".
86
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
87
     */
88
    public $emptyTextOptions = ['class' => 'empty'];
89
    /**
90
     * @var string the layout that determines how different sections of the list view should be organized.
91
     * The following tokens will be replaced with the corresponding section contents:
92
     *
93
     * - `{summary}`: the summary section. See [[renderSummary()]].
94
     * - `{items}`: the list items. See [[renderItems()]].
95
     * - `{sorter}`: the sorter. See [[renderSorter()]].
96
     * - `{pager}`: the pager. See [[renderPager()]].
97
     */
98
    public $layout = "{summary}\n{items}\n{pager}";
99
100
101
    /**
102
     * Renders the data models.
103
     * @return string the rendering result.
104
     */
105
    abstract public function renderItems();
106
107
    /**
108
     * Initializes the view.
109
     */
110 20
    public function init()
111
    {
112 20
        if ($this->dataProvider === null) {
113
            throw new InvalidConfigException('The "dataProvider" property must be set.');
114
        }
115 20
        if ($this->emptyText === null) {
116 19
            $this->emptyText = Yii::t('yii', 'No results found.');
117 19
        }
118 20
        if (!isset($this->options['id'])) {
119 19
            $this->options['id'] = $this->getId();
120 19
        }
121 20
    }
122
123
    /**
124
     * Runs the widget.
125
     */
126 12
    public function run()
127
    {
128 12
        if ($this->showOnEmpty || $this->dataProvider->getCount() > 0) {
129 11
            $content = preg_replace_callback("/{\\w+}/", function ($matches) {
130 11
                $content = $this->renderSection($matches[0]);
131
132 11
                return $content === false ? $matches[0] : $content;
133 11
            }, $this->layout);
134 11
        } else {
135 1
            $content = $this->renderEmpty();
136
        }
137
138 12
        $options = $this->options;
139 12
        $tag = ArrayHelper::remove($options, 'tag', 'div');
140 12
        echo Html::tag($tag, $content, $options);
141 12
    }
142
143
    /**
144
     * Renders a section of the specified name.
145
     * If the named section is not supported, false will be returned.
146
     * @param string $name the section name, e.g., `{summary}`, `{items}`.
147
     * @return string|bool the rendering result of the section, or false if the named section is not supported.
148
     */
149 11
    public function renderSection($name)
150
    {
151
        switch ($name) {
152 11
            case '{summary}':
153 9
                return $this->renderSummary();
154 11
            case '{items}':
155 9
                return $this->renderItems();
156 11
            case '{pager}':
157 9
                return $this->renderPager();
158 2
            case '{sorter}':
159 2
                return $this->renderSorter();
160
            default:
161
                return false;
162
        }
163
    }
164
165
    /**
166
     * Renders the HTML content indicating that the list view has no data.
167
     * @return string the rendering result
168
     * @see emptyText
169
     */
170 1
    public function renderEmpty()
171
    {
172 1
        $options = $this->emptyTextOptions;
173 1
        $tag = ArrayHelper::remove($options, 'tag', 'div');
174 1
        return Html::tag($tag, $this->emptyText, $options);
175
    }
176
177
    /**
178
     * Renders the summary text.
179
     */
180 9
    public function renderSummary()
181
    {
182 9
        $count = $this->dataProvider->getCount();
183 9
        if ($count <= 0) {
184 1
            return '';
185
        }
186 8
        $summaryOptions = $this->summaryOptions;
187 8
        $tag = ArrayHelper::remove($summaryOptions, 'tag', 'div');
188 8
        if (($pagination = $this->dataProvider->getPagination()) !== false) {
189 8
            $totalCount = $this->dataProvider->getTotalCount();
190 8
            $begin = $pagination->getPage() * $pagination->pageSize + 1;
191 8
            $end = $begin + $count - 1;
192 8
            if ($begin > $end) {
193
                $begin = $end;
194
            }
195 8
            $page = $pagination->getPage() + 1;
196 8
            $pageCount = $pagination->pageCount;
197 8
            if (($summaryContent = $this->summary) === null) {
198 8
                return Html::tag($tag, Yii::t('yii', 'Showing <b>{begin, number}-{end, number}</b> of <b>{totalCount, number}</b> {totalCount, plural, one{item} other{items}}.', [
199 8
                        'begin' => $begin,
200 8
                        'end' => $end,
201 8
                        'count' => $count,
202 8
                        'totalCount' => $totalCount,
203 8
                        'page' => $page,
204 8
                        'pageCount' => $pageCount,
205 8
                    ]), $summaryOptions);
206
            }
207
        } else {
208
            $begin = $page = $pageCount = 1;
209
            $end = $totalCount = $count;
210
            if (($summaryContent = $this->summary) === null) {
211
                return Html::tag($tag, Yii::t('yii', 'Total <b>{count, number}</b> {count, plural, one{item} other{items}}.', [
212
                    'begin' => $begin,
213
                    'end' => $end,
214
                    'count' => $count,
215
                    'totalCount' => $totalCount,
216
                    'page' => $page,
217
                    'pageCount' => $pageCount,
218
                ]), $summaryOptions);
219
            }
220
        }
221
222
        return Yii::$app->getI18n()->format($summaryContent, [
223
            'begin' => $begin,
224
            'end' => $end,
225
            'count' => $count,
226
            'totalCount' => $totalCount,
227
            'page' => $page,
228
            'pageCount' => $pageCount,
229
        ], Yii::$app->language);
230
    }
231
232
    /**
233
     * Renders the pager.
234
     * @return string the rendering result
235
     */
236 9
    public function renderPager()
237
    {
238 9
        $pagination = $this->dataProvider->getPagination();
239 9
        if ($pagination === false || $this->dataProvider->getCount() <= 0) {
240 1
            return '';
241
        }
242
        /* @var $class LinkPager */
243 8
        $pager = $this->pager;
244 8
        $class = ArrayHelper::remove($pager, 'class', LinkPager::className());
245 8
        $pager['pagination'] = $pagination;
246 8
        $pager['view'] = $this->getView();
247
248 8
        return $class::widget($pager);
249
    }
250
251
    /**
252
     * Renders the sorter.
253
     * @return string the rendering result
254
     */
255 2
    public function renderSorter()
256
    {
257 2
        $sort = $this->dataProvider->getSort();
258 2
        if ($sort === false || empty($sort->attributes) || $this->dataProvider->getCount() <= 0) {
259
            return '';
260
        }
261
        /* @var $class LinkSorter */
262 2
        $sorter = $this->sorter;
263 2
        $class = ArrayHelper::remove($sorter, 'class', LinkSorter::className());
264 2
        $sorter['sort'] = $sort;
265 2
        $sorter['view'] = $this->getView();
266
267 2
        return $class::widget($sorter);
268
    }
269
}
270