Ajde_Embed   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 101
Duplicated Lines 13.86 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 14
loc 101
rs 10
c 0
b 0
f 0
wmc 18
lcom 2
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
C fromCode() 14 25 8
A __construct() 0 4 1
A getProvider() 0 9 2
A setWidth() 0 4 1
A setHeight() 0 4 1
A _setWidth() 0 6 1
A _setHeight() 0 6 1
A stripTags() 0 4 1
A getCode() 0 4 1
A getThumbnail() 0 3 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
class Ajde_Embed extends Ajde_Object_Standard
4
{
5
    protected $_code;
6
    protected $_parser;
7
8
    protected $_width = 650;
9
    protected $_height = 471;
10
11
    protected $_allowedTags = '<iframe><embed><object>';
12
13
    private static $_detect = [
14
        'soundcloud' => 'soundcloud.com',
15
        'vimeo'      => 'vimeo.com',
16
        'youtube'    => ['youtube.com', 'youtu.be'],
17
        'mixcloud'   => 'mixcloud.com',
18
    ];
19
20
    /**
21
     * @param type $code
22
     *
23
     * @return Ajde_Embed
24
     */
25
    public static function fromCode($code)
26
    {
27
        $embedClass = 'Ajde_Embed';
28
        foreach (self::$_detect as $provider => $test) {
29
            if (is_array($test)) {
30
                foreach ($test as $testPart) {
31 View Code Duplication
                    if (substr_count($code, $testPart) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
32
                        $providerClass = 'Ajde_Embed_'.ucfirst($provider);
33
                        if (class_exists($providerClass)) {
34
                            $embedClass = $providerClass;
35
                        }
36
                    }
37
                }
38 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
39
                if (substr_count($code, $test) > 0) {
40
                    $providerClass = 'Ajde_Embed_'.ucfirst($provider);
41
                    if (class_exists($providerClass)) {
42
                        $embedClass = $providerClass;
43
                    }
44
                }
45
            }
46
        }
47
48
        return new $embedClass($code);
49
    }
50
51
    public function __construct($code)
52
    {
53
        $this->_code = $code;
54
    }
55
56
    public function getProvider()
57
    {
58
        $name = str_replace('Ajde_Embed', '', get_class($this));
59
        if (!empty($name)) {
60
            return str_replace('_', '', $name);
61
        } else {
62
            return false;
63
        }
64
    }
65
66
    public function setWidth($width)
67
    {
68
        $this->_width = $width;
69
    }
70
71
    public function setHeight($height)
72
    {
73
        $this->_height = $height;
74
    }
75
76
    protected function _setWidth()
77
    {
78
        $ptn = "/width=(\'|\")([0-9]+)(\'|\")/";
79
        $rpltxt = "width='".$this->_width."'";
80
        $this->_code = preg_replace($ptn, $rpltxt, $this->_code);
81
    }
82
83
    protected function _setHeight()
84
    {
85
        $ptn = "/height=(\'|\")([0-9]+)(\'|\")/";
86
        $rpltxt = "height='".$this->_height."'";
87
        $this->_code = preg_replace($ptn, $rpltxt, $this->_code);
88
    }
89
90
    public function stripTags()
91
    {
92
        $this->_code = strip_tags($this->_code, $this->_allowedTags);
93
    }
94
95
    public function getCode()
96
    {
97
        return $this->_code;
98
    }
99
100
    public function getThumbnail()
101
    {
102
    }
103
}
104