Completed
Push — master ( ea324c...2e8bb7 )
by Andrey
01:33
created

Html   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 18.64 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 22
loc 118
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A audio() 0 8 1
A video() 0 10 1
A getMainOptions() 11 11 3
A getSourceOptions() 11 11 3
A getTrackOptions() 0 10 3
A getTag() 0 12 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 Itstructure\MFUploader\helpers;
4
5
use Itstructure\MFUploader\Module;
6
use yii\helpers\{ArrayHelper, Html as BaseHtml};
7
8
/**
9
 * HTML helper.
10
 *
11
 * @package Itstructure\MFUploader\helpers
12
 *
13
 * @author Andrey Girnik <[email protected]>
14
 */
15
class Html extends BaseHtml
16
{
17
    /**
18
     * Render html 5 audio tag structure.
19
     *
20
     * @param string $src
21
     * @param array $options
22
     *
23
     * @return string
24
     */
25
    public static function audio(string $src, array $options = []): string
26
    {
27
        return self::getTag('audio',
28
            self::getMainOptions($options),
29
            self::getSourceOptions($src, $options),
30
            self::getTrackOptions($options)
31
        );
32
    }
33
34
    /**
35
     * Render html 5 video tag structure.
36
     *
37
     * @param string $src
38
     * @param array $options
39
     *
40
     * @return string
41
     */
42
    public static function video(string $src, array $options = []): string
43
    {
44
        return self::getTag('video',
45
            ArrayHelper::merge([
46
                'height' => Module::ORIGINAL_PREVIEW_HEIGHT
47
            ], self::getMainOptions($options)),
48
            self::getSourceOptions($src, $options),
49
            self::getTrackOptions($options)
50
        );
51
    }
52
53
    /**
54
     * Get main options of the mediafile.
55
     *
56
     * @param array $options
57
     *
58
     * @return array
59
     */
60 View Code Duplication
    private static function getMainOptions(array $options = []): array
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...
61
    {
62
        $mainOptions = [
63
            'controls' => 'controls',
64
            'width' => Module::ORIGINAL_PREVIEW_WIDTH,
65
        ];
66
        if (isset($options['main']) && is_array($options['main'])) {
67
            $mainOptions = ArrayHelper::merge($mainOptions, $options['main']);
68
        }
69
        return $mainOptions;
70
    }
71
72
    /**
73
     * Get source options of the mediafile.
74
     *
75
     * @param string $src
76
     * @param array $options
77
     *
78
     * @return array
79
     */
80 View Code Duplication
    private static function getSourceOptions(string $src, array $options = []): array
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...
81
    {
82
        $sourceOptions = [
83
            'src' => $src,
84
            'preload' => 'auto'
85
        ];
86
        if (isset($options['source']) && is_array($options['source'])) {
87
            $sourceOptions = ArrayHelper::merge($sourceOptions, $options['source']);
88
        }
89
        return $sourceOptions;
90
    }
91
92
    /**
93
     * Get track options of the mediafile.
94
     *
95
     * @param array $options
96
     *
97
     * @return array
98
     */
99
    private static function getTrackOptions(array $options = []): array
100
    {
101
        $trackOptions = [
102
            'kind' => 'subtitles'
103
        ];
104
        if (isset($options['track']) && is_array($options['track'])) {
105
            $trackOptions = ArrayHelper::merge($trackOptions, $options['track']);
106
        }
107
        return $trackOptions;
108
    }
109
110
    /**
111
     * Get tag of the mediafile.
112
     *
113
     * @param string $tagName
114
     * @param array $mainOptions
115
     * @param array $sourceOptions
116
     * @param array $trackOptions
117
     *
118
     * @return string
119
     */
120
    private static function getTag(
121
        string $tagName,
122
        array $mainOptions,
123
        array $sourceOptions,
124
        array $trackOptions): string {
125
126
        return static::tag(
127
            $tagName,
128
            static::tag('source', '', $sourceOptions) . static::tag('track', '', $trackOptions),
129
            $mainOptions
130
        );
131
    }
132
}
133