Passed
Push — main ( 83e3d1...e51278 )
by Jean-Christophe
02:23
created

JSX::hasBraces()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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 1
	private static function hasBraces(string $str): bool {
46 1
		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
		$name = $root->nodeName;
52
53 2
		if ($root->hasAttributes()) {
54 1
			$attrs = $root->attributes;
55
56 1
			foreach ($attrs as $i => $attr) {
57 1
				$attrName = self::$attributes[$attr->name] ?? $attr->name;
58 1
				$attrValue = $attr->value;
59 1
				if (isset(self::$jsDetect[$attrName])) {
60 1
					if (self::hasBraces($attrValue)) {
61 1
						$attrValue = \substr($attrValue, 1, - 1);
62
					}
63 1
					$attributes[$attrName] = '!!%' . $attrValue . '%!!';
64
				} else {
65 1
					$attributes[$attrName] = $attrValue;
66
				}
67
			}
68
		}
69 2
		$childrenStr = self::getChildrenStr($root, $react);
70 2
		return self::$reactCreateElement . "(" . ((isset($react)) ? self::getName($name, $react) : $name) . "," . self::cleanJSONFunctions(JavascriptUtils::toJSON($attributes)) . "$childrenStr)";
71
	}
72
73 2
	private static function getChildrenStr(\DOMNode $root, ?ReactJS $react): string {
74 2
		$children = [];
75
76 2
		$childNodes = $root->childNodes;
77
78 2
		for ($i = 0; $i < $childNodes->length; $i ++) {
79 1
			$child = $childNodes->item($i);
80 1
			if ($child->nodeType == XML_TEXT_NODE) {
81 1
				$v = \trim($child->nodeValue);
82 1
				if ($v != null) {
83 1
					$parts = \preg_split('@(\{.*?\})@', $v, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
84 1
					if (\count($parts) > 0) {
0 ignored issues
show
Bug introduced by
It seems like $parts can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, 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

84
					if (\count(/** @scrutinizer ignore-type */ $parts) > 0) {
Loading history...
85 1
						foreach ($parts as $ev) {
86 1
							if (self::hasBraces($ev)) {
87 1
								$children[] = \substr($ev, 1, - 1);
88 1
							} elseif (\trim($ev) != null) {
89
								$children[] = '"' . $v . '"';
90
							}
91
						}
92
					} else {
93 1
						$children[] = "`$v`";
94
					}
95
				}
96
			} else {
97 1
				$children[] = self::nodeToJs($child, $react);
98
			}
99
		}
100 2
		return (count($children) > 0) ? (',' . implode(',', $children)) : '';
101
	}
102
103 2
	public static function toJs(string $html, ?ReactJS $react = null): string {
104 2
		\libxml_use_internal_errors(true);
105 2
		$dom = new \DOMDocument('1.0', 'UTF-8');
106 2
		$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
107 2
		return self::nodeToJs($dom->documentElement, $react);
108
	}
109
}
110