FormHelper::addRulePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Part of the Joomla Framework Form Package
4
 *
5
 * @copyright  Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
6
 * @license    GNU General Public License version 2 or later; see LICENSE
7
 */
8
9
namespace Joomla\Form;
10
11
use Joomla\Filesystem\Path;
12
13
/**
14
 * Helper class for the Form package.
15
 * Provides a storage for filesystem's paths where Joomla\Form entities reside and methods for creating those entities.
16
 * Also stores objects with entities' prototypes for further reusing.
17
 *
18
 * @since       1.0
19
 * @deprecated  The joomla/form package is deprecated
20
 */
21
class FormHelper
22
{
23
	/**
24
	 * Array with paths where entities(field, rule, form) can be found.
25
	 *
26
	 * Array's structure:
27
	 * <code>
28
	 * paths:
29
	 * {ENTITY_NAME}:
30
	 * - /path/1
31
	 * - /path/2
32
	 * </code>
33
	 *
34
	 * @var    array
35
	 * @since  1.0
36
	 *
37
	 */
38
	protected static $paths;
39
40
	/**
41
	 * Static array of Joomla\Form's entity objects for re-use.
42
	 * Prototypes for all fields and rules are here.
43
	 *
44
	 * Array's structure:
45
	 * <code>
46
	 * entities:
47
	 * {ENTITY_NAME}:
48
	 * {KEY}: {OBJECT}
49
	 * </code>
50
	 *
51
	 * @var    array
52
	 * @since  1.0
53
	 * @deprecated  2.0  Objects should be autoloaded
54
	 */
55
	protected static $entities = array();
56
57
	/**
58
	 * Method to load a form field object given a type.
59
	 *
60
	 * @param   string   $type  The field type.
61
	 * @param   boolean  $new   Flag to toggle whether we should get a new instance of the object.
62
	 *
63
	 * @return  mixed  Field object on success, false otherwise.
64
	 *
65
	 * @since   1.0
66
	 * @deprecated  2.0  Field objects should be autoloaded
67
	 */
68
	public static function loadFieldType($type, $new = true)
69
	{
70
		return self::loadType('field', $type, $new);
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Form\FormHelper::loadType() has been deprecated with message: 2.0 Objects should be autoloaded

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
71
	}
72
73
	/**
74
	 * Method to load a form rule object given a type.
75
	 *
76
	 * @param   string   $type  The rule type.
77
	 * @param   boolean  $new   Flag to toggle whether we should get a new instance of the object.
78
	 *
79
	 * @return  mixed  Rule object on success, false otherwise.
80
	 *
81
	 * @since   1.0
82
	 * @deprecated  2.0  Rule objects should be autoloaded
83
	 */
84
	public static function loadRuleType($type, $new = true)
85
	{
86
		return self::loadType('rule', $type, $new);
0 ignored issues
show
Deprecated Code introduced by
The method Joomla\Form\FormHelper::loadType() has been deprecated with message: 2.0 Objects should be autoloaded

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
87
	}
88
89
	/**
90
	 * Method to load a form entity object given a type.
91
	 * Each type is loaded only once and then used as a prototype for other objects of same type.
92
	 * Please, use this method only with those entities which support types (forms don't support them).
93
	 *
94
	 * @param   string   $entity  The entity.
95
	 * @param   string   $type    The entity type.
96
	 * @param   boolean  $new     Flag to toggle whether we should get a new instance of the object.
97
	 *
98
	 * @return  mixed  Entity object on success, false otherwise.
99
	 *
100
	 * @since   1.0
101
	 * @deprecated  2.0  Objects should be autoloaded
102
	 */
103
	protected static function loadType($entity, $type, $new = true)
104
	{
105
		// Reference to an array with current entity's type instances
106
		$types = &self::$entities[$entity];
0 ignored issues
show
Deprecated Code introduced by
The property Joomla\Form\FormHelper::$entities has been deprecated with message: 2.0 Objects should be autoloaded

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
107
108
		$key = md5($type);
109
110
		// Return an entity object if it already exists and we don't need a new one.
111
		if (isset($types[$key]) && $new === false)
112
		{
113
			return $types[$key];
114
		}
115
116
		$class = self::loadClass($entity, $type);
117
118
		if ($class !== false)
119
		{
120
			// Instantiate a new type object.
121
			$types[$key] = new $class;
122
123
			return $types[$key];
124
		}
125
		else
126
		{
127
			return false;
128
		}
129
	}
130
131
	/**
132
	 * Attempt to import the Field class file if it isn't already imported.
133
	 * You can use this method outside of Joomla\Form for loading a field for inheritance or composition.
134
	 *
135
	 * @param   string  $type  Type of a field whose class should be loaded.
136
	 *
137
	 * @return  mixed  Class name on success or false otherwise.
138
	 *
139
	 * @since   1.0
140
	 */
141
	public static function loadFieldClass($type)
142
	{
143
		return self::loadClass('field', $type);
144
	}
145
146
	/**
147
	 * Attempt to import the Rule class file if it isn't already imported.
148
	 * You can use this method outside of Joomla\Form for loading a rule for inheritance or composition.
149
	 *
150
	 * @param   string  $type  Type of a rule whose class should be loaded.
151
	 *
152
	 * @return  mixed  Class name on success or false otherwise.
153
	 *
154
	 * @since   1.0
155
	 */
156
	public static function loadRuleClass($type)
157
	{
158
		return self::loadClass('rule', $type);
159
	}
160
161
	/**
162
	 * Load a class for one of the form's entities of a particular type.
163
	 * Currently, it makes sense to use this method for the "field" and "rule" entities
164
	 * (but you can support more entities in your subclass).
165
	 *
166
	 * @param   string  $entity  One of the form entities (field or rule).
167
	 * @param   string  $type    Type of an entity.
168
	 *
169
	 * @return  mixed  Class name on success or false otherwise.
170
	 *
171
	 * @since   1.0
172
	 */
173
	protected static function loadClass($entity, $type)
174
	{
175
		if (strpos($type, '.'))
176
		{
177
			list($prefix, $type) = explode('.', $type);
178
		}
179
		else
180
		{
181
			$prefix = 'Joomla';
182
		}
183
184
		$class = ucfirst($prefix) . '\\Form\\' . ucfirst($entity);
185
186
		if ($entity === 'field')
187
		{
188
			$class .= '_' . ucfirst($type);
189
		}
190
		else
191
		{
192
			$class .= '\\' . ucfirst($type);
193
		}
194
195
		if (class_exists($class))
196
		{
197
			return $class;
198
		}
199
200
		/*
201
		 * @deprecated 2.0 - Classes should be autoloaded, path lookups will no longer be supported.
202
		 */
203
204
		// Get the field search path array.
205
		$paths = self::addPath($entity);
206
207
		// If the type is complex, add the base type to the paths.
208
		if ($pos = strpos($type, '_'))
209
		{
210
			// Add the complex type prefix to the paths.
211
			for ($i = 0, $n = count($paths); $i < $n; $i++)
212
			{
213
				// Derive the new path.
214
				$path = $paths[$i] . '/' . strtolower(substr($type, 0, $pos));
215
216
				// If the path does not exist, add it.
217
				if (!in_array($path, $paths))
218
				{
219
					$paths[] = $path;
220
				}
221
			}
222
223
			// Break off the end of the complex type.
224
			$type = substr($type, $pos + 1);
225
		}
226
227
		// Try to find the class file.
228
		$type = strtolower($type) . '.php';
229
230
		foreach ($paths as $path)
231
		{
232
			if ($file = Path::find($path, $type))
233
			{
234
				require_once $file;
235
236
				if (class_exists($class))
237
				{
238
					break;
239
				}
240
			}
241
		}
242
243
		// Check for all if the class exists.
244
		return class_exists($class) ? $class : false;
245
	}
246
247
	/**
248
	 * Method to add a path to the list of field include paths.
249
	 *
250
	 * @param   mixed  $new  A path or array of paths to add.
251
	 *
252
	 * @return  array  The list of paths that have been added.
253
	 *
254
	 * @since   1.0
255
	 * @deprecated  2.0  Field objects should be autoloaded
256
	 */
257
	public static function addFieldPath($new = null)
258
	{
259
		return self::addPath('field', $new);
260
	}
261
262
	/**
263
	 * Method to add a path to the list of form include paths.
264
	 *
265
	 * @param   mixed  $new  A path or array of paths to add.
266
	 *
267
	 * @return  array  The list of paths that have been added.
268
	 *
269
	 * @since   1.0
270
	 */
271
	public static function addFormPath($new = null)
272
	{
273
		return self::addPath('form', $new);
274
	}
275
276
	/**
277
	 * Method to add a path to the list of rule include paths.
278
	 *
279
	 * @param   mixed  $new  A path or array of paths to add.
280
	 *
281
	 * @return  array  The list of paths that have been added.
282
	 *
283
	 * @since   1.0
284
	 * @deprecated  2.0  Rule objects should be autoloaded
285
	 */
286
	public static function addRulePath($new = null)
287
	{
288
		return self::addPath('rule', $new);
289
	}
290
291
	/**
292
	 * Method to add a path to the list of include paths for one of the form's entities.
293
	 * Currently supported entities: field, rule and form. You are free to support your own in a subclass.
294
	 *
295
	 * @param   string  $entity  Form's entity name for which paths will be added.
296
	 * @param   mixed   $new     A path or array of paths to add.
297
	 *
298
	 * @return  array  The list of paths that have been added.
299
	 *
300
	 * @since   1.0
301
	 */
302
	protected static function addPath($entity, $new = null)
303
	{
304
		// Reference to an array with paths for current entity
305
		$paths = &self::$paths[$entity];
306
307
		// Add the default entity's search path if not set.
308
		if (empty($paths))
309
		{
310
			// While we support limited number of entities (form, field and rule)
311
			// we can do this simple pluralisation:
312
			$entity_plural = $entity . 's';
0 ignored issues
show
Unused Code introduced by
$entity_plural is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
313
314
			/*
315
			 * But when someday we would want to support more entities, then we should consider adding
316
			 * an inflector class to "libraries/joomla/utilities" and use it here (or somebody can use a real inflector in his subclass).
317
			 * See also: pluralization snippet by Paul Osman in JControllerForm's constructor.
318
			 */
319
			$paths[] = __DIR__ . '/' . $entity;
320
		}
321
322
		// Force the new path(s) to an array.
323
		settype($new, 'array');
324
325
		// Add the new paths to the stack if not already there.
326
		foreach ($new as $path)
327
		{
328
			if (!in_array($path, $paths))
329
			{
330
				array_unshift($paths, trim($path));
331
			}
332
333
			if (!is_dir($path))
334
			{
335
				array_unshift($paths, trim($path));
336
			}
337
		}
338
339
		return $paths;
340
	}
341
}
342