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 |
||
10 | abstract class AbstractClient implements RequestsInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var Container |
||
14 | */ |
||
15 | protected $container; |
||
16 | |||
17 | /** |
||
18 | * @param Container $container |
||
19 | */ |
||
20 | public function setContainer(Container $container) |
||
24 | |||
25 | /** |
||
26 | * @return Container |
||
27 | */ |
||
28 | public function getContainer() |
||
32 | |||
33 | /** |
||
34 | * Creates a response object |
||
35 | * |
||
36 | * @param $data |
||
37 | * |
||
38 | * @return \Directus\SDK\Response\EntryCollection|\Directus\SDK\Response\Entry |
||
39 | */ |
||
40 | 28 | protected function createResponseFromData($data) |
|
41 | { |
||
42 | 28 | if (isset($data['rows']) || (isset($data['data']) && ArrayUtils::isNumericKeys($data['data']))) { |
|
43 | 6 | $response = new EntryCollection($data); |
|
44 | } else { |
||
45 | 22 | $response = new Entry($data); |
|
46 | } |
||
47 | |||
48 | 28 | return $response; |
|
49 | } |
||
50 | |||
51 | protected function processData($tableName, array $data) |
||
52 | { |
||
53 | $method = 'processDataOn' . StringUtils::underscoreToCamelCase($tableName, true); |
||
54 | if (method_exists($this, $method)) { |
||
55 | $data = call_user_func_array([$this, $method], [$data]); |
||
56 | } |
||
57 | |||
58 | return $data; |
||
59 | } |
||
60 | |||
61 | protected function processFile(File $file) |
||
62 | { |
||
63 | $data = $file->toArray(); |
||
64 | // Not container, we are using remote :) |
||
65 | if (!$this->container) { |
||
66 | return $data; |
||
67 | } |
||
68 | |||
69 | $Files = $this->container->get('files'); |
||
|
|||
70 | |||
71 | if (!array_key_exists('type', $data) || strpos($data['type'], 'embed/') === 0) { |
||
72 | $recordData = $Files->saveEmbedData($data); |
||
73 | } else { |
||
74 | $recordData = $Files->saveData($data['data'], $data['name']); |
||
75 | } |
||
76 | |||
77 | return array_merge($recordData, ArrayUtils::omit($data, ['data', 'name'])); |
||
78 | } |
||
79 | |||
80 | protected function processDataOnDirectusUsers($data) |
||
81 | { |
||
82 | $data = ArrayUtils::omit($data, ['id', 'user', 'access_token', 'last_login', 'last_access', 'last_page']); |
||
83 | if (ArrayUtils::has($data, 'password')) { |
||
84 | // @TODO: use Auth hash password |
||
85 | $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT, ['cost' => 12]); |
||
86 | } |
||
87 | |||
88 | if (ArrayUtils::has($data, 'avatar_file_id') && $data['avatar_file_id'] instanceof File) { |
||
89 | $data['avatar_file_id'] = $this->processFile($data['avatar_file_id']); |
||
90 | } |
||
91 | |||
92 | return $data; |
||
93 | } |
||
94 | |||
95 | protected function processOnDirectusFiles($data) |
||
102 | |||
103 | protected function processDataOnDirectusPreferences($data) |
||
110 | |||
111 | protected function processDataOnDirectusBookmarks($data) |
||
118 | |||
119 | protected function parseColumnData($data) |
||
120 | { |
||
121 | $requiredAttributes = ['name', 'table', 'type', 'ui']; |
||
122 | if (!ArrayUtils::contains($data, $requiredAttributes)) { |
||
123 | throw new \Exception(sprintf('%s are required', implode(',', $requiredAttributes))); |
||
124 | } |
||
125 | |||
126 | $data = ArrayUtils::aliasKeys($data, [ |
||
127 | 'table_name' => 'table', |
||
128 | 'column_name' => 'name', |
||
129 | 'data_type' => 'type', |
||
130 | 'char_length' => 'length' |
||
131 | ]); |
||
132 | |||
133 | return $data; |
||
134 | } |
||
135 | |||
136 | protected function requiredAttributes(array $attributes, array $data) |
||
142 | |||
143 | protected function requiredOneAttribute(array $attributes, array $data) |
||
149 | |||
150 | protected function getMessagesTo(array $data) |
||
151 | { |
||
152 | $isGroup = ArrayUtils::has($data, 'toGroup'); |
||
153 | $to = ArrayUtils::get($data, 'to', ArrayUtils::get($data, 'toGroup', [])); |
||
154 | |||
165 | } |
||
166 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.