Passed
Push — master ( 17bffe...10bee5 )
by Stefan
06:13
created

marshalObject()   C

Complexity

Conditions 17
Paths 168

Size

Total Lines 51
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
c 0
b 0
f 0
rs 5.1984
cc 17
eloc 38
nc 168
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* 
3
 *******************************************************************************
4
 * Copyright 2011-2017 DANTE Ltd. and GÉANT on behalf of the GN3, GN3+, GN4-1 
5
 * and GN4-2 consortia
6
 *
7
 * License: see the web/copyright.php file in the file structure
8
 *******************************************************************************
9
 */
10
11
/**
12
 * This file contains class definitions and procedures for 
13
 * generation of a generic XML description of a 802.1x configurator
14
 *
15
 * @author Maja Górecka-Wolniewicz <[email protected]>
16
 *
17
 * @package ModuleWriting
18
 */
19
namespace devices\xml;
20
/**
21
 * base class extended by every element
22
 */
23
class XMLElement {
24
25
    private $attributes;
26
    private $value;
27
28
    protected function getObjectVars($obj) {
29
        return get_object_vars($obj);
30
    }
31
32
    /**
33
     *  @var array $AuthMethodElements is used to limit
34
     *  XML elements present within ServerSideCredentials and
35
     *  ClientSideCredentials to ones which are relevant
36
     *  for a given EAP method.
37
     *  @var array of XLM element names which are allowed
38
     *  EAP method names are defined in core/EAP.php
39
     */
40
    public static $authMethodElements = [
41
        'server' => [
42
            \core\common\EAP::TLS => ['CA', 'ServerID'],
43
            \core\common\EAP::FAST => ['CA', 'ServerID'],
44
            \core\common\EAP::PEAP => ['CA', 'ServerID'],
45
            \core\common\EAP::TTLS => ['CA', 'ServerID'],
46
            \core\common\EAP::PWD => ['ServerID'],
47
        ],
48
        'client' => [
49
            \core\common\EAP::TLS => ['UserName', 'Password', 'ClientCertificate'],
50
            \core\common\EAP::MSCHAP2 => ['UserName', 'Password', 'OuterIdentity'],
51
            \core\common\EAP::GTC => ['UserName', 'OneTimeToken'],
52
            \core\common\EAP::NE_PAP => ['UserName', 'Password', 'OuterIdentity'],
53
            \core\common\EAP::NE_SILVERBULLET => ['UserName', 'ClientCertificate'],
54
        ]
55
    ];
56
57
    public function __construct() {
58
        $this->attributes = [];
59
        $this->value = [];
60
    }
61
62
    public function setAttributes($attributes) {
63
        $this->attributes = $attributes;
64
    }
65
66
    public function getAttributes() {
67
        return $this->attributes;
68
    }
69
70
    public function setValue($value) {
71
        $this->value = $value;
72
    }
73
74
    public function getValue() {
75
        return $this->value;
76
    }
77
78
    public function areAttributes() {
79
        return empty($this->attributes) ? 0 : 1;
80
    }
81
82
    /**
83
     * adds an attribute with the given value to the set of attributes
84
     * @param string $attribute
85
     * @param mixed $value
86
     */
87
    public function setAttribute($attribute, $value) {
88
        if (!isset($this->attributes)) {
89
            $this->attributes = [];
90
        }
91
        $this->attributes[$attribute] = $value;
92
    }
93
94
    /**
95
     * 
96
     * @param string $property
97
     * @param mixed $value
98
     */
99
    public function setProperty($property, $value) {
100
        $this->$property = $value;
101
    }
102
103
    public function getAll() {
104
        $elems = get_object_vars($this);
105
        $objvars = [];
106
        foreach ($elems as $key => $val) {
107
            if (($key != 'attributes') && ($key != 'value')) {
108
                $objvars[$key] = $val;
109
            }
110
        }
111
        return $objvars;
112
    }
113
114
}
115
116
class EAPIdentityProvider extends XMLElement {
117
118
    protected $ValidUntil;
119
    protected $AuthenticationMethods;
120
    protected $ProviderInfo;
121
    protected $VendorSpecific;
122
123
}
124
125
class AuthenticationMethods extends XMLElement {
126
127
    protected $AuthenticationMethod;
128
129
}
130
131
class AuthenticationMethod extends XMLElement {
132
133
    protected $EAPMethod;
134
    protected $ServerSideCredential;
135
    protected $ClientSideCredential;
136
    protected $InnerAuthenticationMethod;
137
138
}
139
140
class EAPMethod extends XMLElement {
141
142
    protected $Type;
143
    protected $TypeSpecific;
144
    protected $VendorSpecific;
145
146
}
147
148
class NonEAPAuthMethod extends XMLElement {
149
150
    protected $Type;
151
    protected $TypeSpecific;
152
    protected $VendorSpecific;
153
154
}
155
156
class Type extends XMLElement {
157
    
158
}
159
160
class TypeSpecific extends XMLElement {
161
    
162
}
163
164
class VendorSpecific extends XMLElement {
165
    
166
}
167
168
class ServerSideCredential extends XMLElement {
169
170
    protected $CA; // multi
171
    protected $ServerID; //multi
172
173 View Code Duplication
    public function getAll() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
        if (isset(XMLElement::$authMethodElements['server'][$this->EAPType]) && XMLElement::$authMethodElements['server'][$this->EAPType]) {
0 ignored issues
show
Bug Best Practice introduced by
The property EAPType does not exist on devices\xml\ServerSideCredential. Did you maybe forget to declare it?
Loading history...
175
            $element = XMLElement::$authMethodElements['server'][$this->EAPType];
176
            $objectVariables = get_object_vars($this);
177
            $outArray = [];
178
            foreach ($objectVariables as $o => $v) {
179
                if (in_array($o, $element)) {
180
                    $outArray[$o] = $v;
181
                }
182
            }
183
            return($outArray);
184
        }
185
    }
186
187
}
188
189
class ServerID extends XMLElement {
190
    
191
}
192
193
class ClientSideCredential extends XMLElement {
194
195
    protected $OuterIdentity;
196
    protected $UserName;
197
    protected $Password;
198
    protected $ClientCertificate;
199
    protected $Passphrase;
200
    protected $PAC;
201
    protected $ProvisionPAC;
202
203 View Code Duplication
    public function getAll() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
        if (isset(XMLElement::$authMethodElements['client'][$this->EAPType]) && XMLElement::$authMethodElements['client'][$this->EAPType]) {
0 ignored issues
show
Bug Best Practice introduced by
The property EAPType does not exist on devices\xml\ClientSideCredential. Did you maybe forget to declare it?
Loading history...
205
            $element = XMLElement::$authMethodElements['client'][$this->EAPType];
206
            $objectVars = get_object_vars($this);
207
            $outputArray = [];
208
            foreach ($objectVars as $name => $value) {
209
                if (in_array($name, $element)) {
210
                    $outputArray[$name] = $value;
211
                }
212
            }
213
            return($outputArray);
214
        }
215
    }
216
217
}
218
219
class ClientCertificate extends XMLElement {
220
    
221
}
222
223
class CA extends XMLElement {
224
    
225
}
226
227
class InnerAuthenticationMethod extends XMLElement {
228
229
    protected $EAPMethod;
230
    protected $NonEAPAuthMethod;
231
    protected $ServerSideCredential;
232
    protected $ClientSideCredential;
233
234
}
235
236
class ProviderInfo extends XMLElement {
237
238
    protected $DisplayName;
239
    protected $Description;
240
    protected $ProviderLocation;
241
    protected $ProviderLogo;
242
    protected $TermsOfUse;
243
    protected $Helpdesk;
244
245
}
246
247
class DisplayName extends XMLElement {
248
    
249
}
250
251
class Description extends XMLElement {
252
    
253
}
254
255
class ProviderLocation extends XMLElement {
256
257
    protected $Longitude;
258
    protected $Latitude;
259
260
}
261
262
class ProviderLogo extends XMLElement {
263
    
264
}
265
266
class TermsOfUse extends XMLElement {
267
    
268
}
269
270
class Helpdesk extends XMLElement {
271
272
    protected $EmailAddress;
273
    protected $WebAddress;
274
    protected $Phone;
275
276
}
277
278
class EmailAddress extends XMLElement {
279
    
280
}
281
282
class WebAddress extends XMLElement {
283
    
284
}
285
286
class Phone extends XMLElement {
287
    
288
}
289
290
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
291
  class CompatibleUses  extends XMLElement {
292
  protected $IEEE80211;
293
  protected $IEEE8023;
294
  protected $ABFAB;
295
  }
296
  class IEEE80211 extends XMLElement {
297
  protected $SSID;
298
  protected $ConsortiumOID;
299
  protected $MinRSNProto;
300
  }
301
302
  class IEEE8023 extends XMLElement {
303
  protected $NetworkID;
304
  }
305
  class ABFAB extends XMLElement {
306
  protected $ServiceIdentifier;
307
  }
308
309
 */
310
311
/**
312
 * 
313
 * @param \SimpleXMLElement $key
314
 * @param \SimpleXMLElement $value
315
 */
316
function SimpleXMLElement_append($key, $value) {
317
    if (trim((string) $value) == '') {
318
        $element = $key->addChild($value->getName());
319
        foreach ($value->attributes() as $attKey => $attValue) {
320
            $element->addAttribute($attKey, $attValue);
321
        }
322
        foreach ($value->children() as $child) {
323
            SimpleXMLElement_append($element, $child);
324
        }
325
    } else {
326
        $element = $key->addChild($value->getName(), trim((string) $value));
0 ignored issues
show
Unused Code introduced by
The assignment to $element is dead and can be removed.
Loading history...
327
    }
328
}
329
330
/**
331
 * 
332
 * @param \SimpleXMLElement $node
333
 * @param EAPIdentityProvider $object
334
 * @return void
335
 */
336
function marshalObject($node, $object) {
337
    $val = '';
338
    $qualClassName = get_class($object);
339
    // remove namespace qualifier
340
    $pos = strrpos($qualClassName, '\\');
341
    $className =  substr($qualClassName, $pos + 1);
342
    $name = preg_replace("/_/", "-", $className);
343
    if ($object->getValue()) {
344
        $val = $object->getValue();
345
    }
346
    $simplexmlelement = NULL;
347
    if ($val instanceof \SimpleXMLElement) {
348
        $simplexmlelement = $val;
349
        $val = '';
350
    }
351
    if ($val) {
352
        if (getType($val) == 'string') {
353
            $val = preg_replace('/&/', '&amp;', $val);
354
        }
355
        $node = $node->addChild($name, $val);
0 ignored issues
show
Bug introduced by
It seems like $val can also be of type array; however, parameter $value of SimpleXMLElement::addChild() 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

355
        $node = $node->addChild($name, /** @scrutinizer ignore-type */ $val);
Loading history...
356
    } else {
357
        $node = $node->addChild($name);
358
    }
359
    if ($object->areAttributes()) {
360
        $attrs = $object->getAttributes();
361
        foreach ($attrs as $attrt => $attrv) {
362
            $node->addAttribute($attrt, $attrv);
363
        }
364
    }
365
    if ($simplexmlelement !== NULL) {
366
        SimpleXMLElement_append($node, $simplexmlelement);
367
        return;
368
    }
369
    $fields = $object->getAll();
370
    if (empty($fields)) {
371
        return;
372
    }
373
374
    foreach ($fields as $name => $value) {
375
        if (getType($value) == 'string' || getType($value) == 'integer' || getType($value) == 'double') {
376
            $node->addChild($name, $value);
377
        } else {
378
            if (getType($value) == 'array') {
379
                foreach ($value as $insideValue) {
380
                    if (is_object($insideValue)) {
381
                        marshalObject($node, $insideValue);
382
                    }
383
                }
384
            } else {
385
                if (getType($value) == 'object') {
386
                    marshalObject($node, $value);
387
                }
388
            }
389
        }
390
    }
391
}
392