Completed
Push — master ( 320db8...6f0a28 )
by Vitaly
04:44 queued 02:06
created

Collection   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 169
Duplicated Lines 7.1 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 12
Bugs 3 Features 6
Metric Value
wmc 17
c 12
b 3
f 6
lcom 1
cbo 3
dl 12
loc 169
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A indexView() 0 5 1
A itemView() 0 6 1
A emptyView() 0 5 1
A renderItem() 0 7 1
A renderEmpty() 0 4 1
A renderIndex() 0 6 1
A pager() 0 7 2
B output() 12 33 6
A __toString() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 11.02.16 at 14:15
5
 */
6
namespace samsoncms\api\renderable;
7
8
use samson\activerecord\dbQuery;
9
use samsoncms\api\Entity;
10
use samsonframework\core\ViewInterface;
11
use samsonframework\orm\QueryInterface;
12
13
/**
14
 * Renderable fields table.
15
 * Class should be used to simplify SamsonCMS generated entities outputting.
16
 *
17
 * @see \samsoncms\api\FieldsTable This class is just a wrapper with rendering
18
 *
19
 * @package samsoncms\api\renderable
20
 */
21
class Collection extends \samsoncms\api\query\Entity
22
{
23
    /** Name of the items variable in index view */
24
    const ITEMS_VIEW_VARIABLE = 'items';
25
26
    /** Name of the item prefix for variable in item view */
27
    const ITEM_VIEW_VARIABLE = 'item';
28
29
    /** @var string|callable Block view file or callback */
30
    protected $indexView;
31
32
    /** @var string|callable Item view file or callback */
33
    protected $itemView ;
34
35
    /** @var string|callable Empty view file or callback */
36
    protected $emptyView = 'www/empty';
37
38
    /** @var ViewInterface View render object */
39
    protected $renderer;
40
41
    /** @var int Count of entities on one page */
42
    protected $pageSize;
43
44
    /** @var int Current page number */
45
    protected $pageNumber;
46
47
    /**
48
     * Collection constructor.
49
     *
50
     * @param ViewInterface  $renderer Instance for rendering views
51
     * @param QueryInterface $query Instance for querying database
52
     * @param string|null    $locale Localization language
53
     */
54
    public function __construct(ViewInterface $renderer, QueryInterface $query = null, $locale = null)
55
    {
56
        $this->renderer = $renderer;
57
58
        parent::__construct(null === $query ? new dbQuery() : $query, $locale);
59
    }
60
61
    /**
62
     * Set index view path.
63
     * @param string|callable $indexView Index view path or callback
64
     * @return $this Chaining
65
     */
66
    public function indexView($indexView)
67
    {
68
        $this->indexView = $indexView;
69
        return $this;
70
    }
71
72
    /**
73
     * Set item view path.
74
     * @param string|callable $itemView Item view path or callback
75
     * @return $this Chaining
76
     */
77
    public function itemView($itemView)
78
    {
79
        $this->itemView = $itemView;
80
81
        return $this;
82
    }
83
84
    /**
85
     * Set empty view path.
86
     * @param string|callable $emptyView Empty view path or callback
87
     * @return $this Chaining
88
     */
89
    public function emptyView($emptyView)
90
    {
91
        $this->emptyView = $emptyView;
92
        return $this;
93
    }
94
95
    /**
96
     * Render Entity collection item.
97
     *
98
     * @param Entity $item SamsonCMS entity for rendering
99
     *
100
     * @return string Rendered HTML
101
     */
102
    public function renderItem(Entity $item)
103
    {
104
        return $this->renderer
105
            ->view($this->itemView)
0 ignored issues
show
Documentation introduced by
$this->itemView is of type callable, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
            ->set($item, self::ITEM_VIEW_VARIABLE)
0 ignored issues
show
Documentation introduced by
$item is of type object<samsoncms\api\Entity>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
            ->output();
108
    }
109
110
    /**
111
     * Render empty collection item.
112
     *
113
     * @return string Rendered HTML
114
     */
115
    public function renderEmpty()
116
    {
117
        return $this->renderer->view($this->emptyView)->output();
0 ignored issues
show
Documentation introduced by
$this->emptyView is of type callable, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
118
    }
119
120
    /**
121
     * Render Entity collection index.
122
     *
123
     * @param string $items Collection of rendered items
124
     *
125
     * @return string Rendered HTML
126
     */
127
    public function renderIndex($items)
128
    {
129
        return $this->renderer->view($this->indexView)
0 ignored issues
show
Documentation introduced by
$this->indexView is of type callable, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130
            ->set($items, self::ITEMS_VIEW_VARIABLE)
131
            ->output();
132
    }
133
134
    /**
135
     * Set pagination for SamsonCMS query.
136
     *
137
     * @param int $pageNumber Result page number
138
     * @param int|null $pageSize Results page size
139
     * @return $this Chaining
140
     */
141
    public function pager($pageNumber, $pageSize = null)
142
    {
143
        $this->pageNumber = $pageNumber;
144
        $this->pageSize = null !== $pageSize ? $pageSize : $this->pageSize;
145
146
        return $this;
147
    }
148
149
    /** @return string Rendered HTML for fields table */
150
    public function output()
151
    {
152
        // Perform SamsonCMS query
153
        $collection = $this->find($this->pageNumber, $this->pageSize);
154
155
        $html = '';
156
        if (count($collection)) {
157
            // Render each entity view in collection
158
            foreach ($collection as $row) {
159
                // Call external handler
160 View Code Duplication
                if (is_callable($this->itemView)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
                    $html .= call_user_func($this->itemView, $row, $this->renderer, $this->query, $collection);
162
                } else { // Call default renderer
163
                    $html .= $this->renderItem($row);
164
                }
165
            }
166
167
            // Render collection main view with items
168
            if (is_callable($this->indexView)) {
169
                $html = call_user_func($this->indexView, $html, $this->renderer, $this->query, $collection);
170
            } else { // Call default renderer
171
                $html = $this->renderIndex($html);
172
            }
173 View Code Duplication
        } else { // Render empty entity view
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
            if (is_callable($this->emptyView)) {
175
                $html .= call_user_func($this->emptyView, $html, $this->renderer, $this->query, $collection);
176
            } else {
177
                $html .= $this->renderEmpty();
178
            }
179
        }
180
181
        return $html;
182
    }
183
184
    /** @return string Rendered fields table */
185
    public function __toString()
186
    {
187
        return $this->output();
188
    }
189
}
190