Completed
Push — master ( 8a7d45...7daebe )
by Agel_Nash
02:55
created

object.functions.php ➔ is_serialized()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 26
nc 12
nop 1
dl 0
loc 34
rs 5.1234
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
if (!function_exists('value')) {
3
	/**
4
	 * Если аргумент это замыкание, то вычисляется его значение и отдается в виде результата
5
	 * В противном случае возвращается сам аргумент
6
	 *
7
	 * @param  mixed $value
8
	 * @return mixed
9
	 */
10
	function value($value)
11
	{
12
		return $value instanceof Closure ? $value() : $value;
13
	}
14
}
15
16
if (!function_exists('with')) {
17
	/**
18
	 * Синтаксический сахар реализующий возможности PHP 5.4 "получение доступа к члену класса при создании экземпляра"
19
	 *     PHP >= 5.4: (new test())->run();
20
	 *     PHP <= 5.3: with(new test())->run();
21
	 *
22
	 * @param  mixed $object объект
23
	 * @return mixed
24
	 */
25
	function with($object)
26
	{
27
		return $object;
28
	}
29
}
30
31
if (!function_exists('is_serialized')) {
32
	/**
33
	 * Проверка переменной на предмет наличия в ней сериализованных данных
34
	 *
35
	 * @param  mixed $data данные которые необходимо проверить
36
	 * @return bool
37
	 */
38
	function is_serialized($data)
39
	{
40
		if (!is_string($data)) {
41
			return false;
42
		}
43
		$data = trim($data);
44
		if ('N;' == $data) {
45
			return true;
46
		}
47
		$length = strlen($data);
48
		if ($length < 4) {
49
			return false;
50
		}
51
		if (':' !== $data[1]) {
52
			return false;
53
		}
54
		$lastc = $data[$length - 1];
55
		if (';' !== $lastc && '}' !== $lastc) {
56
			return false;
57
		}
58
		$token = $data[0];
59
		switch ($token) {
60
			case 's':
61
				return ('"' === $data[$length - 2]);
62
			case 'a':
63
			case 'O':
64
				return (bool)preg_match("/^{$token}:[0-9]+:/s", $data);
65
			case 'b':
66
			case 'i':
67
			case 'd':
68
				return (bool)preg_match("/^{$token}:[0-9.E-]+;\$/", $data);
69
		}
70
		return false;
71
	}
72
}
73
74
if (!function_exists('var_switch')) {
75
	/**
76
	 * Поменять местами значения двух переменных
77
	 *
78
	 * @param $var1
79
	 * @param $var2
80
	 * @return void
81
	 */
82
	function var_switch(&$var1, &$var2)
83
	{
84
		list($var2, $var1) = array($var1, $var2);
85
	}
86
}
87
88
if (!function_exists('arity')) {
89
	/**
90
	 * Сколько параметров принимает данное замыкание
91
	 *
92
	 * @param Closure $callback
93
	 * @return int
94
	 */
95
	function arity(Closure $callback)
96
	{
97
		$r = new ReflectionObject($callback);
98
		$m = $r->getMethod('__invoke');
99
		return $m->getNumberOfParameters();
100
	}
101
}
102