StickyForms   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 134
Duplicated Lines 30.6 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 41
loc 134
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A makeStickyForm() 0 23 3
A clearStickyForm() 6 6 1
A isStickyForm() 0 5 1
A getStickyValue() 13 13 3
A getStickyValues() 16 16 4
A clearStickyValue() 6 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
namespace Elgg\Forms;
3
4
/**
5
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
6
 *
7
 * @package    Elgg.Core
8
 * @subpackage Forms
9
 * @since      1.10.0
10
 *
11
 * @access private
12
 */
13
class StickyForms {
14
	
15
	/**
16
	 * Save form submission data (all GET and POST vars) into a session cache
17
	 *
18
	 * Call this from an action when you want all your submitted variables
19
	 * available if the submission fails validation and is sent back to the form
20
	 *
21
	 * @param string $form_name Name of the sticky form
22
	 *
23
	 * @return void
24
	 */
25
	public function makeStickyForm($form_name) {
26
27
		$banned_keys = [];
28
		// TODO make $banned_keys an argument
29
		if (in_array($form_name, ['register', 'useradd', 'usersettings'])) {
30
			$banned_keys = ['password', 'password2'];
31
		}
32
33
		elgg_clear_sticky_form($form_name);
34
	
35
		$session = _elgg_services()->session;
36
		$data = $session->get('sticky_forms', array());
37
		$req = _elgg_services()->request;
38
	
39
		// will go through XSS filtering in elgg_get_sticky_value()
40
		$vars = array_merge($req->query->all(), $req->request->all());
41
		foreach ($banned_keys as $key) {
42
			unset($vars[$key]);
43
		}
44
		$data[$form_name] = $vars;
45
	
46
		$session->set('sticky_forms', $data);
47
	}
48
	
49
	/**
50
	 * Remove form submission data from the session
51
	 *
52
	 * Call this if validation is successful in the action handler or
53
	 * when they sticky values have been used to repopulate the form
54
	 * after a validation error.
55
	 *
56
	 * @param string $form_name Form namespace
57
	 *
58
	 * @return void
59
	 */
60 View Code Duplication
	function clearStickyForm($form_name) {
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...
61
		$session = _elgg_services()->session;
62
		$data = $session->get('sticky_forms', array());
63
		unset($data[$form_name]);
64
		$session->set('sticky_forms', $data);
65
	}
66
	
67
	/**
68
	 * Does form submission data exist for this form?
69
	 *
70
	 * @param string $form_name Form namespace
71
	 *
72
	 * @return boolean
73
	 */
74
	function isStickyForm($form_name) {
75
		$session = _elgg_services()->session;
76
		$data = $session->get('sticky_forms', array());
77
		return isset($data[$form_name]);
78
	}
79
	
80
	/**
81
	 * Get a specific value from cached form submission data
82
	 *
83
	 * @param string  $form_name     The name of the form
84
	 * @param string  $variable      The name of the variable
85
	 * @param mixed   $default       Default value if the variable does not exist in sticky cache
86
	 * @param boolean $filter_result Filter for bad input if true
87
	 *
88
	 * @return mixed
89
	 *
90
	 * @todo should this filter the default value?
91
	 */
92 View Code Duplication
	function getStickyValue($form_name, $variable = '', $default = null, $filter_result = true) {
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...
93
		$session = _elgg_services()->session;
94
		$data = $session->get('sticky_forms', array());
95
		if (isset($data[$form_name][$variable])) {
96
			$value = $data[$form_name][$variable];
97
			if ($filter_result) {
98
				// XSS filter result
99
				$value = filter_tags($value);
100
			}
101
			return $value;
102
		}
103
		return $default;
104
	}
105
	
106
	/**
107
	 * Get all submission data cached for a form
108
	 *
109
	 * @param string $form_name     The name of the form
110
	 * @param bool   $filter_result Filter for bad input if true
111
	 *
112
	 * @return array
113
	 */
114 View Code Duplication
	function getStickyValues($form_name, $filter_result = true) {
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...
115
		$session = _elgg_services()->session;
116
		$data = $session->get('sticky_forms', array());
117
		if (!isset($data[$form_name])) {
118
			return array();
119
		}
120
	
121
		$values = $data[$form_name];
122
		if ($filter_result) {
123
			foreach ($values as $key => $value) {
124
				// XSS filter result
125
				$values[$key] = filter_tags($value);
126
			}
127
		}
128
		return $values;
129
	}
130
	
131
	/**
132
	 * Remove one value of form submission data from the session
133
	 *
134
	 * @param string $form_name The name of the form
135
	 * @param string $variable  The name of the variable to clear
136
	 *
137
	 * @return void
138
	 */
139 View Code Duplication
	function clearStickyValue($form_name, $variable) {
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...
140
		$session = _elgg_services()->session;
141
		$data = $session->get('sticky_forms', array());
142
		unset($data[$form_name][$variable]);
143
		$session->set('sticky_forms', $data);
144
	}
145
	
146
}