Test Failed
Push — main ( a8d1f9...698061 )
by Jean-Christophe
02:01
created

JSX::nodeToJs()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 13.776

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 16
c 5
b 1
f 0
dl 0
loc 23
ccs 6
cts 15
cp 0.4
rs 9.1111
cc 6
nc 4
nop 2
crap 13.776
1
<?php
2
namespace PHPMV\utils;
3
4
use PHPMV\js\JavascriptUtils;
5
use PHPMV\react\ReactJS;
6
7
/**
8
 * PHPMV\utils$JSX
9
 * This class is part of Ubiquity
10
 *
11
 * @author jc
12
 * @version 1.0.0
13
 *
14
 */
15
class JSX {
16
17
	public static $reactCreateElement = 'React.createElement';
18
19
	private static $jsDetect = [
20
		'onBlur' => 0,
21
		'onChange' => 0,
22
		'onDblclick' => 0,
23
		'onClick' => 0,
24
		'value' => 0
25
	];
26
27 2
	private static function getName(string $name, ReactJS $react): string {
28 2
		return $react->components[$name] ?? '"' . $name . '"';
29
	}
30
31
	private static $attributes = [
32
		'classname' => 'className',
33
		'onblur' => 'onBlur',
34
		'onclick' => 'onClick',
35
		'onchange' => 'onChange'
36
	];
37
38 2
	private static function cleanJSONFunctions(string $json): string {
39 2
		return \str_replace([
40 2
			'"!!%',
41
			'%!!"'
42 2
		], '', $json);
43
	}
44
45
	private static function hasBraces(string $str): bool {
46
		return (\substr($str, 0, 1) === '{' && \substr($str, - 1) === '}');
47
	}
48
49 2
	private static function nodeToJs(\DOMNode $root, ?ReactJS $react): string {
50 2
		$attributes = [];
51 2
		$children = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $children is dead and can be removed.
Loading history...
52 2
		$name = $root->nodeName;
53
54 2
		if ($root->hasAttributes()) {
55
			$attrs = $root->attributes;
56
57
			foreach ($attrs as $i => $attr) {
58
				$attrName = self::$attributes[$attr->name] ?? $attr->name;
59
				$attrValue = $attr->value;
60
				if (isset(self::$jsDetect[$attrName])) {
61
					if (self::hasBraces($attrValue)) {
62
						$attrValue = \substr($attrValue, 1, - 1);
63
					}
64
					$attributes[$attrName] = '!!%' . $attrValue . '%!!';
65
				} else {
66
					$attributes[$attrName] = $attrValue;
67
				}
68
			}
69
		}
70
		$childrenStr = self::getChildrenStr($root, $react);
71 2
		return self::$reactCreateElement . "(" . ((isset($react)) ? self::getName($name, $react) : $name) . "," . self::cleanJSONFunctions(JavascriptUtils::toJSON($attributes)) . "$childrenStr)";
72
	}
73 2
74
	private static function getChildrenStr(\DOMNode $root, ?ReactJS $react): string {
75
		$childNodes = $root->childNodes;
76
77
		for ($i = 0; $i < $childNodes->length; $i ++) {
78
			$child = $childNodes->item($i);
79
			if ($child->nodeType == XML_TEXT_NODE) {
80
				$v = \trim($child->nodeValue);
81
				if ($v != null) {
82
					\preg_match_all('@\{(.*?)\}@', $v, $matches);
83
					if (\count($matches[1]) > 0) {
84
						foreach ($matches[1] as $ev) {
85
							$children[] = $ev;
86
						}
87
					} else {
88
						$children[] = "`$v`";
89
					}
90
				}
91 2
			} else {
92 2
				$children[] = self::nodeToJs($child, $react);
93
			}
94
		}
95 2
		return (count($children) > 0) ? (',' . implode(',', $children)) : '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $children does not seem to be defined for all execution paths leading up to this point.
Loading history...
96
	}
97
98 2
	public static function toJs(string $html, ?ReactJS $react = null): string {
99 2
		\libxml_use_internal_errors(true);
100 2
		$dom = new \DOMDocument('1.0', 'UTF-8');
101 2
		$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
102 2
		return self::nodeToJs($dom->documentElement, $react);
103
	}
104
}
105