Test Failed
Push — main ( 624f5f...baf8be )
by Jean-Christophe
04:08
created

VueJSComponent   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 95.08%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 47
c 2
b 0
f 0
dl 0
loc 111
ccs 58
cts 61
cp 0.9508
rs 10
wmc 24

18 Methods

Rating   Name   Duplication   Size   Complexity  
A addProps() 0 2 1
A setModel() 0 2 1
A getName() 0 2 1
A setInheritAttrs() 0 2 1
A onDeactivated() 0 2 1
A addTemplate() 0 2 1
A generateFile() 0 8 4
A onActivated() 0 2 1
A getVarName() 0 2 1
A __construct() 0 11 2
A generateGlobalScript() 0 2 1
A addData() 0 2 1
A generateObject() 0 6 2
A __toString() 0 5 2
A setGlobal() 0 2 1
A extends() 0 2 1
A addDataRaw() 0 2 1
A setTypeAndGlobal() 0 3 1
1
<?php
2
3
namespace PHPMV;
4
5
use PHPMV\js\JavascriptUtils;
6
7
class VueJSComponent extends AbstractVueJS {
8
	protected string $name;
9
	protected array $props;
10
	protected array $template;
11
	protected array $extends;
12
	protected string $varName;
13
	protected string $type;
14 1
	protected bool $global;
15 1
16 1
	public function __construct(string $name, string $varName = null,string $type='component') {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

16
	public function __construct(string $name, string $varName = null,/** @scrutinizer ignore-unused */ string $type='component') {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
17 1
		parent::__construct([]);
18 1
		$this->name = $name;
19 1
		$this->props = [];
20 1
		$this->extends = [];
21 1
		$this->template = [];
22
		if (!$varName) {
23 1
			$varName = JavascriptUtils::kebabToPascal($name);
24 1
		}
25
		$this->varName = $varName;
26 1
		$this->global=false;
27 1
	}
28 1
29
	public function addData(string $name, $value): void {
30 1
		$this->data["data"][$name] = $value;
31 1
	}
32 1
33
	public function addDataRaw(string $name, string $value): void {
34 1
		$this->data["data"][$name] = JavascriptUtils::removeQuotes($value);
35 1
	}
36 1
37
	public function extends(VueJSComponent $component): void {
38 1
		$this->extends['extends'] = $component->getVarName();
39 1
	}
40 1
41 1
	public function generateObject(): string {
42
		$data = [];
43 1
		if (isset($this->data["data"])) {
44 1
			$data["data"] = JavascriptUtils::generateFunction("return " . JavascriptUtils::arrayToJsObject($this->data["data"]));
45
		}
46
		return JavascriptUtils::arrayToJsObject($this->components + $this->filters + $this->extends + $this->mixins + $this->configuration + $this->props + $data + $this->computeds + $this->watchers + $this->hooks + $this->methods + $this->template);
47 1
	}
48 1
49 1
	public function generateGlobalScript(): string {
50 1
		return "Vue.{$this->type}({$this->name},{$this->generateObject()});";
51 1
52
	}
53
54 1
	public function generateFile(bool $inVariable = false, bool $global = false): void {
55 1
		$script = ($inVariable) ? JavascriptUtils::declareVariable("const", $this->varName, $this->generateGlobalScript(), false) : $this->generateGlobalScript();
56 1
		if (!$global) {
57 1
			\file_put_contents("$this->name.js", $script);
58
		} elseif (file_exists("components.js")) {
59
			\file_put_contents("components.js", PHP_EOL . $script, FILE_APPEND);
60
		} else {
61
			\file_put_contents("components.js", $script);
62
		}
63 1
	}
64
65 1
	public function addProps(string ...$props): void {
66 1
		$this->props["props"] = $props;
67 1
	}
68
69 1
	public function setInheritAttrs(bool $inheritAttrs): void {
70 1
		$this->configuration['inheritAttrs'] = $inheritAttrs;
71 1
	}
72
73 1
	public function setModel(string $prop, string $event): void {
74 1
		$this->configuration['model'] = JavascriptUtils::removeQuotes("{ prop: '$prop', event: '$event ' }");
75 1
	}
76
77 1
	public function addTemplate(string $template): void {
78 1
		$this->template["template"] = $template;
79 1
	}
80
81 1
	public function onActivated(string $body): void {
82 1
		$this->addHook("activated", $body);
83 1
	}
84
85 1
	public function onDeactivated(string $body): void {
86 1
		$this->addHook("deactivated", $body);
87 1
	}
88
89 1
	public function getName(): string {
90 1
		return $this->name;
91
	}
92
93 1
	public function getVarName(): string {
94 1
		return $this->varName;
95
	}
96 1
97
	/**
98
	 * @param string $type
99
	 * @param bool $global
100
	 */
101
	public function setTypeAndGlobal(string $type='component',bool $global=true): void {
102
		$this->type = $type;
103
		$this->global=$global;
104
	}
105
106
	/**
107
	 * @param bool $global
108
	 */
109
	public function setGlobal(bool $global): void {
110
		$this->global = $global;
111
	}
112
113
	public function __toString(): string {
114
		if($this->global){
115
			return $this->generateGlobalScript();
116
		}
117
		return JavascriptUtils::declareVariable('const', $this->varName, $this->generateObject(), false);
118
	}
119
120
}