Completed
Push — dev ( b0a1ff...137b1d )
by Nicolas
01:53
created

contentExtensionEntry_Relationship_FieldRender   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 183
Duplicated Lines 4.37 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 29
lcom 1
cbo 2
dl 8
loc 183
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
D view() 8 134 22
A getSectionName() 0 4 1
A getEntryTitle() 0 13 4
A buildDefaultTitle() 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
	Copyright: Deux Huit Huit 2014
4
	LICENCE: MIT http://deuxhuithuit.mit-license.org;
5
	*/
6
7
	if(!defined("__IN_SYMPHONY__")) die("<h2>Error</h2><p>You cannot directly access this file</p>");
8
9
	require_once(TOOLKIT . '/class.xmlpage.php');
10
	require_once(EXTENSIONS . '/entry_relationship_field/lib/class.cacheablefetch.php');
11
	require_once(EXTENSIONS . '/entry_relationship_field/lib/class.erfxsltutilities.php');
12
13
	class contentExtensionEntry_Relationship_FieldRender extends XMLPage {
14
		
15
		const NUMBER_OF_URL_PARAMETERS = 2;
16
17
		private $sectionManager;
18
		private $fieldManager;
19
		private $entryManager;
20
		
21
		public function __construct() {
22
			parent::__construct();
23
			$this->sectionManager = new CacheableFetch('SectionManager');
24
			$this->fieldManager = new CacheableFetch('FieldManager');
25
			$this->entryManager = new CacheableFetch('EntryManager');
26
			// fix jquery
27
			$this->_Result->setIncludeHeader(false);
28
			$this->addHeaderToPage('Content-Type', 'text/html');
29
		}
30
		
31
		/**
32
		 *
33
		 * Builds the content view
34
		 */
35
		public function view() {
36
			// _context[0] => entry values
37
			// _context[1] => fieldId
38
			if (!is_array($this->_context) || empty($this->_context)) {
39
				$this->_Result->appendChild(new XMLElement('error', __('Parameters not found')));
40
				return;
41
			}
42
			else if (count($this->_context) < self::NUMBER_OF_URL_PARAMETERS) {
43
				$this->_Result->appendChild(new XMLElement('error', __('Not enough parameters')));
44
				return;
45
			}
46
			else if (count($this->_context) > self::NUMBER_OF_URL_PARAMETERS) {
47
				$this->_Result->appendChild(new XMLElement('error', __('Too many parameters')));
48
				return;
49
			}
50
			
51
			$entriesId = explode(',', MySQL::cleanValue($this->_context[0]));
52
			$entriesId = array_map(array('General', 'intval'), $entriesId);
53 View Code Duplication
			if (!is_array($entriesId) || empty($entriesId)) {
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...
54
				$this->_Result->appendChild(new XMLElement('error', __('No entry no found')));
55
				return;
56
			}
57
			
58
			$parentFieldId = General::intval($this->_context[1]);
59
			if ($parentFieldId < 1) {
60
				$this->_Result->appendChild(new XMLElement('error', __('Parent field id not valid')));
61
				return;
62
			}
63
			
64
			$parentField = $this->fieldManager->fetch($parentFieldId);
65 View Code Duplication
			if (!$parentField || empty($parentField)) {
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...
66
				$this->_Result->appendChild(new XMLElement('error', __('Parent field not found')));
67
				return;
68
			}
69
			
70
			if ($parentField->get('type') != 'entry_relationship') {
71
				$this->_Result->appendChild(new XMLElement('error', __('Parent field is `%s`, not `entry_relationship`', array($parentField->get('type')))));
72
				return;
73
			}
74
			
75
			// Get entries one by one since they may belong to
76
			// different sections, which prevents us from
77
			// passing an array of entryId.
78
			foreach ($entriesId as $key => $entryId) {
79
				$entry = $this->entryManager->fetch($entryId);
80
				if (empty($entry)) {
81
					$li = new XMLElement('li', null, array(
82
						'data-entry-id' => $entryId
83
					));
84
					$header = new XMLElement('header', null, array('class' => 'frame-header no-content ignore-collapsible'));
85
					$title = new XMLElement('h4');
86
					$title->appendChild(new XMLElement('strong', __('Entry %s not found', array($entryId))));
87
					$header->appendChild($title);
88
					$options = new XMLElement('div', null, array('class' => 'destructor'));
89
					$options->appendChild(new XMLElement('a', __('Un-link'), array(
90
						'class' => 'unlink ignore-collapsible',
91
						'data-unlink' => $entryId,
92
					)));
93
					$header->appendChild($options);
94
					$li->appendChild($header);
95
					$this->_Result->appendChild($li);
96
				} else {
97
					$entry = $entry[0];
98
					$entrySection = $this->sectionManager->fetch($entry->get('section_id'));
99
					$entryVisibleFields = $entrySection->fetchVisibleColumns();
100
					$entryFields = $entrySection->fetchFields();
101
					$entrySectionHandle = $this->getSectionName($entry, 'handle');
102
					
103
					$li = new XMLElement('li', null, array(
104
						'data-entry-id' => $entryId,
105
						'data-section' => $entrySectionHandle,
106
						'data-section-id' => $entrySection->get('id'),
107
					));
108
					if ($parentField->is('show_header')) {
109
						$header = new XMLElement('header', null, array('class' => 'frame-header'));
110
						$title = new XMLElement('h4', null, array('class' => 'ignore-collapsible'));
111
						if (!$parentField->get('mode_header')) {
112
							$title->appendChildArray($this->buildDefaultTitle($entry, $entryVisibleFields, $entryFields));
113
						}
114
						else {
115
							$title->setValue(ERFXSLTUTilities::entryToXml($parentField, $entry, $entrySectionHandle, $entryFields, 'mode_header'));
116
						}
117
						$header->appendChild($title);
118
						
119
						$options = new XMLElement('div', null, array('class' => 'destructor'));
120
						if ($parentField->is('allow_edit')) {
121
							$title->setAttribute('data-edit', $entryId);
122
							$options->appendChild(new XMLElement('a', __('Edit'), array(
123
								'class' => 'edit ignore-collapsible',
124
								'data-edit' => $entryId,
125
							)));
126
						}
127
						if ($parentField->is('allow_delete')) {
128
							$options->appendChild(new XMLElement('a', __('Delete'), array(
129
								'class' => 'delete ignore-collapsible',
130
								'data-delete' => $entryId,
131
							)));
132
						}
133
						if ($parentField->is('allow_link')) {
134
							$options->appendChild(new XMLElement('a', __('Replace'), array(
135
								'class' => 'unlink ignore-collapsible',
136
								'data-replace' => $entryId,
137
							)));
138
						}
139
						if ($parentField->is('allow_delete') || $parentField->is('allow_link')) {
140
							$options->appendChild(new XMLElement('a', __('Un-link'), array(
141
								'class' => 'unlink ignore-collapsible',
142
								'data-unlink' => $entryId,
143
							)));
144
						}
145
						$header->appendChild($options);
146
						$li->appendChild($header);
147
					}
148
					
149
					$content = ERFXSLTUTilities::entryToXml($parentField, $entry, $entrySectionHandle, $entryFields, 'mode', isset($_REQUEST['debug']));
150
					
151
					if ($content) {
152
						$li->appendChild(new XMLElement('div', $content, array('class' => 'content')));
153
					}
154
					else {
155
						if ($parentField->is('show_header')) {
156
							$header->setAttribute('class', $header->getAttribute('class') . ' no-content');
0 ignored issues
show
Bug introduced by
The variable $header does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
157
						}
158
						else {
159
							$content = new XMLElement('div', null, array('class' => 'content'));
160
							$content->appendChildArray($this->buildDefaultTitle($entry, $entryVisibleFields, $entryFields));
161
							$li->appendChild($content);
162
						}
163
					}
164
					
165
					$this->_Result->appendChild($li);
166
				}
167
			}
168
		}
169
		
170
		public function getSectionName($entry, $name = 'name') {
171
			$sectionId = $entry->get('section_id');
172
			return $this->sectionManager->fetch($sectionId)->get($name);
173
		}
174
		
175
		public function getEntryTitle($entry, $entryVisibleFields, $entryFields) {
176
			$data = $entry->getData();
177
			$field = empty($entryVisibleFields) ? $entryFields : $entryVisibleFields;
178
			if (is_array($field)) {
179
				$field = current($field);
180
			}
181
			
182
			if ($field == null) {
183
				return __('None');
184
			}
185
			
186
			return $field->prepareReadableValue($data[$field->get('id')], $entry->get('id'), true);
187
		}
188
		
189
		public function buildDefaultTitle($entry, $entryVisibleFields, $entryFields) {
190
			return array(
191
				new XMLElement('strong', $this->getEntryTitle($entry, $entryVisibleFields, $entryFields), array('class' => 'ignore-collapsible')),
192
				new XMLElement('span', $this->getSectionName($entry), array('class' => 'ignore-collapsible'))
193
			);
194
		}
195
	}