Passed
Push — main ( c550ce...f4ea65 )
by Guillaume
02:52
created

VueManager::addGlobal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PHPMV;
4
5
use PHPMV\js\JavascriptUtils;
6
7
class VueManager {
8
9
	private static ?VueManager $instance = null;
10
	protected array $imports;
11
	protected array $vues;
12
	protected bool $useAxios;
13
14 2
	protected function __construct() {
15 2
		$this->imports = [];
16 2
		$this->vues = [];
17 2
		$this->useAxios = false;
18 2
	}
19
20 2
	public static function getInstance(): ?VueManager {
21 2
		if (!isset(self::$instance)) {
22 2
			self::$instance = new VueManager();
23
		}
24 2
		return self::$instance;
25
	}
26
27 2
	public static function deleteInstance(): void {
28 2
		VueManager::$instance = null;
29 2
	}
30
31 1
	protected function addImport($import): void {
32 1
		$this->imports[] = $import;
33 1
	}
34
35 1
	public function importComponentObject(VueJSComponent $component): void { //component, mixin, or extend
36 1
		$this->addImport(JavascriptUtils::declareVariable('const', $component->getVarName(), $component->generateObject(), false));
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

36
		$this->addImport(JavascriptUtils::/** @scrutinizer ignore-call */ declareVariable('const', $component->getVarName(), $component->generateObject(), false));

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...
37 1
	}
38
39 1
	protected function addGlobal(string $type, string $body, string $name = null): void {
40 1
		if ($name) {
41 1
			$this->addImport("Vue." . $type . "('" . $name . "'," . $body . ");");
42
		} else {
43 1
			$this->addImport("Vue." . $type . "(" . $body . ");");
44
		}
45 1
	}
46
47 1
	public function addGlobalDirective(string $name, array $hookFunction) {
48 1
		foreach ($hookFunction as $key => $value) {
49 1
			$hookFunction[$key] = JavascriptUtils::generateFunction($value, ['el', 'binding', 'vnode', 'oldVnode']);
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

49
			/** @scrutinizer ignore-call */ 
50
   $hookFunction[$key] = JavascriptUtils::generateFunction($value, ['el', 'binding', 'vnode', 'oldVnode']);

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...
50
		}
51 1
		$this->addGlobal('directive', JavascriptUtils::arrayToJsObject($hookFunction), $name);
52 1
	}
53
54 1
	public function addGlobalFilter(string $name, string $body, array $params = []): void {
55 1
		$this->addGlobal('filter', JavascriptUtils::generateFunction($body, $params, false), $name);
56 1
	}
57
58 1
	public function addGlobalExtend(VueJSComponent $extend): void {
59 1
		$this->addGlobal('extend', $extend->generateObject());
60 1
	}
61
62 1
	public function addGlobalMixin(VueJSComponent $mixin): void {
63 1
		$this->addGlobal('mixin', $mixin->generateObject());
64 1
	}
65
66 1
	public function addGlobalObservable(string $varName, array $object): void {
67 1
		$this->addImport(JavascriptUtils::declareVariable('const', $varName, "Vue.observable(" . JavascriptUtils::arrayToJsObject($object) . ")", false));
68 1
	}
69
70 1
	public function addGlobalComponent(VueJSComponent $component): void {
71 1
		$this->addImport($component->generateGlobalScript());
72 1
	}
73
74 1
	public function addVue(VueJS $vue): void {
75 1
		$this->vues[] = $vue;
76 1
	}
77
78 1
	public function __toString(): string {
79 1
		$script = "";
80 1
		if ($this->useAxios) $script = "Vue.prototype.\$http = axios;" . PHP_EOL;
81 1
		$script .= implode(PHP_EOL, $this->imports);
82 1
		$script .= PHP_EOL . implode(PHP_EOL, $this->vues);
83 1
		$script = JavascriptUtils::cleanJSONFunctions($script);
0 ignored issues
show
Bug introduced by
The method cleanJSONFunctions() 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

83
		/** @scrutinizer ignore-call */ 
84
  $script = JavascriptUtils::cleanJSONFunctions($script);

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...
84 1
		return JavascriptUtils::wrapScript($script);
85
	}
86
87 1
	public function setAxios(bool $useAxios): void {
88 1
		$this->useAxios = $useAxios;
89
	}
90
}