ToolbarButton   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 94
Duplicated Lines 7.45 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 8
dl 7
loc 94
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
B renderButton() 0 31 5
A addAttributes() 0 6 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
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid\Toolbar;
10
11
use Nette\Utils\Html;
12
use Ublaboo\DataGrid\DataGrid;
13
use Ublaboo\DataGrid\Exception\DataGridColumnRendererException;
14
use Ublaboo\DataGrid\Traits;
15
16
class ToolbarButton
17
{
18
	use Traits\TButtonTryAddIcon;
19
	use Traits\TButtonClass;
20
	use Traits\TButtonIcon;
21
	use Traits\TButtonRenderer;
22
	use Traits\TButtonText;
23
	use Traits\TButtonTitle;
24
	use Traits\TLink;
25
26
	/**
27
	 * @var DataGrid
28
	 */
29
	protected $grid;
30
31
	/**
32
	 * @var string
33
	 */
34
	protected $href;
35
36
	/**
37
	 * @var array
38
	 */
39
	protected $params;
40
41
	/**
42
	 * @var array
43
	 */
44
	protected $attributes = [];
45
46
47
	/**
48
	 * @param DataGrid $grid
49
	 * @param string   $href
50
	 * @param string   $text
51
	 * @param array    $params
52
	 */
53 View Code Duplication
	public function __construct(DataGrid $grid, $href, $text, $params = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
54
	{
55
		$this->grid = $grid;
56
		$this->href = $href;
57
		$this->text = $text;
58
		$this->params = $params;
59
	}
60
61
62
	/**
63
	 * Render toolbar button
64
	 * @return Html
65
	 */
66
	public function renderButton()
67
	{
68
		try {
69
			// Renderer function may be used
70
			return $this->useRenderer();
71
		} catch (DataGridColumnRendererException $e) {
72
			// Do not use renderer
73
		}
74
75
		$link = $this->createLink($this->grid, $this->href, $this->params);
76
77
		$a = Html::el('a')->href($link);
78
79
		$this->tryAddIcon($a, $this->getIcon(), $this->getText());
80
81
		if (!empty($this->attributes)) {
82
			$a->addAttributes($this->attributes);
83
		}
84
85
		$a->addText($this->grid->getTranslator()->translate($this->text));
86
87
		if ($this->getTitle()) {
88
			$a->title($this->grid->getTranslator()->translate($this->getTitle()));
89
		}
90
91
		if ($this->getClass()) {
92
			$a->class($this->getClass());
93
		}
94
95
		return $a;
96
	}
97
98
99
	/**
100
	 * @param array $attrs
101
	 * @return static
102
	 */
103
	public function addAttributes(array $attrs)
104
	{
105
		$this->attributes = $this->attributes + $attrs;
106
107
		return $this;
108
	}
109
}
110