Completed
Push — master ( e8241e...60d0c3 )
by
unknown
04:24 queued 02:47
created

Book   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 144
Duplicated Lines 22.92 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 15%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 33
loc 144
ccs 3
cts 20
cp 0.15
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getUrl() 19 19 3
A text() 14 14 3

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
namespace Lan\Ebs\Sdk\Model;
4
5
use Exception;
6
use Lan\Ebs\Sdk\Classes\Model;
7
use Lan\Ebs\Sdk\Client;
8
9
/**
10
 * @property mixed name
11
 * @property mixed description
12
 * @property mixed isbn
13
 * @property mixed year
14
 * @property mixed edition
15
 * @property mixed pages
16
 * @property mixed specialMarks
17
 * @property mixed classification
18
 * @property mixed authors
19
 * @property mixed authorAdditions
20
 * @property mixed bibliographicRecord
21
 * @property mixed contentQuality
22
 * @property mixed publisher
23
 * @property mixed url
24
 * @property mixed thumb
25
 */
26
class Book extends Model
27
{
28
    /**
29
     * Наименование книги
30
     */
31
    const FIELD_NAME = 'name';
32
33
    /**
34
     * Описание книги
35
     */
36
    const FIELD_DESCRIPTION = 'description';
37
38
    /**
39
     * ISBN книги
40
     */
41
    const FIELD_ISBN = 'isbn';
42
43
    /**
44
     * Год издания книги
45
     */
46
    const FIELD_YEAR = 'year';
47
48
    /**
49
     * Издание
50
     */
51
    const FIELD_EDITION = 'edition';
52
53
    /**
54
     * Объем книги
55
     */
56
    const FIELD_PAGES = 'pages';
57
58
    /**
59
     * Специальные отметки
60
     */
61
    const FIELD_SPECIAL_MARKS = 'specialMarks';
62
63
    /**
64
     * Гриф
65
     */
66
    const FIELD_CLASSIFICATION = 'classification';
67
68
    /**
69
     * Авторы
70
     */
71
    const FIELD_AUTHORS = 'authors';
72
73
    /**
74
     * Дополнительные авторы
75
     */
76
    const FIELD_AUTHOR_ADDITIONS = 'authorAdditions';
77
78
    /**
79
     * Библиографическая запись
80
     */
81
    const FIELD_BIBLIOGRAPHIC_RECORD = 'bibliographicRecord';
82
83
    /**
84
     * Качество текста книг (процент)
85
     */
86
    const FIELD_CONTENT_QUALITY = 'contentQuality';
87
88
    /**
89
     * Издательство
90
     */
91
    const FIELD_PUBLISHER = 'publisher';
92
93
    /**
94
     * Ссылка на карточку книги
95
     */
96
    const FIELD_URL = 'url';
97
98
    /**
99
     * Ссылка на обложку книги
100
     */
101
    const FIELD_THUMB = 'thumb';
102
103
    /**
104
     * Конструктор модели пользователя
105
     *
106
     * @param Client $client Инстанс клиента
107
     * @param array $fields Поля для выборки
108
     *
109
     * @throws Exception
110
     */
111 2
    public function __construct(Client $client, array $fields = [])
112
    {
113 2
        parent::__construct($client, $fields);
114 2
    }
115
116
    /**
117
     * Получение данных для запроса через API
118
     *
119
     * @param string $method Http-метод запроса
120
     * @param array $params Параметры для формирования урла
121
     *
122
     * @return array
123
     *
124
     * @throws Exception
125
     */
126 View Code Duplication
    public function getUrl($method, array $params = [])
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...
127
    {
128
        switch ($method) {
129
            case 'get':
130
                return [
131
                    'url' => vsprintf('/1.0/resource/book/get/%d', $params),
132
                    'method' => 'GET',
133
                    'code' => 200
134
                ];
135
            case 'text':
136
                return [
137
                    'url' => vsprintf('/1.0/resource/book/text/%d', $params),
138
                    'method' => 'GET',
139
                    'code' => 200
140
                ];
141
            default:
142
                throw new Exception('Route for ' . $method . ' not found');
143
        }
144
    }
145
146
    /**
147
     * Получение текстов книги
148
     *
149
     * @param int $id Идентификатор модели
150
     *
151
     * @return array
152
     *
153
     * @throws Exception
154
     */
155 View Code Duplication
    public function text($id = null)
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...
156
    {
157
        if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
158
            $this->setId($id);
159
        } else {
160
            $id = $this->getId();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $id is correct as $this->getId() (which targets Lan\Ebs\Sdk\Classes\Model::getId()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
161
        }
162
163
        if (empty($id)) {
164
            throw new Exception(Model::MESSAGE_ID_REQUIRED);
165
        }
166
167
        return $this->getClient()->getResponse($this->getUrl(__FUNCTION__, [$id]))['data'];
168
    }
169
}