Url   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 89
Duplicated Lines 8.99 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 8
loc 89
rs 10
c 0
b 0
f 0
wmc 16
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
C test() 8 69 16

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Form;
12
use Joomla\Form\Rule;
13
use Joomla\String\StringHelper;
14
use Joomla\Uri\UriHelper;
15
use Joomla\Registry\Registry;
16
use SimpleXMLElement;
17
18
/**
19
 * Form Rule class for the Joomla Framework.
20
 *
21
 * @since       1.0
22
 * @deprecated  The joomla/form package is deprecated
23
 */
24
class Url 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...
25
{
26
	/**
27
	 * Method to test an external url for a valid parts.
28
	 *
29
	 * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the <field /> tag for the form field object.
30
	 * @param   mixed             $value    The form field value to validate.
31
	 * @param   string            $group    The field name group control value. This acts as as an array container for the field.
32
	 *                                      For example if the field has name="foo" and the group value is set to "bar" then the
33
	 *                                      full field name would end up being "bar[foo]".
34
	 * @param   Registry          $input    An optional Registry object with the entire data set to validate against the entire form.
35
	 * @param   Form              $form     The form object for which the field is being tested.
36
	 *
37
	 * @return  boolean  True if the value is valid, false otherwise.
38
	 *
39
	 * @since   1.0
40
	 * @link    http://www.w3.org/Addressing/URL/url-spec.txt
41
	 * @see	    Jstring
42
	 */
43
	public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
44
	{
45
		// If the field is empty and not required, the field is valid.
46
		$required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
47
48
		if (!$required && empty($value))
49
		{
50
			return true;
51
		}
52
53
		$urlParts = UriHelper::parse_url($value);
54
55
		// See http://www.w3.org/Addressing/URL/url-spec.txt
56
		// Use the full list or optionally specify a list of permitted schemes.
57
		if ($element['schemes'] == '')
58
		{
59
			$scheme = array('http', 'https', 'ftp', 'ftps', 'gopher', 'mailto', 'news', 'prospero', 'telnet', 'rlogin', 'tn3270', 'wais', 'url',
60
				'mid', 'cid', 'nntp', 'tel', 'urn', 'ldap', 'file', 'fax', 'modem', 'git');
61
		}
62
		else
63
		{
64
			$scheme = explode(',', $element['schemes']);
65
		}
66
67
		/*
68
		 * This rule is only for full URLs with schemes because parse_url does not parse
69
		 * accurately without a scheme.
70
		 * @see http://php.net/manual/en/function.parse-url.php
71
		 */
72
		if ($urlParts && !array_key_exists('scheme', $urlParts))
73
		{
74
			return false;
75
		}
76
77
		$urlScheme = (string) $urlParts['scheme'];
78
		$urlScheme = strtolower($urlScheme);
79
80
		if (in_array($urlScheme, $scheme) == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
81
		{
82
			return false;
83
		}
84
85
		// For some schemes here must be two slashes.
86
		$scheme = array('http', 'https', 'ftp', 'ftps', 'gopher', 'wais', 'prospero', 'sftp', 'telnet', 'git');
87
88
		if (in_array($urlScheme, $scheme) && substr($value, strlen($urlScheme), 3) !== '://')
89
		{
90
			return false;
91
		}
92
93
		// The best we can do for the rest is make sure that the strings are valid UTF-8
94
		// and the port is an integer.
95 View Code Duplication
		if (array_key_exists('host', $urlParts) && !StringHelper::valid((string) $urlParts['host']))
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...
96
		{
97
			return false;
98
		}
99
100
		if (array_key_exists('port', $urlParts) && !is_int((int) $urlParts['port']))
101
		{
102
			return false;
103
		}
104
105 View Code Duplication
		if (array_key_exists('path', $urlParts) && !StringHelper::valid((string) $urlParts['path']))
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...
106
		{
107
			return false;
108
		}
109
110
		return true;
111
	}
112
}
113