Test Failed
Push — main ( 2642ec...a2c00e )
by Guillaume
02:33
created

VueJSComponent::generateFile()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 6
nop 2
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
crap 4.0466
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PHPMV;
4
5
use PHPMV\js\JavascriptUtils;
6
use PHPMV\utils\JsUtils;
7
8
class VueJSComponent extends AbstractVueJS {
9
    protected string $name;
10
    protected array $props;
11
    protected array $template;
12 2
    protected array $extends;
13 2
    protected ?string $varName;
14 2
15 2
    public function __construct(string $name,string $varName = null) {
16 2
        parent::__construct();
17 2
        $this->name = $name;
18
        $this->props = [];
19 1
        $this->extends = [];
20 1
        $this->template = [];
21 1
        if(!$varName){
22 1
            $varName = JsUtils::kebabToPascal($name);
23
        }
24
        $this->varName = $varName;
25 1
    }
26 1
27 1
    public function extends(VueJSComponent $component):void {
28 1
        $this->extends['extends'] = $component->getVarName();
29 1
    }
30
31
    public function generateObject():string {
32
        $script = JavascriptUtils::arrayToJsObject($this->props + $this->components + $this->filters + $this->extends + $this->mixins + $this->data + $this->computeds + $this->watchers + $this->hooks + $this->methods + $this->template);
33
        $script = JsUtils::cleanJSONFunctions($script);
34
        return $script;
35
    }
36
37
    public function generateGlobalScript():string {
38
        $script = "Vue.component('".$this->name."',";
39
        $script .= $this->generateObject();
40
        $script .= ")";
41
        return $script;
42
    }
43
44
    public function generateFile(bool $inVariable = false, bool $global = false):void {
45
        $script = ($inVariable) ? JsUtils::declareVariable("const", $this->varName, $this->generateGlobalScript(), false) : $this->generateGlobalScript().";";
0 ignored issues
show
Bug introduced by
It seems like $this->varName can also be of type null; however, parameter $name of PHPMV\utils\JsUtils::declareVariable() does only seem to accept string, 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

45
        $script = ($inVariable) ? JsUtils::declareVariable("const", /** @scrutinizer ignore-type */ $this->varName, $this->generateGlobalScript(), false) : $this->generateGlobalScript().";";
Loading history...
46
        if (!$global){
47 1
            \file_put_contents($this->name.".js",$script);
48 1
        }
49 1
        elseif(file_exists("components.js")){
50
            \file_put_contents("components.js",PHP_EOL . $script,FILE_APPEND);
51 1
        }
52 1
        else{
53 1
            \file_put_contents("components.js",$script);
54
        }
55 1
    }
56 1
57
    public function addProps(string ...$props):void {
58 1
        $this->props["props"] = $props;
59
    }
60
61
    public function addTemplate(string $template):void {
62
        $this->template["template"] = $template;
63
    }
64
65
    public function importTemplate(string $template):void {
66
        $this->template["template"] = "'".\str_replace(["\n","\r","\t"]," ",(\file_get_contents($template.'.html',true))."'");
67
    }
68
69
    public function onActivated(string $body):void {
70
        $this->addHook("activated", $body);
71
    }
72
73
    public function onDeactivated(string $body):void {
74
        $this->addHook("deactivated", $body);
75
    }
76
77
    public function getName():string {
78
        return $this->name;
79
    }
80
81
    public function getVarName():string {
82
        return $this->varName;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->varName could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
83
    }
84
}