ImageCaptionRenderer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 33.8 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 1
dl 24
loc 71
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 20 2
A getAttributionContents() 0 14 4
A getTitle() 8 8 2
A getAuthor() 8 8 2
A getLicense() 8 8 2

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
 * Lichtenwallner  (https://lichtenwallner.at)
4
 *
5
 * @see https://github.com/jolicht/markdown-cms for the canonical source repository
6
 * @license https://github.com/jolicht/markdown-cms/blob/master/LICENSE MIT
7
 * @copyright Copyright (c) Johannes Lichtenwallner
8
 */
9
declare(strict_types = 1);
10
namespace Jolicht\MarkdownCms\Markdown\Renderer;
11
12
use League\CommonMark\HtmlElement;
13
14
class ImageCaptionRenderer
15
{
16
    /**
17
     * Render figcaption
18
     *
19
     * @param array $config
20
     * @return HtmlElement
21
     */
22
    public function __invoke(array $config) : HtmlElement
23
    {
24
        if (isset($config['attribution'])) {
25
            $attribution = new HtmlElement(
26
                'span',
27
                ['class' => 'attribution'],
28
                $this->getAttributionContents($config['attribution']));
29
            $contents = [
30
                $config['caption'],
31
                $attribution
32
            ];
33
        } else {
34
            $contents = $config['caption'];
35
        }
36
37
38
        $figcaption = new HtmlElement('figcaption', [], $contents);
39
40
        return $figcaption;
41
    }
42
43
    private function getAttributionContents(array $params)
44
    {
45
        $contents = [];
46
        if (isset($params['title'])) {
47
            $contents[] = $this->getTitle($params);
48
        }
49
        if (isset($params['author'])) {
50
            $contents[] = $this->getAuthor($params);
51
        }
52
        if (isset($params['license'])) {
53
            $contents[] = $this->getLicense($params);
54
        }
55
        return $contents;
56
    }
57
58 View Code Duplication
    private function getTitle(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...
59
    {
60
        if (isset($params['url'])) {
61
            return new HtmlElement('a', ['class' => 'source', 'href' => $params['url']], $params['title']);
62
        } else {
63
            return new HtmlElement('span', ['class' => 'source'], $params['title']);
64
        }
65
    }
66
67 View Code Duplication
    private function getAuthor(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...
68
    {
69
        if (isset($params['author_url'])) {
70
            return new HtmlElement('a', ['class' => 'author', 'href' => $params['author_url']], $params['author']);
71
        } else {
72
            return new HtmlElement('span', ['class' => 'author'], $params['author']);
73
        }
74
    }
75
76 View Code Duplication
    private function getLicense(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...
77
    {
78
        if (isset($params['license_url'])) {
79
            return new HtmlElement('a', ['class' => 'license', 'href' => $params['license_url']], $params['license']);
80
        } else {
81
            return new HtmlElement('span', ['class' => 'license'], $params['license']);
82
        }
83
    }
84
}