Completed
Push — master ( 450435...3f0d1d )
by Günter
9s
created

ContactTrait   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 3
dl 0
loc 168
rs 8.2608
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
set() 0 1 ?
A forceAscii() 0 4 1
A skipInt() 0 4 1
A skipLoc() 0 4 1
A appendId() 0 4 1
A appendName() 0 10 4
A appendOrganization() 0 10 4
A appendStreet() 0 10 4
A appendCity() 0 10 4
A appendProvince() 0 10 4
A appendPostalCode() 0 10 4
B appendCountryCode() 0 14 5
A appendVoice() 0 4 1
A appendFax() 0 4 1
A appendEmail() 0 8 2
A appendAuthInfo() 0 9 2
A appendDisclose() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like ContactTrait 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 ContactTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * This file is part of the php-epp2 library.
5
 *
6
 * (c) Gunter Grodotzki <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace AfriCC\EPP;
13
14
use Exception;
15
16
trait ContactTrait
17
{
18
    /**
19
     * This was once needed as the conversion done by COZA was faulty
20
     * the bug has since been fixed but this remains to allow testing
21
     * set true to force ascii usage on type=loc (which should allow UTF8).
22
     *
23
     * @var bool
24
     */
25
    protected $force_ascii = false;
26
27
    /**
28
     * Set true to skip the generation of type=int.
29
     *
30
     * @var bool
31
     */
32
    protected $skip_int = false;
33
34
    /**
35
     * Set true to skip the generation of type=loc.
36
     *
37
     * @var bool
38
     */
39
    protected $skip_loc = false;
40
41
    abstract public function set($path = null, $value = null);
42
43
    public function forceAscii()
44
    {
45
        $this->force_ascii = true;
46
    }
47
48
    /**
49
     * Skip the generation of type=int.
50
     */
51
    public function skipInt()
52
    {
53
        $this->skip_int = true;
54
    }
55
56
    /**
57
     * Skip the generation of type=loc.
58
     */
59
    public function skipLoc()
60
    {
61
        $this->skip_loc = true;
62
    }
63
64
    public function appendId($path, $id)
65
    {
66
        $this->set($path, $id);
67
    }
68
69
    public function appendName($path, $name)
70
    {
71
        if (!$this->skip_loc) {
72
            $this->set(sprintf($path, 'loc'), $this->force_ascii ? Translit::transliterate($name) : $name);
73
        }
74
75
        if (!$this->skip_int) {
76
            $this->set(sprintf($path, 'int'), Translit::transliterate($name));
77
        }
78
    }
79
80
    public function appendOrganization($path, $org)
81
    {
82
        if (!$this->skip_loc) {
83
            $this->set(sprintf($path, 'loc'), $this->force_ascii ? Translit::transliterate($org) : $org);
84
        }
85
86
        if (!$this->skip_int) {
87
            $this->set(sprintf($path, 'int'), Translit::transliterate($org));
88
        }
89
    }
90
91
    public function appendStreet($path, $street)
92
    {
93
        if (!$this->skip_loc) {
94
            $this->set(sprintf($path, 'loc'), $this->force_ascii ? Translit::transliterate($street) : $street);
95
        }
96
97
        if (!$this->skip_int) {
98
            $this->set(sprintf($path, 'int'), Translit::transliterate($street));
99
        }
100
    }
101
102
    public function appendCity($path, $city)
103
    {
104
        if (!$this->skip_loc) {
105
            $this->set(sprintf($path, 'loc'), $this->force_ascii ? Translit::transliterate($city) : $city);
106
        }
107
108
        if (!$this->skip_int) {
109
            $this->set(sprintf($path, 'int'), Translit::transliterate($city));
110
        }
111
    }
112
113
    public function appendProvince($path, $sp)
114
    {
115
        if (!$this->skip_loc) {
116
            $this->set(sprintf($path, 'loc'), $this->force_ascii ? Translit::transliterate($sp) : $sp);
117
        }
118
119
        if (!$this->skip_int) {
120
            $this->set(sprintf($path, 'int'), Translit::transliterate($sp));
121
        }
122
    }
123
124
    public function appendPostalCode($path, $pc)
125
    {
126
        if (!$this->skip_loc) {
127
            $this->set(sprintf($path, 'loc'), $this->force_ascii ? Translit::transliterate($pc) : $pc);
128
        }
129
130
        if (!$this->skip_int) {
131
            $this->set(sprintf($path, 'int'), Translit::transliterate($pc));
132
        }
133
    }
134
135
    public function appendCountryCode($path, $cc)
136
    {
137
        if (!Validator::isCountryCode($cc)) {
138
            throw new Exception(sprintf('the country-code: \'%s\' is unknown', $cc));
139
        }
140
141
        if (!$this->skip_loc) {
142
            $this->set(sprintf($path, 'loc'), $this->force_ascii ? Translit::transliterate($cc) : $cc);
143
        }
144
145
        if (!$this->skip_int) {
146
            $this->set(sprintf($path, 'int'), Translit::transliterate($cc));
147
        }
148
    }
149
150
    public function appendVoice($path, $voice)
151
    {
152
        $this->set($path, $voice);
153
    }
154
155
    public function appendFax($path, $fax)
156
    {
157
        $this->set($path, $fax);
158
    }
159
160
    public function appendEmail($path, $email)
161
    {
162
        if (!Validator::isEmail($email)) {
163
            throw new Exception(sprintf('%s is not a valid email', $email));
164
        }
165
166
        $this->set($path, $email);
167
    }
168
169
    public function appendAuthInfo($path, $pw = null)
170
    {
171
        if ($pw === null) {
172
            $pw = Random::auth(12);
173
        }
174
        $this->set($path, $pw);
175
176
        return $pw;
177
    }
178
179
    public function appendDisclose($path)
180
    {
181
        $this->set($path);
182
    }
183
}
184