Passed
Push — main ( 1eb874...c550ce )
by Guillaume
02:31
created

VueJSComponent::addProps()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 $configuration;
10
	protected array $props;
11
	protected array $template;
12
	protected array $extends;
13
	protected string $varName;
14
15 1
	public function __construct(string $name, string $varName = null) {
16 1
		parent::__construct();
17 1
		$this->name = $name;
18 1
		$this->configuration = [];
19 1
		$this->props = [];
20 1
		$this->extends = [];
21 1
		$this->template = [];
22 1
		if (!$varName) {
23 1
			$varName = JavascriptUtils::kebabToPascal($name);
0 ignored issues
show
Bug introduced by
The method kebabToPascal() does not exist on PHPMV\js\JavascriptUtils. ( Ignorable by Annotation )

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

23
			/** @scrutinizer ignore-call */ 
24
   $varName = JavascriptUtils::kebabToPascal($name);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
		}
25 1
		$this->varName = $varName;
26 1
	}
27
28 1
	public function addData(string $name, $value): void {
29 1
		$this->data["data"][$name] = $value;
30 1
	}
31
32
	public function addDataRaw(string $name, string $value): void {
33
		$this->data["data"][$name] = JavascriptUtils::removeQuotes($value);
0 ignored issues
show
Bug introduced by
The method removeQuotes() does not exist on PHPMV\js\JavascriptUtils. ( Ignorable by Annotation )

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

33
		/** @scrutinizer ignore-call */ 
34
  $this->data["data"][$name] = JavascriptUtils::removeQuotes($value);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
	}
35
36 1
	public function extends(VueJSComponent $component): void {
37 1
		$this->extends['extends'] = $component->getVarName();
38 1
	}
39
40 1
	public function generateObject(): string {
41 1
		$data = [];
42 1
		if (isset($this->data["data"])) {
43 1
			$data["data"] = JavascriptUtils::generateFunction("return " . JavascriptUtils::arrayToJsObject($this->data["data"]));
0 ignored issues
show
Bug introduced by
The method generateFunction() does not exist on PHPMV\js\JavascriptUtils. ( Ignorable by Annotation )

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

43
			/** @scrutinizer ignore-call */ 
44
   $data["data"] = JavascriptUtils::generateFunction("return " . JavascriptUtils::arrayToJsObject($this->data["data"]));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
		}
45 1
		$script = 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);
46 1
		return $script;
47
	}
48
49 1
	public function generateGlobalScript(): string {
50 1
		$script = "Vue.component('" . $this->name . "',";
51 1
		$script .= $this->generateObject();
52 1
		$script .= ");";
53 1
		return $script;
54
	}
55
56 1
	public function generateFile(bool $inVariable = false, bool $global = false): void {
57 1
		$script = ($inVariable) ? JavascriptUtils::declareVariable("const", $this->varName, $this->generateGlobalScript(), false) : $this->generateGlobalScript();
0 ignored issues
show
Bug introduced by
The method declareVariable() does not exist on PHPMV\js\JavascriptUtils. ( Ignorable by Annotation )

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

57
		$script = ($inVariable) ? JavascriptUtils::/** @scrutinizer ignore-call */ declareVariable("const", $this->varName, $this->generateGlobalScript(), false) : $this->generateGlobalScript();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58 1
		if (!$global) {
59 1
			\file_put_contents($this->name . ".js", $script);
60
		} elseif (file_exists("components.js")) {
61
			\file_put_contents("components.js", PHP_EOL . $script, FILE_APPEND);
62
		} else {
63
			\file_put_contents("components.js", $script);
64
		}
65 1
	}
66
67 1
	public function addProps(string ...$props): void {
68 1
		$this->props["props"] = $props;
69 1
	}
70
71 1
	public function setInheritAttrs(bool $inheritAttrs): void {
72 1
		$this->configuration['inheritAttrs'] = $inheritAttrs;
73 1
	}
74
75 1
	public function setModel(string $prop, string $event): void {
76 1
		$this->configuration['model'] = JavascriptUtils::removeQuotes("{ prop: '" . $prop . "', event: '" . $event . "' }");
77 1
	}
78
79 1
	public function addTemplate(string $template): void {
80 1
		$this->template["template"] = $template;
81 1
	}
82
83 1
	public function onActivated(string $body): void {
84 1
		$this->addHook("activated", $body);
85 1
	}
86
87 1
	public function onDeactivated(string $body): void {
88 1
		$this->addHook("deactivated", $body);
89 1
	}
90
91 1
	public function getName(): string {
92 1
		return $this->name;
93
	}
94
95 1
	public function getVarName(): string {
96 1
		return $this->varName;
97
	}
98
}