Passed
Pull Request — master (#103)
by Mark
01:28
created

Field.resetDirty   C

Complexity

Conditions 10

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 4
dl 0
loc 4
rs 5.9999
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like Field.resetDirty 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.

1
import {ModelInterface, TableInterface} from "../../JeloquentInterfaces";
2
3
export default class Field {
4
5
    protected $fieldValue: unknown;
6
7
    protected $name: string;
8
9
    protected $originalValue: unknown;
10
11
    protected $parent: ModelInterface;
12
13
    protected $previousValue: unknown;
14
15
    private _isPrimary: boolean;
16
17
    /**
18
     *
19
     * @param name
20
     * @param isPrimary
21
     */
22
    constructor(name: string, isPrimary = false) {
23
        this._isPrimary = isPrimary;
24
        this.$name = name;
25
        this.$fieldValue = null;
26
        this.$previousValue = undefined;
27
        this.$originalValue = undefined;
28
        this.$parent = null;
29
    }
30
31
    get isDirty(): boolean {
32
        return this.$fieldValue != this.$previousValue;
33
    }
34
35
    get isPrimary(): boolean {
36
        return this._isPrimary;
37
    }
38
39
    get name(): string {
40
        return this.$name;
41
    }
42
43
    get originalValue(): unknown {
44
        return this.$originalValue;
45
    }
46
47
    get previousValue(): unknown {
48
        return this.$previousValue;
49
    }
50
51
    get value(): unknown {
52
        return this.$fieldValue;
53
    }
54
55
    set _value(value) {
56
        if (this.$originalValue === undefined) {
57
            this.$originalValue = JSON.parse(JSON.stringify(this.value ?? value));
58
        }
59
60
        this.$previousValue = JSON.parse(JSON.stringify(this.value));
61
        this.$fieldValue = value;
62
    }
63
64
    set value(value: unknown) {
65
        if (this.$previousValue === undefined) {
66
            this.$previousValue = JSON.parse(JSON.stringify(this.value ?? value));
67
        }
68
69
        if (this.$originalValue === undefined) {
70
            this.$originalValue = JSON.parse(JSON.stringify(this.value ?? value));
71
        }
72
73
        this.$previousValue = JSON.parse(JSON.stringify(this.value));
74
75
        this.$fieldValue = value;
76
    }
77
78
    resetDirty(): void {
79
        this.$originalValue = JSON.parse(JSON.stringify(this.$fieldValue));
80
        this.$previousValue = JSON.parse(JSON.stringify(this.$fieldValue));
81
    }
82
83
    setName(): Field {
84
        return this;
85
    }
86
87
    setup(parent: ModelInterface): Field {
88
        this.$parent = parent;
89
        return this.setName().setParentProperties();
90
    }
91
92
    tableSetup(table: TableInterface): void {
93
        console.info(table.name);
94
    }
95
96
    toJson(): object {
97
        const object = {};
98
        object[this.$name] = this.value;
99
        return JSON.parse(JSON.stringify(object));
100
    }
101
102
    protected setParentProperties(): Field {
103
        return this;
104
    }
105
}