Email::test()   C
last analyzed

Complexity

Conditions 12
Paths 130

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 130
nop 5
dl 0
loc 49
rs 6.7166
c 0
b 0
f 0

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
 * 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\Rule;
10
11
use Joomla\Form\Rule;
12
use Joomla\Form\Form;
13
use Joomla\Registry\Registry;
14
use SimpleXMLElement;
15
16
/**
17
 * Form Rule class for the Joomla Framework.
18
 *
19
 * @since       1.0
20
 * @deprecated  The joomla/form package is deprecated
21
 */
22
class Email extends Rule
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Form\Rule 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...
23
{
24
	/**
25
	 * The regular expression to use in testing a form field value.
26
	 *
27
	 * @var    string
28
	 * @since  1.0
29
	 * @see    http://www.w3.org/TR/html-markup/input.email.html
30
	 */
31
	protected $regex = '^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$';
32
33
	/**
34
	 * Method to test the email address and optionally check for uniqueness.
35
	 *
36
	 * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the <field /> tag for the form field object.
37
	 * @param   mixed             $value    The form field value to validate.
38
	 * @param   string            $group    The field name group control value. This acts as as an array container for the field.
39
	 *                                      For example if the field has name="foo" and the group value is set to "bar" then the
40
	 *                                      full field name would end up being "bar[foo]".
41
	 * @param   Registry          $input    An optional Registry object with the entire data set to validate against the entire form.
42
	 * @param   Form              $form     The form object for which the field is being tested.
43
	 *
44
	 * @return  boolean  True if the value is valid, false otherwise.
45
	 *
46
	 * @since   1.0
47
	 */
48
	public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
49
	{
50
		// If the field is empty and not required, the field is valid.
51
		$required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
52
53
		if (!$required && empty($value))
54
		{
55
			return true;
56
		}
57
58
		// If the tld attribute is present, change the regular expression to require at least 2 characters for it.
59
		$tld = ((string) $element['tld'] == 'tld' || (string) $element['tld'] == 'required');
60
61
		if ($tld)
62
		{
63
			$this->regex = '^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])'
64
				. '?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$';
65
		}
66
67
		// Determine if the multiple attribute is present
68
		$multiple = ((string) $element['multiple'] == 'true' || (string) $element['multiple'] == 'multiple');
69
70
		if ($multiple)
71
		{
72
			$values = explode(',', $value);
73
		}
74
75
		if (!$multiple)
76
		{
77
			// Test the value against the regular expression.
78
			if (!parent::test($element, $value, $group, $input, $form))
79
			{
80
				return false;
81
			}
82
		}
83
		else
84
		{
85
			foreach ($values as $value)
0 ignored issues
show
Bug introduced by
The variable $values 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...
86
			{
87
				// Test the value against the regular expression.
88
				if (!parent::test($element, $value, $group, $input, $form))
89
				{
90
					return false;
91
				}
92
			}
93
		}
94
95
		return true;
96
	}
97
}
98