Completed
Push — master ( 1246a9...320db8 )
by Vitaly
03:07
created

FieldsTable   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 123
Duplicated Lines 4.07 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 7
Bugs 3 Features 3
Metric Value
wmc 11
c 7
b 3
f 3
lcom 1
cbo 2
dl 5
loc 123
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A indexView() 0 5 1
A rowView() 0 5 1
A renderRow() 0 4 1
A renderIndex() 0 6 1
A render() 5 21 4
A __toString() 0 4 1
A toView() 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 10.02.16 at 18:15
5
 */
6
namespace samsoncms\api\renderable;
7
8
use samsoncms\api\field\Row;
9
use samsonframework\core\RenderInterface;
10
use samsonframework\core\ViewInterface;
11
use samsonframework\orm\QueryInterface;
12
13
/**
14
 * Renderable fields table.
15
 * Class should be used to simplify additional field tables rendering.
16
 * If your additional fields table has special logic just extend this class
17
 * and change any ot its method.
18
 *
19
 * @see \samsoncms\api\FieldsTable This class is just a wrapper with rendering
20
 *
21
 * @package samsoncms\api\renderable
22
 */
23
class FieldsTable extends \samsoncms\api\field\Table implements RenderInterface
24
{
25
    /** Name of the rows variable in index view */
26
    const ROWS_VIEW_VARIABLE = 'rows';
27
28
    /** Name of the row prefix for variable in row view */
29
    const ROW_VIEW_VARIABLE = 'row';
30
31
    /** @var string|callable Block view file or callback */
32
    protected $indexView = 'index';
33
34
    /** @var string|callable Row view file or callback */
35
    protected $rowView = 'row';
36
37
    /** @var ViewInterface */
38
    protected $renderer;
39
40
    /**
41
     * GeneralInfo constructor.
42
     *
43
     * @param QueryInterface $query
44
     * @param ViewInterface $renderer
45
     * @param int[] $navigationID Collection of entity navigation identifiers
46
     * @param int $entityID Entity identifier
47
     * @param string|null $locale Table localization
48
     */
49
    public function __construct(QueryInterface $query, ViewInterface $renderer, $navigationID, $entityID, $locale = null)
50
    {
51
        // Store renderer
52
        $this->renderer = $renderer;
53
54
        parent::__construct($query, $navigationID, $entityID, $locale);
55
    }
56
57
    /**
58
     * Set index view path.
59
     * @param string|callable $indexView Index view path or callback
60
     * @return $this Chaining
61
     */
62
    public function indexView($indexView)
63
    {
64
        $this->indexView = $indexView;
65
        return $this;
66
    }
67
    /**
68
     * Set row view path.
69
     * @param string|callable $rowView Row view path or callback
70
     * @return $this Chaining
71
     */
72
    public function rowView($rowView)
73
    {
74
        $this->rowView = $rowView;
75
        return $this;
76
    }
77
78
    /**
79
     * Render table row.
80
     *
81
     * @param Row $row Collection of column values.
82
     *
83
     * @return string Rendered HTML
84
     */
85
    public function renderRow(Row $row)
86
    {
87
        return $this->renderer->view($this->rowView)->set($row, self::ROW_VIEW_VARIABLE)->output();
0 ignored issues
show
Documentation introduced by
$this->rowView 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...
Documentation introduced by
$row is of type object<samsoncms\api\field\Row>, 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...
88
    }
89
90
    /**
91
     * Render table row.
92
     *
93
     * @param string $items Collection of rendered rows.
94
     *
95
     * @return string Rendered HTML
96
     */
97
    public function renderIndex($items)
98
    {
99
        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...
100
            ->set($items, self::ROWS_VIEW_VARIABLE)
101
            ->output();
102
    }
103
104
    /** @return string Rendered HTML for fields table */
105
    public function render()
106
    {
107
        $html = '';
108
        foreach ($this->collection as $row) {
109
            // Call external handler
110 View Code Duplication
            if (is_callable($this->rowView)) {
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...
111
                $html .= call_user_func($this->rowView, $row, $this->renderer, $this->query, $this->collection);
112
            } else { // Call default renderer
113
                $html .= $this->renderRow($row);
114
            }
115
        }
116
117
        // Call external handler
118
        if (is_callable($this->indexView)) {
119
            $html = call_user_func($this->indexView, $html, $this->renderer, $this->query, $this->collection);
120
        } else { // Call default renderer
121
            $html = $this->renderIndex($html);
122
        }
123
124
        return $html;
125
    }
126
127
    /** @return string Rendered fields table */
128
    public function __toString()
129
    {
130
        return $this->render();
131
    }
132
133
    /**
134
     * Prepare fields table for rendering in the view.
135
     *
136
     * @param string|null $prefix Prefix to be added to view variables
137
     * @param array $restricted Collection of fields to be restricted
138
     *
139
     * @return array Collection of view variables for rendering
140
     */
141
    public function toView($prefix = null, array $restricted = array())
142
    {
143
        return array_diff_key(array($prefix.'html' => $this->render()), $restricted);
144
    }
145
}
146