Completed
Push — master ( 2c7b4a...2bc7fe )
by Jean-Christophe
03:22
created

DataElement   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 17.39 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 4
dl 12
loc 69
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 12 12 2
A _generateContent() 0 8 2
A __construct() 0 5 1
A _getFieldName() 0 3 1
A _getFieldCaption() 0 3 1
A getHtmlComponent() 0 3 1
A _setToolbarPosition() 0 3 1
A onNewRow() 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
namespace Ajax\semantic\widgets\dataelement;
4
5
use Ajax\common\Widget;
6
use Ajax\semantic\widgets\base\InstanceViewer;
7
use Ajax\semantic\widgets\datatable\PositionInTable;
8
use Ajax\JsUtils;
9
use Ajax\service\JArray;
10
use Ajax\semantic\html\collections\table\HtmlTable;
11
12
/**
13
 * DataElement widget for displaying an instance of model
14
 * @version 1.0
15
 * @author jc
16
 * @since 2.2
17
 *
18
 */
19
class DataElement extends Widget {
20
21
	public function __construct($identifier, $modelInstance=NULL) {
22
		parent::__construct($identifier, null,$modelInstance);
23
		$this->_init(new InstanceViewer($identifier), "table", new HtmlTable($identifier, 0,2), false);
24
		$this->content["table"]->setDefinition();
25
	}
26
27 View Code Duplication
	public function compile(JsUtils $js=NULL,&$view=NULL){
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...
28
		$this->_instanceViewer->setInstance($this->_modelInstance);
29
30
		$table=$this->content["table"];
31
		$this->_generateContent($table);
32
33
		if(isset($this->_toolbar)){
34
			$this->_setToolbarPosition($table);
35
		}
36
		$this->content=JArray::sortAssociative($this->content, [PositionInTable::BEFORETABLE,"table",PositionInTable::AFTERTABLE]);
37
		return parent::compile($js,$view);
38
	}
39
40
	/**
41
	 * @param HtmlTable $table
42
	 */
43
	protected function _generateContent($table){
44
		$captions=$this->_instanceViewer->getCaptions();
45
		$values= $this->_instanceViewer->getValues();
46
		$count=$this->_instanceViewer->count();
47
		for($i=0;$i<$count;$i++){
48
			$table->addRow([$captions[$i],$values[$i]]);
49
		}
50
	}
51
52
	protected function _getFieldName($index){
53
		return $this->_instanceViewer->getFieldName($index);
54
	}
55
56
	protected function _getFieldCaption($index){
57
		return null;
58
	}
59
60
	/**
61
	 * {@inheritDoc}
62
	 * @see \Ajax\common\Widget::getHtmlComponent()
63
	 * @return HtmlTable
64
	 */
65
	public function getHtmlComponent() {
66
		return $this->content["table"];
67
	}
68
69
	/**
70
	 * {@inheritdoc}
71
	 * @see \Ajax\common\Widget::_setToolbarPosition()
72
	 */
73
	protected function _setToolbarPosition($table, $captions=NULL) {
74
		$this->content[$this->_toolbarPosition]=$this->_toolbar;
75
	}
76
77
	/**
78
	 * The callback function called after the insertion of each row when fromDatabaseObjects is called
79
	 * callback function takes the parameters $row : the row inserted and $object: the instance of model used
80
	 * @param callable $callback
81
	 * @return DataElement
82
	 */
83
	public function onNewRow($callback) {
84
		$this->content["table"]->onNewRow($callback);
85
		return $this;
86
	}
87
}