Passed
Push — master ( 3c6756...8b3b37 )
by Michael
14:24 queued 07:02
created

FileSelectField::init()   B

Complexity

Conditions 11
Paths 12

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 12
c 2
b 0
f 0
nc 12
nop 1
dl 0
loc 19
rs 7.3166

How to fix   Complexity   

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
	 * @package Demos
4
	 */
5
	class FileSelectField extends Field
6
	{
7
		public $request;
8
		public $files = array();
9
		public $options;
10
		
11
		function __construct($name, $path, $options = array())
12
		{
13
			$this->name = $name;
14
			$this->path = $path;
0 ignored issues
show
Bug Best Practice introduced by
The property path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
15
			$this->options = $options;
16
			
17
			if (!isset($options['show']))
18
				$this->options['show'] = true;
19
			
20
			if (!isset($options['pattern']))
21
				$this->options['pattern'] = '/(.*)/';
22
		}
23
		
24
		function init($request)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
		{
26
			$this->value = null;
27
			$di = new DirectoryIterator(DEMO_PATH . $this->path);
28
			foreach ($di as $file)
29
				if (!$file->isDot() && strpos($file->getFilename(), '.') !== 0 && preg_match($this->options['pattern'], $file->getFilename()))
30
				{
31
					$this->files[] = $file->getFilename();
32
					if ($this->value === null && isset($this->options['default']) && $this->options['default'] == $file->getFilename())
33
						$this->value = $this->options['default'];
34
					
35
					if ($this->request->get($this->name) == $file->getFilename())
36
						$this->value = $file->getFilename();
37
				}
38
			
39
			sort($this->files);
40
			
41
			if (!$this->value && count($this->files) > 0)
42
				$this->value = $this->files[0];
43
		}
44
		
45
		function renderBody($name, $id)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
46
		{
47
			if ($this->options['show'])
48
			{
49
				$onch = "document.getElementById('sel_{$id}').src = '{$this->path}/' + this.options[this.selectedIndex].value;";
50
			}
51
			else
52
				$onch = '';
53
			
54
			echo '<select id="' . $id . '" name="' . $name . '" onchange="' . $onch . '">';
55
			$selected_file = null;
56
			foreach ($this->files as $file)
57
			{
58
				if ($this->value == $file)
59
				{
60
					$sel = 'selected="selected"';
61
					$selected_file = $file;
62
				}
63
				else
64
					$sel = '';
65
				
66
				echo '<option ' . $sel . ' value="' . $file . '">' . $file . '</option>' . PHP_EOL;
67
			}
68
			echo '</select>';
69
			
70
			if ($this->options['show'] && $selected_file)
71
			{
72
				echo '<div style="display: inline; min-width: 50px; min-height: 50px">';
73
				echo '<img style="position: absolute" id="sel_' . $id . '" width="50" src="' . $this->path . '/' . $selected_file . '" /> ';
74
				echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
75
				echo '</div>';
76
			}
77
		}
78
		
79
		function getURLValue()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
80
		{
81
			return $this->name . '=' . $this->value;
82
		}
83
	}
84