Validator::get()   C
last analyzed

Complexity

Conditions 11
Paths 9

Size

Total Lines 31
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 11
nc 9
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
/**
4
 * @package Cadmium\Framework\Dataset
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Dataset {
11
12
	class Validator {
13
14
		/**
15
		 * Get a validator for a given value type
16
		 */
17
18
		public static function get($default) : callable {
19
20
			# Check basic types
21
22
			if (is_string   ($default)) return function (string     $value) { return $value; };
23
24
			if (is_bool     ($default)) return function (bool       $value) { return $value; };
25
26
			if (is_int      ($default)) return function (int        $value) { return $value; };
27
28
			if (is_float    ($default)) return function (float      $value) { return $value; };
29
30
			if (is_array    ($default)) return function (array      $value) { return $value; };
31
32
			if (is_callable ($default)) return function (callable   $value) { return $value; };
33
34
			# Check object type
35
36
			if (is_object($default)) return function ($value) use ($default) {
37
38
				return (is_a($value, get_class($default)) ? $value : null);
39
			};
40
41
			# Check resource type
42
43
			if (is_resource($default)) return function ($value) { return (is_resource($value) ? $value : null); };
44
45
			# ------------------------
46
47
			return function () { return null; };
48
		}
49
	}
50
}
51