Passed
Push — master ( 700b0f...aafb0a )
by Björn
18:25 queued 10s
created

UserProfile::exchangeArray()   F

Complexity

Conditions 11
Paths 1024

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 12
cp 0
rs 3.15
c 0
b 0
f 0
cc 11
nc 1024
nop 1
crap 132

How to fix   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
 * BB's Zend Framework 2 Components
4
 * 
5
 * AdminModule
6
 *
7
 * @package   [MyApplication]
8
 * @package   BB's Zend Framework 2 Components
9
 * @package   AdminModule
10
 * @author    Björn Bartels <[email protected]>
11
 * @link      https://gitlab.bjoernbartels.earth/groups/zf2
12
 * @license   http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
13
 * @copyright copyright (c) 2016 Björn Bartels <[email protected]>
14
 */
15
16
namespace Admin\Model;
17
18
use Admin\Module as AdminModule;
19
20
use Zend\InputFilter\InputFilter;
21
use Zend\InputFilter\Factory as InputFactory;
22
use Zend\InputFilter\InputFilterAwareInterface;
23
use Zend\InputFilter\InputFilterInterface;
24
25
class UserProfile implements InputFilterAwareInterface
26
{
27
    public $user_id;
28
    
29
    public $street;
30
    public $city;
31
    public $phone;
32
    public $cell;
33
34
    public $twitter;
35
    public $facebook;
36
    public $skype;
37
    public $icq;
38
39
    protected $inputFilter;
40
    protected $userService;
41
    protected $serviceManager;
42
    protected $serviceLocator;
43
    
44
    public function load( $id )
45
    {
46
        if (!$id ) {
47
            return $this;
48
        }
49
        $this->user_id = $id;
50
        try {
51
            $oModule = new AdminModule();
52
            $oSM = $oModule->getServiceManager();
0 ignored issues
show
Unused Code introduced by
$oSM is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
53
            $table = \Application\Module::getService('Admin\Model\UserProfileTable');
54
            //$oSM->get('Admin\Model\UserProfileTable'); // $this->getServiceManager()->get('Admin\Model\UserProfileTable');
55
            $profile = $table->getUserProfile($id);
56
            if ($profile) {
57
                $this->exchangeArray($profile->getArrayCopy());
58
            }
59
        } catch (\Exception $ex) {
60
            return (false);
61
        }
62
        
63
        return $this;
64
    }
65
    
66
    public function save()
67
    {
68
        try {
69
            $oModule = new AdminModule();
70
            $oSM = $oModule->getServiceManager();
0 ignored issues
show
Unused Code introduced by
$oSM is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
71
            $table = \Application\Module::getService('Admin\Model\UserProfileTable');
72
            //$oSM->get('Admin\Model\UserProfileTable'); // $this->getServiceManager()->get('Admin\Model\UserProfileTable');
73
            $table->saveUserProfile($this);
74
            return true;
75
        } catch (\Exception $ex) {
76
            return $ex->getMessage();
77
        }
78
    }
79
    
80
    public function exchangeArray($data)
81
    {
82
        $this->user_id        = (isset($data['user_id'])) ? $data['user_id'] : null;
83
        
84
        $this->street        = (isset($data['street'])) ? $data['street'] : '';
85
        $this->city            = (isset($data['city'])) ? $data['city'] : '';
86
        $this->country        = (isset($data['country'])) ? $data['country'] : '';
0 ignored issues
show
Bug introduced by
The property country does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
87
        $this->phone        = (isset($data['phone'])) ? $data['phone'] : '';
88
        $this->cell            = (isset($data['cell'])) ? $data['cell'] : '';
89
        
90
        $this->twitter        = (isset($data['twitter'])) ? $data['twitter'] : '';
91
        $this->facebook        = (isset($data['facebook'])) ? $data['facebook'] : '';
92
        $this->skype        = (isset($data['skype'])) ? $data['skype'] : '';
93
        $this->icq            = (isset($data['icq'])) ? $data['icq'] : '';
94
        
95
    }
96
97
    public function getArrayCopy()
98
    {
99
        return get_object_vars($this);
100
    }
101
    
102
    public function setInputFilter(InputFilterInterface $inputFilter)
103
    {
104
        throw new \Exception("Not used");
105
    }
106
107
    public function getInputFilter()
108
    {
109
        if (!$this->inputFilter) {
110
            $inputFilter = new InputFilter();
111
            $factory     = new InputFactory();
112
113
            $inputFilter->add(
114
                $factory->createInput(
115
                    array(
116
                    'name'     => 'user_id',
117
                    'required' => true,
118
                    'filters'  => array(
119
                    array('name' => 'Int'),
120
                    ),
121
                    )
122
                )
123
            );
124
125
            $inputFilter->add(
126
                $factory->createInput(
127
                    array(
128
                    'name'     => 'street',
129
                    'required' => false,
130
                    'filters'  => array(
131
                    array('name' => 'StripTags'),
132
                    array('name' => 'StringTrim'),
133
                    ),
134
                    'validators' => array(
135
                    array(
136
                    'name'    => 'StringLength',
137
                    'options' => array(
138
                            'encoding' => 'UTF-8',
139
                            'min'      => 1,
140
                            'max'      => 100,
141
                    ),
142
                    ),
143
                    ),
144
                    )
145
                )
146
            );
147
148
            $inputFilter->add(
149
                $factory->createInput(
150
                    array(
151
                    'name'     => 'city',
152
                    'required' => false,
153
                    'filters'  => array(
154
                    array('name' => 'StripTags'),
155
                    array('name' => 'StringTrim'),
156
                    ),
157
                    'validators' => array(
158
                    array(
159
                    'name'    => 'StringLength',
160
                    'options' => array(
161
                            'encoding' => 'UTF-8',
162
                            'min'      => 1,
163
                            'max'      => 100,
164
                    ),
165
                    ),
166
                    array(
167
                    'name'    => 'EmailAddress',
168
                    ),
169
                    ),
170
                    )
171
                )
172
            );
173
174
            $inputFilter->add(
175
                $factory->createInput(
176
                    array(
177
                    'name'     => 'country',
178
                    'required' => false,
179
                    'filters'  => array(
180
                    array('name' => 'StripTags'),
181
                    array('name' => 'StringTrim'),
182
                    ),
183
                    'validators' => array(
184
                    array(
185
                    'name'    => 'StringLength',
186
                    'options' => array(
187
                            'encoding' => 'UTF-8',
188
                            'min'      => 1,
189
                            'max'      => 100,
190
                    ),
191
                    ),
192
                    ),
193
                    )
194
                )
195
            );
196
197
            $inputFilter->add(
198
                $factory->createInput(
199
                    array(
200
                    'name'     => 'phone',
201
                    'required' => false,
202
                    'filters'  => array(
203
                    array('name' => 'StripTags'),
204
                    array('name' => 'StringTrim'),
205
                    ),
206
                    'validators' => array(
207
                    array(
208
                    'name'    => 'StringLength',
209
                    'options' => array(
210
                            'encoding' => 'UTF-8',
211
                            'min'      => 1,
212
                            'max'      => 100,
213
                    ),
214
                    ),
215
                    ),
216
                    )
217
                )
218
            );
219
220
            $inputFilter->add(
221
                $factory->createInput(
222
                    array(
223
                    'name'     => 'cell',
224
                    'required' => false,
225
                    'filters'  => array(
226
                    array('name' => 'StripTags'),
227
                    array('name' => 'StringTrim'),
228
                    ),
229
                    'validators' => array(
230
                    array(
231
                    'name'    => 'StringLength',
232
                    'options' => array(
233
                            'encoding' => 'UTF-8',
234
                            'min'      => 1,
235
                            'max'      => 100,
236
                    ),
237
                    ),
238
                    ),
239
                    )
240
                )
241
            );
242
243
            $inputFilter->add(
244
                $factory->createInput(
245
                    array(
246
                    'name'     => 'twitter',
247
                    'required' => false,
248
                    'filters'  => array(
249
                    array('name' => 'StripTags'),
250
                    array('name' => 'StringTrim'),
251
                    ),
252
                    'validators' => array(
253
                    array(
254
                    'name'    => 'StringLength',
255
                    'options' => array(
256
                            'encoding' => 'UTF-8',
257
                            'min'      => 1,
258
                            'max'      => 255,
259
                    ),
260
                    ),
261
                    ),
262
                    )
263
                )
264
            );
265
266
            $inputFilter->add(
267
                $factory->createInput(
268
                    array(
269
                    'name'     => 'facebook',
270
                    'required' => false,
271
                    'filters'  => array(
272
                    array('name' => 'StripTags'),
273
                    array('name' => 'StringTrim'),
274
                    ),
275
                    'validators' => array(
276
                    array(
277
                    'name'    => 'StringLength',
278
                    'options' => array(
279
                            'encoding' => 'UTF-8',
280
                            'min'      => 1,
281
                            'max'      => 255,
282
                    ),
283
                    ),
284
                    ),
285
                    )
286
                )
287
            );
288
289
            $inputFilter->add(
290
                $factory->createInput(
291
                    array(
292
                    'name'     => 'skype',
293
                    'required' => false,
294
                    'filters'  => array(
295
                    array('name' => 'StripTags'),
296
                    array('name' => 'StringTrim'),
297
                    ),
298
                    'validators' => array(
299
                    array(
300
                    'name'    => 'StringLength',
301
                    'options' => array(
302
                            'encoding' => 'UTF-8',
303
                            'min'      => 1,
304
                            'max'      => 255,
305
                    ),
306
                    ),
307
                    ),
308
                    )
309
                )
310
            );
311
312
            $inputFilter->add(
313
                $factory->createInput(
314
                    array(
315
                    'name'     => 'icq',
316
                    'required' => false,
317
                    'filters'  => array(
318
                    array('name' => 'StripTags'),
319
                    array('name' => 'StringTrim'),
320
                    ),
321
                    'validators' => array(
322
                    array(
323
                    'name'    => 'StringLength',
324
                    'options' => array(
325
                            'encoding' => 'UTF-8',
326
                            'min'      => 1,
327
                            'max'      => 255,
328
                    ),
329
                    ),
330
                    ),
331
                    )
332
                )
333
            );
334
335
            $this->inputFilter = $inputFilter;
336
        }
337
338
        return $this->inputFilter;
339
    }
340
    
341
    
342
    
343
    
344
    /**
345
     * Getters/setters for DI stuff
346
     */
347
348
    public function getUserService()
349
    {
350
        if (!$this->userService) {
351
            $this->userService = $this->getServiceManager()->get('zfcuser_user_service');
352
        }
353
        return $this->userService;
354
    }
355
356
    public function setUserService(UserService $userService)
357
    {
358
        $this->userService = $userService;
359
        return $this;
360
    }
361
362
    /**
363
     * Retrieve service manager instance
364
     *
365
     * @return ServiceManager 
366
     */
367
    public function getServiceManager()
368
    {
369
        return $this->serviceManager;
370
    }
371
372
    /**
373
     * Set service manager instance
374
     *
375
     * @param  ServiceManager $serviceManager
376
     * @return User
377
     */
378
    public function setServiceManager(ServiceManager $serviceManager)
379
    {
380
        $this->serviceManager = $serviceManager;
381
        return $this;
382
    }
383
384
    /**
385
     * Set serviceManager instance
386
     *
387
     * @param  ServiceLocatorInterface $serviceLocator
388
     * @return void
389
     */
390
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
391
    {
392
        $this->serviceLocator = $serviceLocator;
393
    }
394
395
    /**
396
     * Retrieve serviceManager instance
397
     *
398
     * @return ServiceLocatorInterface
399
     */
400
    public function getServiceLocator()
401
    {
402
        return $this->serviceLocator;
403
    }
404
    
405
    /**
406
     * @return the $user_id
407
     */
408
    public function getId() 
409
    {
410
        return $this->user_id;
411
    }
412
413
    /**
414
     * @param Ambigous <unknown, NULL> $user_id
415
     */
416
    public function setId($user_id) 
417
    {
418
        $this->user_id = $user_id;
419
    }
420
421
    /**
422
     * @return the $street
423
     */
424
    public function getStreet() 
425
    {
426
        return $this->street;
427
    }
428
429
    /**
430
     * @param Ambigous <string, unknown> $street
431
     */
432
    public function setStreet($street) 
433
    {
434
        $this->street = $street;
435
    }
436
437
    /**
438
     * @return the $city
439
     */
440
    public function getCity() 
441
    {
442
        return $this->city;
443
    }
444
445
    /**
446
     * @param Ambigous <string, unknown> $city
447
     */
448
    public function setCity($city) 
449
    {
450
        $this->city = $city;
451
    }
452
453
    /**
454
     * @return the $phone
455
     */
456
    public function getPhone() 
457
    {
458
        return $this->phone;
459
    }
460
461
    /**
462
     * @param Ambigous <string, unknown> $phone
463
     */
464
    public function setPhone($phone) 
465
    {
466
        $this->phone = $phone;
467
    }
468
469
    /**
470
     * @return the $cell
471
     */
472
    public function getCell() 
473
    {
474
        return $this->cell;
475
    }
476
477
    /**
478
     * @param Ambigous <string, unknown> $cell
479
     */
480
    public function setCell($cell) 
481
    {
482
        $this->cell = $cell;
483
    }
484
485
    /**
486
     * @return the $twitter
487
     */
488
    public function getTwitter() 
489
    {
490
        return $this->twitter;
491
    }
492
493
    /**
494
     * @param Ambigous <string, unknown> $twitter
495
     */
496
    public function setTwitter($twitter) 
497
    {
498
        $this->twitter = $twitter;
499
    }
500
501
    /**
502
     * @return the $facebook
503
     */
504
    public function getFacebook() 
505
    {
506
        return $this->facebook;
507
    }
508
509
    /**
510
     * @param Ambigous <string, unknown> $facebook
511
     */
512
    public function setFacebook($facebook) 
513
    {
514
        $this->facebook = $facebook;
515
    }
516
517
    /**
518
     * @return the $skype
519
     */
520
    public function getSkype() 
521
    {
522
        return $this->skype;
523
    }
524
525
    /**
526
     * @param Ambigous <string, unknown> $skype
527
     */
528
    public function setSkype($skype) 
529
    {
530
        $this->skype = $skype;
531
    }
532
533
    /**
534
     * @return the $icq
535
     */
536
    public function getIcq() 
537
    {
538
        return $this->icq;
539
    }
540
541
    /**
542
     * @param Ambigous <string, unknown> $icq
543
     */
544
    public function setIcq($icq) 
545
    {
546
        $this->icq = $icq;
547
    }
548
549
550
    
551
}