UrlSchemeValidators::getValidator()   D
last analyzed

Complexity

Conditions 22
Paths 22

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 4.1666
c 0
b 0
f 0
cc 22
nc 22
nop 1

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
namespace Wikibase\Repo\Validators;
4
5
use Parser;
6
use ValueValidators\ValueValidator;
7
8
/**
9
 * UrlSchemeValidators is a collection of validators for some commonly used URL schemes.
10
 * This is intended for conveniently supplying a map of validators to UrlValidator.
11
 *
12
 * @license GPL-2.0-or-later
13
 * @author Daniel Kinzler
14
 * @author Thiemo Kreuz
15
 */
16
class UrlSchemeValidators {
17
18
	/**
19
	 * Returns a validator for the given URL scheme, or null if
20
	 * no validator is defined for that scheme.
21
	 *
22
	 * @todo 'bitcoin', 'geo', 'magnet', 'news', 'sip', 'sips', 'sms', 'tel', 'urn', 'xmpp'.
23
	 * @todo protocol relative '//'.
24
	 *
25
	 * @param string $scheme e.g. 'http'.
26
	 *
27
	 * @return ValueValidator|null
28
	 */
29
	public function getValidator( $scheme ) {
30
		switch ( $scheme ) {
31
			// Schemes with "://", copied from $wgUrlProtocols in MediaWiki's DefaultSettings.php
32
			case 'ftp':
33
			case 'ftps':
34
			case 'git':
35
			case 'gopher':
36
			case 'http':
37
			case 'https':
38
			case 'irc':
39
			case 'ircs':
40
			case 'mms':
41
			case 'nntp':
42
			case 'redis':
43
			case 'sftp':
44
			case 'ssh':
45
			case 'svn':
46
			case 'telnet':
47
			case 'worldwind':
48
49
			// Additional VCS schemes not supported by MediaWiki. "bzr" is GNU Bazaar from Canonical
50
			// Ltd. See https://phabricator.wikimedia.org/T146692
51
			case 'bzr':
52
			case 'cvs':
53
				$regex = '!^' . preg_quote( $scheme, '!' ) . '://(' . Parser::EXT_LINK_URL_CLASS
54
					. ')+\z!ui';
55
				break;
56
57
			case 'mailto':
58
				$regex = '!^mailto:(' . Parser::EXT_LINK_URL_CLASS . ')+@('
59
					. Parser::EXT_LINK_URL_CLASS . ')+\z!ui';
60
				break;
61
62
			case '*':
63
			case 'any':
64
				$regex = '!^([a-z][a-z\d+.-]*):(' . Parser::EXT_LINK_URL_CLASS . ')+\z!ui';
65
				break;
66
67
			default:
68
				return null;
69
		}
70
71
		return new RegexValidator( $regex, false, 'bad-url' );
72
	}
73
74
	/**
75
	 * Given a list of schemes, this function returns a mapping for each supported
76
	 * scheme to a corresponding ValueValidator. If the schema isn't supported,
77
	 * no mapping is created for it.
78
	 *
79
	 * @param string[] $schemes a list of scheme names, e.g. 'http'.
80
	 *
81
	 * @return ValueValidator[] a map of scheme names to ValueValidator objects.
82
	 */
83
	public function getValidators( array $schemes ) {
84
		$validators = [];
85
86
		foreach ( $schemes as $scheme ) {
87
			$validator = $this->getValidator( $scheme );
88
89
			if ( $validator !== null ) {
90
				$validators[$scheme] = $validator;
91
			}
92
		}
93
94
		return $validators;
95
	}
96
97
}
98