Completed
Push — master ( 0636af...e088fc )
by Adam
07:34
created

Widget   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 168
Duplicated Lines 5.95 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getTitle() 0 6 2
A getDescription() 0 4 1
A getPriority() 0 4 1
A getStatus() 0 4 1
A getPosition() 0 4 1
C beforeRender() 10 77 11
A render() 0 7 1
A __toString() 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
 * Widget.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:Widgets!
9
 * @subpackage     Widgets
10
 * @since          1.0.0
11
 *
12
 * @date           15.09.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Widgets\Widgets;
18
19
use Nette;
20
use Nette\Application;
21
use Nette\ComponentModel;
22
use Nette\Localization;
23
use Nette\Utils;
24
25
use IPub;
26
use IPub\Widgets;
27
use IPub\Widgets\Decorators;
28
use IPub\Widgets\Entities;
29
use IPub\Widgets\Exceptions;
30
31
/**
32
 * Widgets control definition
33
 *
34
 * @package        iPublikuj:Widgets!
35
 * @subpackage     Widgets
36
 *
37
 * @author         Adam Kadlec <[email protected]>
38
 *
39
 * @property Application\UI\ITemplate $template
40
 */
41
abstract class Widget extends Widgets\Application\UI\BaseControl implements IWidget
42
{
43
	/**
44
	 * @var Entities\IData
45
	 */
46
	protected $data;
47
48
	/**
49
	 * @param Entities\IData $data
50
	 * @param ComponentModel\IContainer $parent
51
	 * @param string $name
52
	 */
53
	public function __construct(
54
		Entities\IData $data,
55
		ComponentModel\IContainer $parent = NULL,
56
		string $name = NULL
57
	) {
58
		parent::__construct($parent, $name);
59
60
		$this->data = $data;
61
	}
62
63
	/**
64
	 * {@inheritdoc}
65
	 */
66
	public function getTitle()
67
	{
68
		$title = $this->data->getTitle();
69
70
		return $title ? $title : '';
71
	}
72
73
	/**
74
	 * {@inheritdoc}
75
	 */
76
	public function getDescription()
77
	{
78
		return $this->data->getDescription();
79
	}
80
81
	/**
82
	 * {@inheritdoc}
83
	 */
84
	public function getPriority()
85
	{
86
		return $this->data->getPriority();
87
	}
88
89
	/**
90
	 * {@inheritdoc}
91
	 */
92
	public function getStatus()
93
	{
94
		return $this->data->getStatus();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->data->getStatus(); (boolean) is incompatible with the return type declared by the interface IPub\Widgets\Widgets\IWidget::getStatus of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
95
	}
96
97
	/**
98
	 * {@inheritdoc}
99
	 */
100
	public function getPosition()
101
	{
102
		return $this->data->getPosition();
103
	}
104
105
	/**
106
	 * Before render actions
107
	 */
108
	protected function beforeRender()
109
	{
110
		// Check if control has template
111
		if (!$this->template instanceof Nette\Bridges\ApplicationLatte\Template) {
112
			throw new Exceptions\InvalidStateException('Widgets container control is without template.');
113
		}
114
115
		// Get widget title
116
		$name = $this->getTitle();
117
118
		// If widget name has space...
119
		$pos = mb_strpos($name, ' ');
120
		if ($pos !== FALSE && mb_strpos($name, '||') === FALSE) {
121
			$name = Utils\Html::el('span')
122
				->addAttributes(['class' => 'color'])
123
				->setText(mb_substr($name, 0, $pos))
124
				->render();
125
126
			// Modify widget name
127
			$name = $name . mb_substr($name, $pos);
128
		}
129
130
		// If widget name has subtitle...
131
		$pos = mb_strpos($name, '||');
132
		if ($pos !== FALSE) {
133
			$title = Utils\Html::el('span')
134
				->addAttributes(['class' => 'title'])
135
				->setText(mb_substr($name, 0, $pos))
136
				->render();
137
138
			$subtitle = Utils\Html::el('span')
139
				->addAttributes(['class' => 'subtitle'])
140
				->setText(mb_substr($name, $pos + 2))
141
				->render();
142
143
			// Split name to title & subtitle
144
			$name = $title . $subtitle;
145
		}
146
147
		// Set badge if exists
148 View Code Duplication
		if ($badge = $this->data->getBadge()) {
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...
149
			$badge = Utils\Html::el('span')
150
				->addAttributes(['class' => 'badge badge-' . $badge])
151
				->render();
152
		}
153
154
		// Set icon if exists
155 View Code Duplication
		if ($icon = $this->data->getIcon()) {
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...
156
			$icon = Utils\Html::el('span')
157
				->addAttributes(['class' => 'icon icon-' . $icon])
158
				->render();
159
		}
160
161
		// Assign basic widget data to template
162
		$this->template->add('badge', $badge);
163
		$this->template->add('icon', $icon);
164
		$this->template->add('title', [
165
			'text'   => $name,
166
			'insert' => $this->data->getParam('widget.title.insert', TRUE),
0 ignored issues
show
Documentation introduced by
TRUE is of type boolean, but the function expects a string|null.

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...
167
			'hidden' => $this->data->getParam('widget.title.hidden', FALSE)
0 ignored issues
show
Documentation introduced by
FALSE is of type boolean, but the function expects a string|null.

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...
168
		]);
169
170
		// Check if translator is available
171
		if ($this->getTranslator() instanceof Localization\ITranslator) {
172
			$this->template->setTranslator($this->getTranslator());
173
		}
174
175
		// If template was not defined before...
176
		if ($this->template->getFile() === NULL) {
177
			// Get component actual dir
178
			$dir = dirname($this->getReflection()->getFileName());
179
180
			// ...try to get base component template file
181
			$templateFile = $this->templateFile !== NULL && is_file($this->templateFile) ? $this->templateFile : $dir . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default.latte';
182
			$this->template->setFile($templateFile);
183
		}
184
	}
185
186
	/**
187
	 * Render widget
188
	 */
189
	public function render()
190
	{
191
		$this->beforeRender();
192
193
		// Render component template
194
		$this->template->render();
195
	}
196
197
	/**
198
	 * Convert widget name to string representation
199
	 *
200
	 * @return string
201
	 */
202
	public function __toString()
203
	{
204
		$class = explode('\\', get_class($this));
205
206
		return end($class);
207
	}
208
}
209