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 |
||
34 | class Article extends Model |
||
35 | { |
||
36 | /** |
||
37 | * Наименование статьи |
||
38 | */ |
||
39 | const FIELD_NAME = 'name'; |
||
40 | |||
41 | /** |
||
42 | * Авторы статьи |
||
43 | */ |
||
44 | const FIELD_AUTHORS = 'authors'; |
||
45 | |||
46 | /** |
||
47 | * Аннотация статьи |
||
48 | */ |
||
49 | const FIELD_DESCRIPTION = 'description'; |
||
50 | |||
51 | /** |
||
52 | * Ключевые слова статьи |
||
53 | */ |
||
54 | const FIELD_KEYWORDS = 'keywords'; |
||
55 | |||
56 | /** |
||
57 | * Страница начала статьи |
||
58 | */ |
||
59 | const START_PAGE = 'startPage'; |
||
60 | |||
61 | /** |
||
62 | * Страница окончания статьи |
||
63 | */ |
||
64 | const FINISH_PAGE = 'finishPage'; |
||
65 | |||
66 | /** |
||
67 | * Библиографическая запись |
||
68 | */ |
||
69 | const FIELD_BIBLIOGRAPHIC_RECORD = 'bibliographicRecord'; |
||
70 | |||
71 | /** |
||
72 | * Конструктор модели пользователя |
||
73 | * |
||
74 | * @param Client $client Инстанс клиента |
||
75 | * @param array $fields Поля для выборки |
||
76 | * |
||
77 | * @throws Exception |
||
78 | */ |
||
79 | public function __construct(Client $client, array $fields = array()) |
||
80 | { |
||
81 | parent::__construct($client, $fields); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Получение текстов статьи |
||
86 | * |
||
87 | * @param int $id Идентификатор модели |
||
88 | * |
||
89 | * @return array |
||
90 | * |
||
91 | * @throws Exception |
||
92 | */ |
||
93 | View Code Duplication | public function text($id = null) |
|
107 | |||
108 | /** |
||
109 | * Получение данных для запроса через API |
||
110 | * |
||
111 | * @param string $method Http-метод запроса |
||
112 | * @param array $params Параметры для формирования урла |
||
113 | * |
||
114 | * @return array |
||
115 | * |
||
116 | * @throws Exception |
||
117 | */ |
||
118 | View Code Duplication | public function getUrl($method, array $params = array()) |
|
137 | } |
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.