Test Setup Failed
Push — master ( cec569...bf8bab )
by
unknown
01:08 queued 12s
created

MetatagTrait   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 442
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 41
lcom 1
cbo 2
dl 0
loc 442
rs 9.1199
c 0
b 0
f 0

37 Methods

Rating   Name   Duplication   Size   Complexity  
canonicalUrl() 0 1 ?
defaultMetaTitle() 0 1 ?
defaultMetaDescription() 0 1 ?
defaultMetaImage() 0 1 ?
A setMetaTitle() 0 5 1
A metaTitle() 0 4 1
A setMetaDescription() 0 5 1
A metaDescription() 0 4 1
A setMetaImage() 0 5 1
A metaImage() 0 4 1
A setMetaAuthor() 0 5 1
A metaAuthor() 0 4 1
A setMetaTags() 0 5 1
A metaTags() 0 4 1
A setFacebookAppId() 0 5 1
A facebookAppId() 0 4 1
A setOpengraphTitle() 0 5 1
A opengraphTitle() 0 4 1
A setOpengraphSiteName() 0 5 1
A opengraphSiteName() 0 4 1
A setOpengraphDescription() 0 5 1
A opengraphDescription() 0 4 1
A setOpengraphType() 0 5 1
A opengraphType() 0 4 1
A setOpengraphImage() 0 5 1
A opengraphImage() 0 4 1
A setOpengraphAuthor() 0 5 1
A opengraphAuthor() 0 4 1
A setOpengraphPublisher() 0 5 1
A opengraphPublisher() 0 4 1
A setTwitterCardType() 0 5 1
A twitterCardType() 0 4 1
A setTwitterCardImage() 0 5 1
A twitterCardImage() 0 4 1
A generateDefaultMetaTags() 0 13 4
B isEmptyMeta() 0 25 7
translator() 0 1 ?

How to fix   Complexity   

Complex Class

Complex classes like MetatagTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MetatagTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Charcoal\Cms;
4
5
// From 'charcoal-translator'
6
use Charcoal\Translator\Translation;
7
use Charcoal\Translator\Translator;
8
9
/**
10
 *
11
 */
12
trait MetatagTrait
13
{
14
    /**
15
     * @var Translation|string|null
16
     */
17
    protected $metaTitle;
18
19
    /**
20
     * @var Translation|string|null
21
     */
22
    protected $metaDescription;
23
24
    /**
25
     * @var Translation|string|null
26
     */
27
    protected $metaImage;
28
29
    /**
30
     * @var Translation|string|null
31
     */
32
    protected $metaAuthor;
33
34
    /**
35
     * @var Translation|string|null
36
     */
37
    protected $metaTags;
38
39
    /**
40
     * @var string $facebookAppId
41
     */
42
    protected $facebookAppId;
43
44
    /**
45
     * @var Translation|string|null
46
     */
47
    protected $opengraphTitle;
48
49
    /**
50
     * @var Translation|string|null
51
     */
52
    protected $opengraphSiteName;
53
54
    /**
55
     * @var Translation|string|null
56
     */
57
    protected $opengraphDescription;
58
59
    /**
60
     * @var string
61
     */
62
    protected $opengraphType;
63
64
    /**
65
     * @var Translation|string|null
66
     */
67
    protected $opengraphImage;
68
69
    /**
70
     * @var Translation|string|null
71
     */
72
    protected $opengraphAuthor;
73
74
    /**
75
     * @var Translation|string|null
76
     */
77
    protected $opengraphPublisher;
78
79
    /**
80
     * @var Translation|string|null
81
     */
82
    protected $opengraphTags;
83
84
    /**
85
     * @var Translation|string|null
86
     */
87
    protected $twitterCardImage;
88
89
    /**
90
     * @var Translation|string|null
91
     */
92
    protected $twitterCardType;
93
94
    /**
95
     * @return string
96
     */
97
    abstract public function canonicalUrl();
98
99
    /**
100
     * @return Translation|string|null
101
     */
102
    abstract public function defaultMetaTitle();
103
104
    /**
105
     * @return Translation|string|null
106
     */
107
    abstract public function defaultMetaDescription();
108
109
    /**
110
     * @return Translation|string|null
111
     */
112
    abstract public function defaultMetaImage();
113
114
    /**
115
     * @param  mixed $title The meta tile (localized).
116
     * @return self
117
     */
118
    public function setMetaTitle($title)
119
    {
120
        $this->metaTitle = $this->translator()->translation($title);
121
        return $this;
122
    }
123
124
    /**
125
     * @return Translation|string|null
126
     */
127
    public function metaTitle()
128
    {
129
        return $this->metaTitle;
130
    }
131
132
    /**
133
     * @param  mixed $description The meta description (localized).
134
     * @return self
135
     */
136
    public function setMetaDescription($description)
137
    {
138
        $this->metaDescription = $this->translator()->translation($description);
139
        return $this;
140
    }
141
142
    /**
143
     * @return Translation|string|null
144
     */
145
    public function metaDescription()
146
    {
147
        return $this->metaDescription;
148
    }
149
150
    /**
151
     * @param  mixed $image The meta image (localized).
152
     * @return self
153
     */
154
    public function setMetaImage($image)
155
    {
156
        $this->metaImage = $this->translator()->translation($image);
157
        return $this;
158
    }
159
160
    /**
161
     * @return Translation|string|null
162
     */
163
    public function metaImage()
164
    {
165
        return $this->metaImage;
166
    }
167
168
    /**
169
     * @param  mixed $author The meta author (localized).
170
     * @return self
171
     */
172
    public function setMetaAuthor($author)
173
    {
174
        $this->metaAuthor = $this->translator()->translation($author);
175
        return $this;
176
    }
177
178
    /**
179
     * @return Translation|string|null
180
     */
181
    public function metaAuthor()
182
    {
183
        return $this->metaAuthor;
184
    }
185
186
    /**
187
     * @param  string $tags Comma separated list of tags.
188
     * @return string
189
     */
190
    public function setMetaTags($tags)
191
    {
192
        $this->metaTags = $this->translator()->translation($tags);
193
        return $this;
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function metaTags()
200
    {
201
        return $this->metaTags;
202
    }
203
204
    /**
205
     * @param  string $appId The facebook App ID (numeric string).
206
     * @return self
207
     */
208
    public function setFacebookAppId($appId)
209
    {
210
        $this->facebookAppId = $appId;
211
        return $this;
212
    }
213
214
    /**
215
     * @return string
216
     */
217
    public function facebookAppId()
218
    {
219
        return $this->facebookAppId;
220
    }
221
222
    /**
223
     * @param  mixed $title The opengraph title (localized).
224
     * @return self
225
     */
226
    public function setOpengraphTitle($title)
227
    {
228
        $this->opengraphTitle = $this->translator()->translation($title);
229
        return $this;
230
    }
231
232
    /**
233
     * @return Translation|string|null
234
     */
235
    public function opengraphTitle()
236
    {
237
        return $this->opengraphTitle;
238
    }
239
240
    /**
241
     * @param  mixed $siteName The site name (localized).
242
     * @return self
243
     */
244
    public function setOpengraphSiteName($siteName)
245
    {
246
        $this->opengraphSiteName = $this->translator()->translation($siteName);
247
        return $this;
248
    }
249
250
    /**
251
     * @return Translation|string|null
252
     */
253
    public function opengraphSiteName()
254
    {
255
        return $this->opengraphSiteName;
256
    }
257
258
    /**
259
     * @param  mixed $description The opengraph description (localized).
260
     * @return self
261
     */
262
    public function setOpengraphDescription($description)
263
    {
264
        $this->opengraphDescription = $this->translator()->translation($description);
265
        return $this;
266
    }
267
268
    /**
269
     * @return Translation|string|null
270
     */
271
    public function opengraphDescription()
272
    {
273
        return $this->opengraphDescription;
274
    }
275
276
    /**
277
     * @param  string $type The opengraph type.
278
     * @return self
279
     */
280
    public function setOpengraphType($type)
281
    {
282
        $this->opengraphType = $type;
283
        return $this;
284
    }
285
286
    /**
287
     * @return string
288
     */
289
    public function opengraphType()
290
    {
291
        return $this->opengraphType;
292
    }
293
294
    /**
295
     * @param  mixed $image The opengraph image (localized).
296
     * @return self
297
     */
298
    public function setOpengraphImage($image)
299
    {
300
        $this->opengraphImage = $this->translator()->translation($image);
301
        return $this;
302
    }
303
304
    /**
305
     * @return Translation|string|null
306
     */
307
    public function opengraphImage()
308
    {
309
        return $this->opengraphImage;
310
    }
311
312
    /**
313
     * @param  mixed $author The opengraph author (localized).
314
     * @return self
315
     */
316
    public function setOpengraphAuthor($author)
317
    {
318
        $this->opengraphAuthor = $this->translator()->translation($author);
319
        return $this;
320
    }
321
322
    /**
323
     * @return Translation|string|null
324
     */
325
    public function opengraphAuthor()
326
    {
327
        return $this->opengraphAuthor;
328
    }
329
330
    /**
331
     * @param  mixed $publisher The opengraph publisher (localized).
332
     * @return self
333
     */
334
    public function setOpengraphPublisher($publisher)
335
    {
336
        $this->opengraphPublisher = $this->translator()->translation($publisher);
337
        return $this;
338
    }
339
340
    /**
341
     * @return Translation|string|null
342
     */
343
    public function opengraphPublisher()
344
    {
345
        return $this->opengraphPublisher;
346
    }
347
348
    /**
349
     * @param  string $type The twitter card type.
350
     * @return self
351
     */
352
    public function setTwitterCardType($type)
353
    {
354
        $this->twitterCardType = $type;
355
        return $this;
356
    }
357
358
    /**
359
     * Retrieve the object's {@link https://dev.twitter.com/cards/types card type},
360
     * for the "twitter:card" meta-property.
361
     *
362
     * @return string|null
363
     */
364
    public function twitterCardType()
365
    {
366
        return $this->twitterCardType;
367
    }
368
369
    /**
370
     * @param  mixed $image The twitter card image (localized).
371
     * @return self
372
     */
373
    public function setTwitterCardImage($image)
374
    {
375
        $this->twitterCardImage = $this->translator()->translation($image);
376
        return $this;
377
    }
378
379
    /**
380
     * Retrieve the URL to the object's social image for the "twitter:image" meta-property.
381
     *
382
     * @return string|null
383
     */
384
    public function twitterCardImage()
385
    {
386
        return $this->twitterCardImage;
387
    }
388
389
    /**
390
     * Generates the default metatags for the current object.
391
     * Prevents some problem where the defaultMetaTag method
392
     * content isn't set at the moment of setting the meta.
393
     * Should be called on preSave and preUpdate of the object.
394
     *
395
     * @return self $this.
396
     */
397
    public function generateDefaultMetaTags()
398
    {
399
        if ($this->isEmptyMeta($this->metaTitle)) {
400
            $this->setMetaTitle($this->defaultMetaTitle());
401
        }
402
        if ($this->isEmptyMeta($this->metaDescription)) {
403
            $this->setMetaDescription($this->defaultMetaDescription());
404
        }
405
        if ($this->isEmptyMeta($this->metaImage)) {
406
            $this->setMetaImage($this->defaultMetaImage());
407
        }
408
        return $this;
409
    }
410
411
    /**
412
     * Check if the meta is empty. Method exists
413
     * because at this point we don't really know
414
     * what's in the meta.
415
     * Possible param type:
416
     * - [ lang => value, lang => value ]
417
     * - Translation
418
     * - null
419
     *
420
     * @param  mixed $meta Current meta value.
421
     * @return boolean       Empty or not.
422
     */
423
    public function isEmptyMeta($meta)
424
    {
425
        if (!$meta) {
426
            return true;
427
        }
428
429
        // From back-end form which post meta_description[lang]=value
430
        // Gives [ lang => value ] as value
431
        if (is_array($meta)) {
432
            $meta = $this->translator()->translation($meta);
433
        }
434
435
        // If one value is set in whatever language,
436
        // this is NOT empty.
437
        if ($meta instanceof Translation) {
438
            $empty = true;
439
            foreach ($meta->data() as $lang => $val) {
440
                if ($val && $val != '') {
441
                    $empty = false;
442
                }
443
            }
444
            return $empty;
445
        }
446
        return true;
447
    }
448
449
    /**
450
     * @return Translator
451
     */
452
    abstract protected function translator();
453
}
454