Tel::test()   C
last analyzed

Complexity

Conditions 14
Paths 22

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 22
nop 5
dl 0
loc 69
rs 5.6896
c 0
b 0
f 0

How to fix   Long Method    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 Tel 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
	 * Method to test the url for a valid parts.
26
	 *
27
	 * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the <field /> tag for the form field object.
28
	 * @param   mixed             $value    The form field value to validate.
29
	 * @param   string            $group    The field name group control value. This acts as as an array container for the field.
30
	 *                                      For example if the field has name="foo" and the group value is set to "bar" then the
31
	 *                                      full field name would end up being "bar[foo]".
32
	 * @param   Registry          $input    An optional Registry object with the entire data set to validate against the entire form.
33
	 * @param   Form              $form     The form object for which the field is being tested.
34
	 *
35
	 * @return  boolean  True if the value is valid, false otherwise.
36
	 *
37
	 * @since   1.0
38
	 */
39
	public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
40
	{
41
		// If the field is empty and not required, the field is valid.
42
		$required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
43
44
		if (!$required && empty($value))
45
		{
46
			return true;
47
		}
48
49
		/*
50
		 * @see http://www.nanpa.com/
51
		 * @see http://tools.ietf.org/html/rfc4933
52
		 * @see http://www.itu.int/rec/T-REC-E.164/en
53
		 *
54
		 * Regex by Steve Levithan
55
		 * @see http://blog.stevenlevithan.com/archives/validate-phone-number
56
		 * @note that valid ITU-T and EPP must begin with +.
57
		 */
58
		$regexarray = array('NANP' => '/^(?:\+?1[-. ]?)?\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/',
59
			'ITU-T' => '/^\+(?:[0-9] ?){6,14}[0-9]$/', 'EPP' => '/^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$/');
60
61
		if (isset($element['plan']))
62
		{
63
			$plan = (string) $element['plan'];
64
65
			if ($plan == 'northamerica' || $plan == 'us')
66
			{
67
				$plan = 'NANP';
68
			}
69
			elseif ($plan == 'International' || $plan == 'int' || $plan == 'missdn' || !$plan)
70
			{
71
				$plan = 'ITU-T';
72
			}
73
			elseif ($plan == 'IETF')
74
			{
75
				$plan = 'EPP';
76
			}
77
78
			$regex = $regexarray[$plan];
79
80
			// Test the value against the regular expression.
81
			if (preg_match($regex, $value) == false)
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match($regex, $value) of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
82
			{
83
				return false;
84
			}
85
		}
86
		else
87
		{
88
			/*
89
			 * If the rule is set but no plan is selected just check that there are between
90
			 * 7 and 15 digits inclusive and no illegal characters (but common number separators
91
			 * are allowed).
92
			 */
93
			$cleanvalue = preg_replace('/[+. \-(\)]/', '', $value);
94
			$regex = '/^[0-9]{7,15}?$/';
95
96
			if (preg_match($regex, $cleanvalue) == true)
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match($regex, $cleanvalue) of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
97
			{
98
				return true;
99
			}
100
			else
101
			{
102
				return false;
103
			}
104
		}
105
106
		return true;
107
	}
108
}
109