Completed
Push — master ( 2f7f6c...a88bdd )
by Alexey
05:49
created

Param   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 116
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 116
loc 116
rs 10
wmc 10
lcom 0
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
B indexes() 24 24 1
A realType() 16 16 4
A name() 4 4 1
A value() 9 9 3
A relations() 21 21 1

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
 * Item param
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Ecommerce\Item\Offer;
13
14 View Code Duplication
class Param extends \Model
15
{
16
    public static $objectName = 'Параметр товара';
17
    public static $labels = [
18
        'item_offer_option_id' => 'Параметр предложения',
19
        'item_offer_id' => 'Предложение',
20
        'value' => 'Значение',
21
    ];
22
    public static $cols = [
23
        //Основные параметры
24
        'item_offer_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'offer'],
25
        'item_offer_option_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'option', 'onChange' => 'reloadForm'],
26
        'value' => ['type' => 'dynamicType', 'typeSource' => 'selfMethod', 'selfMethod' => 'realType'],
27
        //Системные
28
        'date_create' => ['type' => 'dateTime']
29
    ];
30
31
    public static function indexes()
32
    {
33
        return [
34
            'ecommerce_itemOfferOptionRelation' => [
35
                'type' => 'INDEX',
36
                'cols' => [
37
                    'item_offer_param_item_offer_id',
38
                    'item_offer_param_item_offer_option_id'
39
                ]
40
            ],
41
            'ecommerce_paramItemOfferIndex' => [
42
                'type' => 'INDEX',
43
                'cols' => [
44
                    'item_offer_param_item_offer_id',
45
                ]
46
            ],
47
            'ecommerce_paramOfferOptionIndex' => [
48
                'type' => 'INDEX',
49
                'cols' => [
50
                    'item_offer_param_item_offer_option_id'
51
                ]
52
            ],
53
        ];
54
    }
55
56
    public function realType()
57
    {
58
        if ($this->option && $this->option->type)  {
59
            $type = $this->option->type;
60
61
            if ($type == 'select') {
62
                return [
63
                    'type' => 'select',
64
                    'source' => 'relation',
65
                    'relation' => 'option:items',
66
                ];
67
            }
68
            return $type;
69
        }
70
        return 'text';
71
    }
72
73
    public static $dataManagers = [
74
75
        'manager' => [
76
            'name' => 'Параметры предложения',
77
            'cols' => [
78
                'item_offer_option_id',
79
                'item_offer_id',
80
                'value',
81
            ],
82
        ],
83
    ];
84
    public static $forms = [
85
        'manager' => [
86
            'map' => [
87
                ['item_offer_id', 'item_offer_option_id'],
88
                ['value']
89
            ]
90
    ]];
91
92
    function name()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
93
    {
94
        return $this->value;
95
    }
96
97
    public function value($default = '')
98
    {
99
        if ($this->option->type != 'select') {
100
            return $this->value;
101
        } elseif ($this->optionItem) {
102
            return $this->optionItem->value;
103
        }
104
        return $default;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $default; (string) is incompatible with the return type of the parent method Model::value of type Value|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
105
    }
106
107
    public static function relations()
108
    {
109
        return [
110
            'file' => [
111
                'model' => 'Files\File',
112
                'col' => 'value'
113
            ],
114
            'option' => [
115
                'model' => 'Ecommerce\Item\Offer\Option',
116
                'col' => 'item_offer_option_id'
117
            ],
118
            'offer' => [
119
                'model' => 'Ecommerce\Item\Offer',
120
                'col' => 'item_offer_id'
121
            ],
122
            'optionItem' => [
123
                'model' => 'Ecommerce\Item\Offer\Option\Item',
124
                'col' => 'value'
125
            ]
126
        ];
127
    }
128
129
}
130