Completed
Push — master ( 66dbd9...44e5f7 )
by Marko
8s
created

AssetsAbstract   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 7.53 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 7
loc 93
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A id() 0 5 1
A src() 0 5 1
A domElement() 0 7 1
A setDomElementTag() 0 4 1
A appendAttributes() 0 6 2
A setAttribute() 7 7 4

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
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 28, 2016 - 3:47:35 PM
5
 * Contact      [email protected]
6
 * @copyright   2016 Marko Kungla - https://github.com/mkungla
7
 * @license     The MIT License (MIT)
8
 * 
9
 * @category       AframeVR
10
 * @package        aframe-php
11
 * 
12
 * Lang         PHP (php version >= 7)
13
 * Encoding     UTF-8
14
 * File         AssetsAbstract.php
15
 * Code format  PSR-2 and 12
16
 * @link        https://github.com/mkungla/aframe-php
17
 * @issues      https://github.com/mkungla/aframe-php/issues
18
 * ********************************************************************
19
 * Contributors:
20
 * @author Marko Kungla <[email protected]>
21
 * ********************************************************************
22
 * Comments:
23
 * @formatter:on */
24
namespace AframeVR\Core\Helpers;
25
26
use \AframeVR\Interfaces\AssetsInterface;
27
use \DOMElement;
28
29
abstract class AssetsAbstract implements AssetsInterface
30
{
31
32
    /**
33
     * DOM tag name of asset item
34
     *
35
     * @var string
36
     */
37
    protected $element_tag = 'a-asset-item';
38
    
39
    protected $attrs = array();
40
    /**
41
     * Asset constructor set asset ID
42
     *
43
     * @param string $id            
44
     */
45 6
    public function __construct(string $id)
46
    {
47 6
        $this->id($id);
48 6
    }
49
50
    /**
51
     * Set ID attribute of the asset
52
     *
53
     * {@inheritdoc}
54
     *
55
     * @param string $id            
56
     */
57 9
    public function id(string $id = 'untitled'): AssetsInterface
58
    {
59 9
        $this->attrs['id'] = $id;
60 9
        return $this;
61
    }
62
63
    /**
64
     * Set Assets src attribute
65
     *
66
     * {@inheritdoc}
67
     *
68
     * @param null|string $src            
69
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be AssetsInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
70
     */
71 3
    public function src(string $src = null): AssetsInterface
72
    {
73 3
        $this->attrs['src'] = $src;
74 3
        return $this;
75
    }
76
77
    /**
78
     * Create and add DOM element of the asset
79
     *
80
     * @param \DOMDocument $aframe_dom            
81
     * @return \DOMElement
82
     */
83 3
    public function domElement(&$aframe_dom): DOMElement
84
    {
85 3
        $a_asset = $aframe_dom->createElement($this->element_tag);
86
        /* Asset must have a id */
87 3
        $this->appendAttributes($a_asset);
88 3
        return $a_asset;
89
    }
90
91
    /**
92
     * Set Dom element name
93
     *
94
     * @param string $element_tag            
95
     * @return void
96
     */
97 6
    public function setDomElementTag(string $element_tag)
98
    {
99 6
        $this->element_tag = $element_tag;
100 6
    }
101
    
102
    /**
103
     * Append DOM attributes no set by components
104
     *
105
     * @param \DOMElement $a_entity
106
     */
107 3
    private function appendAttributes(\DOMElement &$a_entity)
108
    {
109 3
        foreach ($this->attrs as $attr => $val) {
110 3
            $this->setAttribute($a_entity, $attr, $val);
111
        }
112 3
    }
113
    
114 3 View Code Duplication
    private function setAttribute(&$a_entity, $attr, $val)
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...
115
    {
116 3
        if ($attr === 'id' && ($val === 'untitled' || is_numeric($val)))
117 1
            return;
118
    
119 3
            $a_entity->setAttribute($attr, $val);
120 3
    }
121
}
122