Test Failed
Push — main ( 5f3e65...06393c )
by Jean-Christophe
01:44
created

JSX::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 4
Bugs 1 Features 1
Metric Value
eloc 1
c 4
b 1
f 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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 = [];
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
71 2
		$childNodes = $root->childNodes;
72
73 2
		for ($i = 0; $i < $childNodes->length; $i ++) {
74
			$child = $childNodes->item($i);
75
			if ($child->nodeType == XML_TEXT_NODE) {
76
				$v = \trim($child->nodeValue);
77
				if ($v != null) {
78
					\preg_match_all('@\{(.*?)\}@', $v, $matches);
79
					if (\count($matches[1]) > 0) {
80
						foreach ($matches[1] as $ev) {
81
							$children[] = $ev;
82
						}
83
					} else {
84
						$children[] = "`$v`";
85
					}
86
				}
87
			} else {
88
				$children[] = self::nodeToJs($child, $react);
89
			}
90
		}
91 2
		$childrenStr = '';
92 2
		if (count($children) > 0) {
93
			$childrenStr = ',' . implode(',', $children);
94
		}
95 2
		return self::$reactCreateElement . "(" . (isset($react)) ? self::getName($name, $react) : $name . "," . self::cleanJSONFunctions(JavascriptUtils::toJSON($attributes)) . "$childrenStr)";
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);
0 ignored issues
show
Bug introduced by
It seems like $react can also be of type null; however, parameter $react of PHPMV\utils\JSX::nodeToJs() does only seem to accept PHPMV\react\ReactJS, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
		return self::nodeToJs($dom->documentElement, /** @scrutinizer ignore-type */ $react);
Loading history...
103
	}
104
}
105