Passed
Push — master ( f2dea1...58a092 )
by Nate
04:47
created

Model   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 27.12 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 16
loc 59
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
findRecordByModel() 0 1 ?
A transferToRecord() 0 14 2
A toRecord() 16 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/spark/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/spark
7
 */
8
9
namespace flipbox\spark\services\traits;
10
11
use flipbox\spark\models\Model as BaseModel;
12
use flipbox\spark\records\Record;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 1.2.0
17
 */
18
trait Model
19
{
20
21
    use Object;
22
23
    /*******************************************
24
     * Model -to- Record
25
     *******************************************/
26
27
    /**
28
     * @param BaseModel $model
29
     * @return Record
30
     */
31
    abstract public function findRecordByModel(BaseModel $model);
32
33
    /**
34
     * @param BaseModel $model
35
     * @param Record $record
36
     * @param bool $mirrorScenario
37
     * @return void
38
     */
39
    public function transferToRecord(BaseModel $model, Record $record, bool $mirrorScenario = true)
40
    {
41
42
        if ($mirrorScenario === true) {
43
44
            // Mirror scenarios
45
            $record->setScenario($model->getScenario());
46
47
        }
48
49
        // Transfer attributes
50
        $record->setAttributes($model->toArray());
51
52
    }
53
54
    /**
55
     * @param BaseModel $model
56
     * @param bool $mirrorScenario
57
     * @return Record
58
     */
59 View Code Duplication
    public function toRecord(BaseModel $model, bool $mirrorScenario = true): Record
1 ignored issue
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...
60
    {
61
62
        if (!$record = $this->findRecordByModel($model)) {
63
64
            // Create new record
65
            $record = $this->createRecord();
66
67
        }
68
69
        // Populate the record attributes
70
        $this->transferToRecord($model, $record, $mirrorScenario);
71
72
        return $record;
73
74
    }
75
76
}
77