FieldMigration   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 218
Duplicated Lines 17.89 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 8
dl 39
loc 218
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addTransform() 0 6 1
A getTransforms() 0 4 1
A changeName() 0 6 1
A changeKey() 0 6 1
A migrate() 0 4 1
A getField() 0 4 1
A includeOptionPage() 0 6 1
A excludeOptionPage() 0 8 1
A getSubjects() 0 10 1
A applyTransform() 0 10 2
A getPosts() 0 12 1
A getTerms() 13 13 1
A getUsers() 13 13 1
A getComments() 13 13 1
A getOptions() 0 11 1
A getOptionPages() 0 5 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
namespace StoutLogic\ACF\Migrations;
4
5
class FieldMigration
6
{
7
    /**
8
     * @var Field
9
     */
10
    private $field;
11
12
    /**
13
     * @var Transform[]
14
     */
15
    private $transforms = [];
16
17
    /**
18
     * Include the option and options pages by default, this is what
19
     * ACF/ACF PRO will name an unnamed options page.
20
     * @var array
21
     */
22
    private $optionPages = ['option', 'options'];
23
24
    /**
25
     * MigrateField constructor.
26
     * @param string $fieldKey ACF Field key of field to be migrated
27
     */
28
    public function __construct($fieldKey)
29
    {
30
        $this->field = new Field($fieldKey);
31
    }
32
33
34
    /**
35
     * @param Transform $transform
36
     * @return $this
37
     */
38
    public function addTransform(Transform $transform)
39
    {
40
        $this->transforms[] = $transform;
41
42
        return $this;
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function getTransforms()
49
    {
50
        return $this->transforms;
51
    }
52
53
    /**
54
     * Change the field name.
55
     * @param stirng $newName
56
     * @return $this
57
     */
58
    public function changeName($newName)
59
    {
60
        $this->transforms[] = new ChangeNameTransform($newName);
61
62
        return $this;
63
    }
64
65
    /**
66
     * Change the field key.
67
     * @param stirng $newKey
68
     * @return $this
69
     */
70
    public function changeKey($newKey)
71
    {
72
        $this->transforms[] = new ChangeKeyTransform($newKey);
73
74
        return $this;
75
    }
76
77
    /**
78
     * Pefrom the migration.
79
     */
80
    public function migrate()
81
    {
82
        array_map([$this, 'applyTransform'], $this->transforms);
83
    }
84
85
86
    /**
87
     * @return Field
88
     */
89
    public function getField()
90
    {
91
        return $this->field;
92
    }
93
94
95
    public function includeOptionPage($optionPage)
96
    {
97
        $this->optionPages[] = $optionPage;
98
99
        return $this;
100
    }
101
102
    public function excludeOptionPage($optionPage)
103
    {
104
        $this->optionPages = array_merge(array_filter($this->optionPages, function ($o) use ($optionPage) {
105
            return $o !== $optionPage;
106
        }));
107
108
        return $this;
109
    }
110
111
    public function getSubjects()
112
    {
113
        return array_merge(
114
            $this->getPosts(),
115
            $this->getTerms(),
116
            $this->getUsers(),
117
            $this->getComments(),
118
            $this->getOptions()
119
        );
120
    }
121
122
    private function applyTransform($transform)
123
    {
124
        $transform->setField($this->getField());
125
126
        foreach ($this->getSubjects() as $subject) {
127
            $transform->transformSubject($subject);
128
        }
129
130
        $transform->transformField();
131
    }
132
133
    /**
134
     * @return array
135
     */
136
    protected function getPosts()
137
    {
138
        return array_map(
139
            function ($id) {
140
                return new Post($id);
141
            },
142
            get_posts([
143
                'fields' => 'ids',
144
                'meta_value' => $this->getField()->getKey(),
145
            ])
146
        );
147
    }
148
149
    /**
150
     * @return array
151
     */
152 View Code Duplication
    protected function getTerms()
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...
153
    {
154
        return array_map(
155
            function ($id) {
156
                return new Term($id);
157
            },
158
            get_terms([
159
                'fields' => 'ids',
160
                'hide_empty' => false,
161
                'meta_value' => $this->getField()->getKey()
162
            ])
163
        );
164
    }
165
166
    /**
167
     * @return array
168
     */
169 View Code Duplication
    protected function getUsers()
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...
170
    {
171
        return array_map(
172
            function ($id) {
173
                return new User($id);
174
            },
175
            get_users([
176
                'fields' => 'ids',
177
                'hide_empty' => false,
178
                'meta_value' => $this->getField()->getKey()
179
            ])
180
        );
181
    }
182
183
    /**
184
     * @return array
185
     */
186 View Code Duplication
    protected function getComments()
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...
187
    {
188
        return array_map(
189
            function ($id) {
190
                return new Comment($id);
191
            },
192
            get_comments([
193
                'fields' => 'ids',
194
                'hide_empty' => false,
195
                'meta_value' => $this->getField()->getKey()
196
            ])
197
        );
198
    }
199
    /**
200
     * @return array
201
     */
202
    protected function getOptions()
203
    {
204
        return array_filter(
205
            array_map(function ($id) {
206
                return new OptionPage($id);
207
            }, $this->optionPages),
208
            function (OptionPage $optionPage) {
209
                return $optionPage->hasMetaValue($this->getField()->getKey());
210
            }
211
        );
212
    }
213
214
    /**
215
     * @return array
216
     */
217
    public
218
    function getOptionPages()
219
    {
220
        return $this->optionPages;
221
    }
222
}
223