Field_FileList::getOptions()   B
last analyzed

Complexity

Conditions 9
Paths 16

Size

Total Lines 62

Duplication

Lines 15
Ratio 24.19 %

Importance

Changes 0
Metric Value
cc 9
nc 16
nop 0
dl 15
loc 62
rs 7.2735
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Form\Html\Select as HtmlSelect;
12
use Joomla\Language\Text;
13
use Joomla\Filesystem\File;
14
use Joomla\Filesystem\Folder;
15
16
FormHelper::loadFieldClass('list');
17
18
/**
19
 * Supports an HTML select list of files
20
 *
21
 * @since       1.0
22
 * @deprecated  The joomla/form package is deprecated
23
 */
24
class Field_FileList extends Field_List
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Form\Field_List has been deprecated with message: The joomla/form package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
25
{
26
	/**
27
	 * The form field type.
28
	 *
29
	 * @var    string
30
	 * @since  1.0
31
	 */
32
	public $type = 'FileList';
33
34
	/**
35
	 * Method to get the list of files for the field options.
36
	 * Specify the target directory with a directory attribute
37
	 * Attributes allow an exclude mask and stripping of extensions from file name.
38
	 * Default attribute may optionally be set to null (no file) or -1 (use a default).
39
	 *
40
	 * @return  array  The field option objects.
41
	 *
42
	 * @since   1.0
43
	 */
44
	protected function getOptions()
45
	{
46
		$options = array();
47
48
		// Initialize some field attributes.
49
		$filter = (string) $this->element['filter'];
50
		$exclude = (string) $this->element['exclude'];
51
		$stripExt = (string) $this->element['stripext'];
52
		$hideNone = (string) $this->element['hide_none'];
53
		$hideDefault = (string) $this->element['hide_default'];
54
55
		// Get the path in which to search for file options.
56
		$path = (string) $this->element['directory'];
57
58
		if (!is_dir($path))
59
		{
60
			$path = JPATH_ROOT . '/' . $path;
61
		}
62
63
		// Prepend some default options based on field attributes.
64 View Code Duplication
		if (!$hideNone)
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...
65
		{
66
			$options[] = HtmlSelect::option('-1', Text::alt('JOPTION_DO_NOT_USE', preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)));
67
		}
68
69 View Code Duplication
		if (!$hideDefault)
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...
70
		{
71
			$options[] = HtmlSelect::option('', Text::alt('JOPTION_USE_DEFAULT', preg_replace('/[^a-zA-Z0-9_\-]/', '_', $this->fieldname)));
72
		}
73
74
		// Get a list of files in the search path with the given filter.
75
		$files = Folder::files($path, $filter);
76
77
		// Build the options list from the list of files.
78
		if (is_array($files))
79
		{
80
			foreach ($files as $file)
81
			{
82
				// Check to see if the file is in the exclude mask.
83 View Code Duplication
				if ($exclude)
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...
84
				{
85
					if (preg_match(chr(1) . $exclude . chr(1), $file))
86
					{
87
						continue;
88
					}
89
				}
90
91
				// If the extension is to be stripped, do it.
92
				if ($stripExt)
93
				{
94
					$file = File::stripExt($file);
95
				}
96
97
				$options[] = HtmlSelect::option($file, $file);
98
			}
99
		}
100
101
		// Merge any additional options in the XML definition.
102
		$options = array_merge(parent::getOptions(), $options);
103
104
		return $options;
105
	}
106
}
107