BuildErrorBase   B
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 496
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 36
c 2
b 2
f 0
lcom 1
cbo 3
dl 0
loc 496
ccs 0
cts 118
cp 0
rs 8.8

21 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 6 1
A getBuildId() 0 6 1
A getPlugin() 0 6 1
A getFile() 0 6 1
A getLineStart() 0 6 1
A getLineEnd() 0 6 1
A getSeverity() 0 6 1
A getMessage() 0 6 1
A getCreatedDate() 0 10 2
A setId() 0 13 2
A setBuildId() 0 13 2
A setPlugin() 0 13 2
A setFile() 0 12 2
A setLineStart() 0 12 2
A setLineEnd() 0 12 2
A setSeverity() 0 13 2
A setMessage() 0 13 2
A setCreatedDate() 0 13 2
A getBuild() 0 18 3
A setBuild() 0 15 4
A setBuildObject() 0 4 1
1
<?php
2
3
/**
4
 * BuildError base model for table: build_error.
5
 */
6
7
namespace PHPCI\Model\Base;
8
9
use PHPCI\Model;
10
use b8\Store\Factory;
11
12
/**
13
 * BuildError Base Model.
14
 */
15
class BuildErrorBase extends Model
16
{
17
    /**
18
     * @var array
19
     */
20
    public static $sleepable = array();
21
22
    /**
23
     * @var string
24
     */
25
    protected $tableName = 'build_error';
26
27
    /**
28
     * @var string
29
     */
30
    protected $modelName = 'BuildError';
31
32
    /**
33
     * @var array
34
     */
35
    protected $data = array(
36
        'id' => null,
37
        'build_id' => null,
38
        'plugin' => null,
39
        'file' => null,
40
        'line_start' => null,
41
        'line_end' => null,
42
        'severity' => null,
43
        'message' => null,
44
        'created_date' => null,
45
    );
46
47
    /**
48
     * @var array
49
     */
50
    protected $getters = array(
51
        // Direct property getters:
52
        'id' => 'getId',
53
        'build_id' => 'getBuildId',
54
        'plugin' => 'getPlugin',
55
        'file' => 'getFile',
56
        'line_start' => 'getLineStart',
57
        'line_end' => 'getLineEnd',
58
        'severity' => 'getSeverity',
59
        'message' => 'getMessage',
60
        'created_date' => 'getCreatedDate',
61
62
        // Foreign key getters:
63
        'Build' => 'getBuild',
64
    );
65
66
    /**
67
     * @var array
68
     */
69
    protected $setters = array(
70
        // Direct property setters:
71
        'id' => 'setId',
72
        'build_id' => 'setBuildId',
73
        'plugin' => 'setPlugin',
74
        'file' => 'setFile',
75
        'line_start' => 'setLineStart',
76
        'line_end' => 'setLineEnd',
77
        'severity' => 'setSeverity',
78
        'message' => 'setMessage',
79
        'created_date' => 'setCreatedDate',
80
81
        // Foreign key setters:
82
        'Build' => 'setBuild',
83
    );
84
85
    /**
86
     * @var array
87
     */
88
    public $columns = array(
89
        'id' => array(
90
            'type' => 'int',
91
            'length' => 11,
92
            'primary_key' => true,
93
            'auto_increment' => true,
94
            'default' => null,
95
        ),
96
        'build_id' => array(
97
            'type' => 'int',
98
            'length' => 11,
99
            'default' => null,
100
        ),
101
        'plugin' => array(
102
            'type' => 'varchar',
103
            'length' => 100,
104
            'default' => null,
105
        ),
106
        'file' => array(
107
            'type' => 'varchar',
108
            'length' => 250,
109
            'nullable' => true,
110
            'default' => null,
111
        ),
112
        'line_start' => array(
113
            'type' => 'int',
114
            'length' => 11,
115
            'nullable' => true,
116
            'default' => null,
117
        ),
118
        'line_end' => array(
119
            'type' => 'int',
120
            'length' => 11,
121
            'nullable' => true,
122
            'default' => null,
123
        ),
124
        'severity' => array(
125
            'type' => 'tinyint',
126
            'length' => 3,
127
            'default' => null,
128
        ),
129
        'message' => array(
130
            'type' => 'varchar',
131
            'length' => 250,
132
            'default' => null,
133
        ),
134
        'created_date' => array(
135
            'type' => 'datetime',
136
            'default' => null,
137
        ),
138
    );
139
140
    /**
141
     * @var array
142
     */
143
    public $indexes = array(
144
            'PRIMARY' => array('unique' => true, 'columns' => 'id'),
145
            'build_id' => array('columns' => 'build_id, created_date'),
146
    );
147
148
    /**
149
     * @var array
150
     */
151
    public $foreignKeys = array(
152
            'build_error_ibfk_1' => array(
153
                'local_col' => 'build_id',
154
                'update' => 'CASCADE',
155
                'delete' => 'CASCADE',
156
                'table' => 'build',
157
                'col' => 'id',
158
                ),
159
    );
160
161
    /**
162
     * Get the value of Id / id.
163
     *
164
     * @return int
165
     */
166
    public function getId()
167
    {
168
        $rtn = $this->data['id'];
169
170
        return $rtn;
171
    }
172
173
    /**
174
     * Get the value of BuildId / build_id.
175
     *
176
     * @return int
177
     */
178
    public function getBuildId()
179
    {
180
        $rtn = $this->data['build_id'];
181
182
        return $rtn;
183
    }
184
185
    /**
186
     * Get the value of Plugin / plugin.
187
     *
188
     * @return string
189
     */
190
    public function getPlugin()
191
    {
192
        $rtn = $this->data['plugin'];
193
194
        return $rtn;
195
    }
196
197
    /**
198
     * Get the value of File / file.
199
     *
200
     * @return string
201
     */
202
    public function getFile()
203
    {
204
        $rtn = $this->data['file'];
205
206
        return $rtn;
207
    }
208
209
    /**
210
     * Get the value of LineStart / line_start.
211
     *
212
     * @return int
213
     */
214
    public function getLineStart()
215
    {
216
        $rtn = $this->data['line_start'];
217
218
        return $rtn;
219
    }
220
221
    /**
222
     * Get the value of LineEnd / line_end.
223
     *
224
     * @return int
225
     */
226
    public function getLineEnd()
227
    {
228
        $rtn = $this->data['line_end'];
229
230
        return $rtn;
231
    }
232
233
    /**
234
     * Get the value of Severity / severity.
235
     *
236
     * @return int
237
     */
238
    public function getSeverity()
239
    {
240
        $rtn = $this->data['severity'];
241
242
        return $rtn;
243
    }
244
245
    /**
246
     * Get the value of Message / message.
247
     *
248
     * @return string
249
     */
250
    public function getMessage()
251
    {
252
        $rtn = $this->data['message'];
253
254
        return $rtn;
255
    }
256
257
    /**
258
     * Get the value of CreatedDate / created_date.
259
     *
260
     * @return \DateTime
261
     */
262
    public function getCreatedDate()
263
    {
264
        $rtn = $this->data['created_date'];
265
266
        if (!empty($rtn)) {
267
            $rtn = new \DateTime($rtn);
268
        }
269
270
        return $rtn;
271
    }
272
273
    /**
274
     * Set the value of Id / id.
275
     *
276
     * Must not be null.
277
     *
278
     * @param $value int
279
     */
280
    public function setId($value)
281
    {
282
        $this->_validateNotNull('Id', $value);
283
        $this->_validateInt('Id', $value);
284
285
        if ($this->data['id'] === $value) {
286
            return;
287
        }
288
289
        $this->data['id'] = $value;
290
291
        $this->_setModified('id');
292
    }
293
294
    /**
295
     * Set the value of BuildId / build_id.
296
     *
297
     * Must not be null.
298
     *
299
     * @param $value int
300
     */
301
    public function setBuildId($value)
302
    {
303
        $this->_validateNotNull('BuildId', $value);
304
        $this->_validateInt('BuildId', $value);
305
306
        if ($this->data['build_id'] === $value) {
307
            return;
308
        }
309
310
        $this->data['build_id'] = $value;
311
312
        $this->_setModified('build_id');
313
    }
314
315
    /**
316
     * Set the value of Plugin / plugin.
317
     *
318
     * Must not be null.
319
     *
320
     * @param $value string
321
     */
322
    public function setPlugin($value)
323
    {
324
        $this->_validateNotNull('Plugin', $value);
325
        $this->_validateString('Plugin', $value);
326
327
        if ($this->data['plugin'] === $value) {
328
            return;
329
        }
330
331
        $this->data['plugin'] = $value;
332
333
        $this->_setModified('plugin');
334
    }
335
336
    /**
337
     * Set the value of File / file.
338
     *
339
     * @param $value string
340
     */
341
    public function setFile($value)
342
    {
343
        $this->_validateString('File', $value);
344
345
        if ($this->data['file'] === $value) {
346
            return;
347
        }
348
349
        $this->data['file'] = $value;
350
351
        $this->_setModified('file');
352
    }
353
354
    /**
355
     * Set the value of LineStart / line_start.
356
     *
357
     * @param $value int
358
     */
359
    public function setLineStart($value)
360
    {
361
        $this->_validateInt('LineStart', $value);
362
363
        if ($this->data['line_start'] === $value) {
364
            return;
365
        }
366
367
        $this->data['line_start'] = $value;
368
369
        $this->_setModified('line_start');
370
    }
371
372
    /**
373
     * Set the value of LineEnd / line_end.
374
     *
375
     * @param $value int
376
     */
377
    public function setLineEnd($value)
378
    {
379
        $this->_validateInt('LineEnd', $value);
380
381
        if ($this->data['line_end'] === $value) {
382
            return;
383
        }
384
385
        $this->data['line_end'] = $value;
386
387
        $this->_setModified('line_end');
388
    }
389
390
    /**
391
     * Set the value of Severity / severity.
392
     *
393
     * Must not be null.
394
     *
395
     * @param $value int
396
     */
397
    public function setSeverity($value)
398
    {
399
        $this->_validateNotNull('Severity', $value);
400
        $this->_validateInt('Severity', $value);
401
402
        if ($this->data['severity'] === $value) {
403
            return;
404
        }
405
406
        $this->data['severity'] = $value;
407
408
        $this->_setModified('severity');
409
    }
410
411
    /**
412
     * Set the value of Message / message.
413
     *
414
     * Must not be null.
415
     *
416
     * @param $value string
417
     */
418
    public function setMessage($value)
419
    {
420
        $this->_validateNotNull('Message', $value);
421
        $this->_validateString('Message', $value);
422
423
        if ($this->data['message'] === $value) {
424
            return;
425
        }
426
427
        $this->data['message'] = $value;
428
429
        $this->_setModified('message');
430
    }
431
432
    /**
433
     * Set the value of CreatedDate / created_date.
434
     *
435
     * Must not be null.
436
     *
437
     * @param $value \DateTime
438
     */
439
    public function setCreatedDate($value)
440
    {
441
        $this->_validateNotNull('CreatedDate', $value);
442
        $this->_validateDate('CreatedDate', $value);
443
444
        if ($this->data['created_date'] === $value) {
445
            return;
446
        }
447
448
        $this->data['created_date'] = $value;
449
450
        $this->_setModified('created_date');
451
    }
452
453
    /**
454
     * Get the Build model for this BuildError by Id.
455
     *
456
     * @uses \PHPCI\Store\BuildStore::getById()
457
     * @uses \PHPCI\Model\Build
458
     *
459
     * @return \PHPCI\Model\Build
460
     */
461
    public function getBuild()
462
    {
463
        $key = $this->getBuildId();
464
465
        if (empty($key)) {
466
            return;
467
        }
468
469
        $cacheKey = 'Cache.Build.'.$key;
470
        $rtn = $this->cache->get($cacheKey, null);
471
472
        if (empty($rtn)) {
473
            $rtn = Factory::getStore('Build', 'PHPCI')->getById($key);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class b8\Store as the method getById() does only exist in the following sub-classes of b8\Store: PHPCI\Store\Base\BuildErrorStoreBase, PHPCI\Store\Base\BuildMetaStoreBase, PHPCI\Store\Base\BuildStoreBase, PHPCI\Store\Base\ProjectGroupStoreBase, PHPCI\Store\Base\ProjectStoreBase, PHPCI\Store\Base\UserStoreBase, PHPCI\Store\BuildErrorStore, PHPCI\Store\BuildMetaStore, PHPCI\Store\BuildStore, PHPCI\Store\ProjectGroupStore, PHPCI\Store\ProjectStore, PHPCI\Store\UserStore. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
474
            $this->cache->set($cacheKey, $rtn);
475
        }
476
477
        return $rtn;
478
    }
479
480
    /**
481
     * Set Build - Accepts an ID, an array representing a Build or a Build model.
482
     *
483
     * @param $value mixed
484
     */
485
    public function setBuild($value)
486
    {
487
        // Is this an instance of Build?
488
        if ($value instanceof \PHPCI\Model\Build) {
489
            return $this->setBuildObject($value);
490
        }
491
492
        // Is this an array representing a Build item?
493
        if (is_array($value) && !empty($value['id'])) {
494
            return $this->setBuildId($value['id']);
495
        }
496
497
        // Is this a scalar value representing the ID of this foreign key?
498
        return $this->setBuildId($value);
499
    }
500
501
    /**
502
     * Set Build - Accepts a Build model.
503
     * 
504
     * @param $value \PHPCI\Model\Build
505
     */
506
    public function setBuildObject(\PHPCI\Model\Build $value)
507
    {
508
        return $this->setBuildId($value->getId());
509
    }
510
}
511