Field_DatabaseConnection::getOptions()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 0
dl 0
loc 42
rs 8.6257
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\Language\Text;
12
use Joomla\Database\DatabaseDriver;
13
14
FormHelper::loadFieldClass('list');
15
16
/**
17
 * Form Field class for the Joomla Framework.
18
 * Provides a list of available database connections, optionally limiting to
19
 * a given list.
20
 *
21
 * @see         Joomla\Database\DatabaseDriver
22
 * @since       1.0
23
 * @deprecated  The joomla/form package is deprecated
24
 */
25
class Field_DatabaseConnection 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...
26
{
27
	/**
28
	 * The form field type.
29
	 *
30
	 * @var    string
31
	 * @since  1.0
32
	 */
33
	public $type = 'DatabaseConnection';
34
35
	/**
36
	 * Method to get the list of database options.
37
	 *
38
	 * This method produces a drop down list of available databases supported
39
	 * by JDatabaseDriver classes that are also supported by the application.
40
	 *
41
	 * @return  array    The field option objects.
42
	 *
43
	 * @since   1.0
44
	 * @see		Joomla\Database\DatabaseDriver
45
	 */
46
	protected function getOptions()
47
	{
48
		// This gets the connectors available in the platform and supported by the server.
49
		$available = DatabaseDriver::getConnectors();
50
51
		/**
52
		 * This gets the list of database types supported by the application.
53
		 * This should be entered in the form definition as a comma separated list.
54
		 * If no supported databases are listed, it is assumed all available databases
55
		 * are supported.
56
		 */
57
		$supported = $this->element['supported'];
58
59
		if (!empty($supported))
60
		{
61
			$supported = explode(',', $supported);
62
63
			foreach ($supported as $support)
64
			{
65
				if (in_array($support, $available))
66
				{
67
					$options[$support] = Text::_(ucfirst($support));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

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...
68
				}
69
			}
70
		}
71
		else
72
		{
73
			foreach ($available as $support)
74
			{
75
				$options[$support] = Text::_(ucfirst($support));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

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...
76
			}
77
		}
78
79
		// This will come into play if an application is installed that requires
80
		// a database that is not available on the server.
81
		if (empty($options))
82
		{
83
			$options[''] = Text::_('JNONE');
0 ignored issues
show
Bug introduced by
The variable $options 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...
Deprecated Code introduced by
The method Joomla\Language\Text::_() has been deprecated with message: 2.0 Will be replaced with a `translate` method.

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...
84
		}
85
86
		return $options;
87
	}
88
}
89