Test Failed
Push — main ( 114191...29603f )
by Guillaume
02:49
created

AbstractVueJS   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 61
dl 0
loc 173
ccs 105
cts 112
cp 0.9375
rs 9.2
c 2
b 0
f 0
wmc 40

37 Methods

Rating   Name   Duplication   Size   Complexity  
A onUpdatedNextTick() 0 2 1
A addData() 0 2 1
A onCreated() 0 2 1
A onBeforeMount() 0 2 1
A onBeforeDestroy() 0 2 1
A onBeforeCreate() 0 2 1
A addMethod() 0 2 1
A __construct() 0 8 1
A addDataRaw() 0 2 1
A onBeforeUpdate() 0 2 1
A addHook() 0 2 1
A onDestroyed() 0 2 1
A onMounted() 0 2 1
A onUpdated() 0 2 1
A addWatcher() 0 2 1
A addComputed() 0 4 2
A addFilter() 0 3 1
A getWatchers() 0 2 1
A getMethods() 0 2 1
A getComputeds() 0 2 1
A getData() 0 2 1
A addGlobalObservable() 0 2 1
A addGlobalDirective() 0 5 2
A setData() 0 2 1
A setMethods() 0 2 1
A addGlobalFilter() 0 2 1
A setDirectives() 0 2 1
A setComputeds() 0 2 1
A addDirective() 0 5 2
A getDirectives() 0 2 1
A setWatchers() 0 2 1
A setFilters() 0 2 1
A generateFunction() 0 2 1
A getFilters() 0 2 1
A removeQuotes() 0 2 1
A getHooks() 0 2 1
A setHooks() 0 2 1

How to fix   Complexity   

Complex Class

Complex classes like AbstractVueJS often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AbstractVueJS, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace PHPMV;
3
4
use PHPMV\js\JavascriptUtils;
5
6
/**
7
 * Created by PhpStorm.
8
 * User: qgorak
9
 * Date: 19/11/2020
10
 * Time: 14:20
11
 */
12
class AbstractVueJS {
13
    static private $removeQuote = ["start"=>"!!%","end"=>"%!!"];
14
    static public $global;
15
	protected $data;
16
	protected $methods;
17
	protected $computeds;
18
	protected $watchers;
19
	protected $directives;
20
	protected $filters;
21
	protected $hooks;
22
	
23 15
	public function __construct() {
24 15
	    $this->data=[];
25 15
	    $this->methods=[];
26 15
	    $this->computeds=[];
27 15
	    $this->watchers=[];
28 15
	    $this->directives=[];
29 15
	    $this->filters=[];
30 15
	    $this->hooks=[];
31 15
	}
32
	
33 2
	public function addHook(string $name,string $body):void {
34 2
	    $this->hooks[$name] = self::generateFunction($body);
35 2
	}
36
	
37 1
	public function onBeforeCreate(string $body):void {
38 1
		$this->addHook("beforeCreate", $body);
39 1
	}
40
	
41 1
	public function onCreated(string $body):void {
42 1
		$this->addHook("created", $body);
43 1
	}
44
	
45 1
	public function onBeforeMount(string $body):void {
46 1
		$this->addHook("beforeMount", $body);
47 1
	}
48
	
49 2
	public function onMounted(string $body):void {
50 2
		$this->addHook("mounted", $body);
51 2
	}
52
	
53 1
	public function onBeforeUpdate(string $body):void {
54 1
		$this->addHook("beforeUpdate", $body);
55 1
	}
56
	
57 1
	public function onUpdated(string $body):void {
58 1
		$this->addHook("updated", $body);
59 1
	}
60
	
61 1
	public function onUpdatedNextTick(string $body):void {
62 1
		$this->addHook("updated", "this.\$nextTick(function () {".$body."})");
63 1
	}
64
	
65 1
	public function onBeforeDestroy(string $body):void {
66 1
		$this->addHook("beforeDestroy", $body);
67 1
	}
68
	
69 1
	public function onDestroyed(string $body):void {
70 1
		$this->addHook("destroyed", $body);
71 1
	}
72
73 3
	public function addData(string $name,$value):void {
74 3
        $this->data["data"][$name]=$value;
75 3
	}
76
77 2
	public function addDataRaw(string $name,string $value):void {
78 2
        $this->data["data"][$name]=self::removeQuotes($value);
79 2
	}
80
	
81 4
	public function addMethod(string $name,string $body, array $params = []):void {
82 4
        $this->methods["methods"][$name]=self::generateFunction($body,$params);
83 4
	}
84
	
85 3
	public function addComputed(string $name,string $get,string $set=null):void {
86 3
        $name=self::removeQuotes($name);
87 3
	    $vc=(is_null($set)) ? self::generateFunction($get) : self::removeQuotes("{ get: ".self::generateFunction($get).", set: ".self::generateFunction($set,["v"])." }");
88 3
	    $this->computeds["computeds"][$name]=$vc;
89 3
	}
90
	
91 2
	public function addWatcher(string $var,string $body,array $params=[]):void {
92 2
	    $this->watchers["watch"][$var]=self::generateFunction($body,$params);
93 2
	}
94
95
	public function addFilter(string $name,string $body, array $params = []):void {
96
	    $name=self::removeQuotes($name);
97
        $this->filters["filters"][$name]=self::generateFunction($body,$params);
98
    }
99
100 1
    public function addDirective(string $name,array $hookFunction):void {
101 1
	    $name = self::removeQuotes($name);
102 1
	    foreach ($hookFunction as $key=>$value){
103 1
            $key = self::removeQuotes($key);
104 1
            $this->directives["directives"][$name][$key] = self::generateFunction($value,['el', 'binding', 'vnode', 'oldVnode']);
105
        }
106 1
    }
107
108 1
    public static function addGlobalDirective(string $name,array $hookFunction):void {
109 1
        foreach ($hookFunction as $key=>$value){
110 1
            $hookFunction[$key] = self::generateFunction($value,['el', 'binding', 'vnode', 'oldVnode']);
111
        }
112 1
	    self::$global[]=self::removeQuotes("Vue.directive('".$name."',".self::removeQuotes(JavascriptUtils::arrayToJsObject($hookFunction)).");");
113 1
    }
114
115 1
    public static function addGlobalFilter(string $name,string $body, array $params = []):void {
116 1
        self::$global[]=self::removeQuotes("Vue.filter('".$name."',".self::generateFunction($body,$params).");");
117 1
    }
118
119 1
    public static function addGlobalObservable(string $varName, array $object){
120 1
        self::$global[]=self::removeQuotes("const ".$varName." = Vue.observable(". JavascriptUtils::arrayToJsObject($object) .");");
121 1
    }
122
123 4
	public function getData():array {
124 4
	    return $this->data;
125
	}
126
	
127 1
	public function setData(array $data):void {
128 1
	    $this->data=$data;
129 1
	}
130
	
131 3
	public function getMethods():array {
132 3
	    return $this->methods;
133
	}
134
	
135 1
	public function setMethods(array $methods):void {
136 1
	    $this->methods=$methods;
137 1
	}
138
	
139 3
	public function getComputeds():array {
140 3
		return $this->computeds;
141
	}
142
143 1
	public function setComputeds(array $computeds):void {
144 1
		$this->computeds = $computeds;
145 1
	}
146
147 2
	public function getWatchers():array {
148 2
	    return $this->watchers;
149
	}
150
	
151 1
	public function setWatchers(array $watchers):void {
152 1
		$this->watchers = $watchers;
153 1
	}
154
155 1
    public function getDirectives():array {
156 1
        return $this->directives;
157
    }
158
159
    public function setDirectives(array $directives):void {
160
        $this->directives = $directives;
161
    }
162
163 2
    public function getFilters():array {
164 2
        return $this->filters;
165
    }
166
167 1
    public function setFilters(array $filters):void {
168 1
        $this->filters = $filters;
169 1
    }
170
	
171 11
	public function getHooks():array {
172 11
		return $this->hooks;
173
	}
174
	
175 9
	public function setHooks(array $hooks):void {
176 9
		$this->hooks = $hooks;
177
	}
178 1
179
	public static function removeQuotes(string $body):string{
180
        return self::$removeQuote["start"].$body.self::$removeQuote["end"];
181
    }
182
183
	public static function generateFunction(string $body, array $params = []):string {
184
        return self::removeQuotes("function(".implode(",",$params)."){".$body."}");
185
    }
186
}