Passed
Push — main ( faf76d...59f012 )
by Jean-Christophe
02:21
created

ReactClass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 27
c 2
b 0
f 0
dl 0
loc 86
ccs 28
cts 28
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A __construct() 0 3 1
A parse() 0 14 2
A addMethod() 0 4 1
1
<?php
2
namespace PHPMV\core;
3
4
/**
5
 * A React class generator.
6
 * PHPMV\core$ReactClass
7
 * This class is part of php-react
8
 *
9
 * @author jc
10
 * @version 1.0.0
11
 *
12
 */
13
class ReactClass {
14
15
	/**
16
	 *
17
	 * @var TemplateParser
18
	 */
19
	private static $template;
20
21
	/**
22
	 *
23
	 * @var TemplateParser
24
	 */
25
	private static $methodTemplate;
26
27
	/**
28
	 * The class name.
29
	 *
30
	 * @var string
31
	 */
32
	private $name;
33
34
	/**
35
	 * The base class.
36
	 *
37
	 * @var string
38
	 */
39
	private $base;
40
41
	/**
42
	 *
43
	 * @var array
44
	 */
45
	private $methods;
46
47 3
	public function __construct(string $name, string $baseClass) {
48 3
		$this->name = $name;
49 3
		$this->base = $baseClass;
50 3
	}
51
52
	/**
53
	 * Initialize templates.
54
	 */
55 3
	public static function init(): void {
56 3
		$templateFolder = ReactLibrary::getTemplateFolder();
57 3
		self::$template = new TemplateParser();
58 3
		self::$template->loadTemplatefile($templateFolder . '/class');
59 3
		self::$methodTemplate = new TemplateParser();
60 3
		self::$methodTemplate->loadTemplatefile($templateFolder . '/method');
61 3
	}
62
63
	/**
64
	 * Add a new method to the class.
65
	 *
66
	 * @param string $name
67
	 *        	The method name
68
	 * @param string $body
69
	 *        	The method javascript code body
70
	 * @param mixed ...$params
71
	 *        	The method parameters
72
	 */
73 3
	public function addMethod(string $name, string $body, ...$params): void {
74 3
		$this->methods[$name] = [
75 3
			'body' => $body,
76 3
			'params' => $params
77
		];
78 3
	}
79
80
	/**
81
	 * Generate the component javascript code.
82
	 *
83
	 * @return string
84
	 */
85 3
	public function parse(): string {
86 3
		$body = [];
87 3
		foreach ($this->methods as $name => $arrayMethod) {
88 3
			$body[] = self::$methodTemplate->parse([
89 3
				'name' => $name,
90 3
				'body' => $arrayMethod['body'],
91 3
				'params' => implode(',', $arrayMethod['params'])
92
			]);
93
		}
94 3
		$bodyStr = implode("\n", $body);
95 3
		return self::$template->parse([
96 3
			'name' => $this->name,
97 3
			'base' => $this->base,
98 3
			'body' => $bodyStr
99
		]);
100
	}
101
}
102
103