Passed
Push — master ( f762b7...fe4615 )
by Jean-Christophe
11:36
created

UArrayAsTrait::parseValue()   B

Complexity

Conditions 11
Paths 7

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 11.0295

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 18
ccs 15
cts 16
cp 0.9375
rs 7.3166
cc 11
nc 7
nop 4
crap 11.0295

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 Ubiquity\utils\base\traits;
4
5
use Ubiquity\utils\base\UIntrospection;
6
use Ubiquity\utils\base\UString;
7
8
/**
9
 * Ubiquity\utils\base\traits$UArrayAsTrait
10
 * This class is part of Ubiquity
11
 * @author jc
12
 * @version 1.0.5
13
 *
14
 */
15
trait UArrayAsTrait {
16 59
	private static function parseValue($v, $prefix = '', $depth = 1, $format = false) {
17 59
		if (\is_numeric ( $v )) {
18 54
			$result = $v;
19 58
		} elseif ($v !== '' && UString::isBooleanStr ( $v )) {
20 47
			$result = UString::getBooleanStr ( $v );
21 58
		} elseif (\is_array ( $v )) {
22 55
			$result = self::asPhpArray ( $v, $prefix, $depth + 1, $format );
23 58
		} elseif (\is_string ( $v ) && (UString::startswith ( \trim ( $v ), '$config' ) || UString::startswith ( \trim ( $v ), 'function' ) || UString::startswith ( \trim ( $v ), 'array(' ))) {
24 9
			$result = $v;
25 58
		} elseif ($v instanceof \Closure) {
26 11
			$result = UIntrospection::closure_dump ( $v );
27 58
		} elseif ($v instanceof \DateTime) {
28
			$result = "\DateTime::createFromFormat('Y-m-d H:i:s','" . $v->format ( 'Y-m-d H:i:s' ) . "')";
29
		} else {
30 58
			$result = UString::doubleBackSlashes ( $v );
31 58
			$result = "\"" . \str_replace ( [ '$','"' ], [ '\$','\"' ], $result ) . "\"";
32
		}
33 59
		return $result;
34
	}
35 60
	public static function asPhpArray($array, $prefix = '', $depth = 1, $format = false) {
36 60
		$exts = array ();
37 60
		$extsStr = '';
38 60
		$tab = '';
39 60
		$nl = '';
40 60
		if ($format) {
41 33
			$tab = \str_repeat ( "\t", $depth );
42 33
			$nl = PHP_EOL;
43
		}
44 60
		foreach ( $array as $k => $v ) {
45 59
			if (\is_string ( $k )) {
46 59
				$exts [] = "\"" . UString::doubleBackSlashes ( $k ) . "\"=>" . self::parseValue ( $v, 'array', $depth + 1, $format );
47
			} else {
48 52
				$exts [] = self::parseValue ( $v, $prefix, $depth + 1, $format );
49
			}
50
		}
51 60
		if ($prefix !== '') {
52 60
			$extsStr = '()';
53
		}
54 60
		if (\sizeof ( $exts ) > 0) {
55 59
			$extsStr = "({$nl}{$tab}" . \implode ( ",{$nl}{$tab}", $exts ) . "{$nl}{$tab})";
56
		}
57 60
		return $prefix . $extsStr;
58
	}
59
60 28
	public static function asPhpClass($array, $name, $namespace = '', $format = false) {
61 28
		$tab = '';
62 28
		$nl = '';
63 28
		if ($format) {
64 28
			$tab = "\t";
65 28
			$nl = PHP_EOL;
66
		}
67 28
		$content = 'public static $value=' . self::asPhpArray ( $array, 'array', 1, true ) . ';';
68 28
		if ($namespace != null) {
69 28
			$namespace = "namespace {$namespace};{$nl}";
70
		}
71 28
		return "{$namespace}class {$name} {" . $nl . $tab . $content . $nl . $tab . "}";
72
	}
73
74 1
	public static function asJSON($array) {
75 1
		return \json_encode ( $array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE );
76
	}
77
}
78
79