Completed
Push — master ( ce557b...46a40a )
by Marko
02:10
created

Rotation   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 96.04 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 97
loc 101
ccs 15
cts 20
cp 0.75
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getScripts() 4 4 1
A __construct() 4 4 1
A hasDOMAttributes() 4 4 1
A removeDefaultDOMAttributes() 8 8 4
A getDOMAttributes() 4 4 1
A update() 3 6 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
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 21, 2016 - 12:10:50 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         Rotation.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\Components;
25
26
use \AframeVR\Interfaces\ComponentInterface;
27
use \DOMAttr;
28
29
/**
30
 * The rotation component defines the orientation of an entity.
31
 *
32
 * It takes the roll (x), pitch (y), and yaw (z) as three space-delimited numbers indicating degrees of rotation.
33
 *
34
 * All entities inherently have the rotation component.
35
 */
36 View Code Duplication
class Rotation implements ComponentInterface
0 ignored issues
show
Duplication introduced by
This class 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...
37
{
38
39
    /**
40
     * Roll, rotation about the X-axis.
41
     *
42
     * @var int $x
43
     */
44
    protected $x;
45
46
    /**
47
     * Pitch, rotation about the Y-axis.
48
     *
49
     * @var int $y
50
     */
51
    protected $y;
52
53
    /**
54
     * Yaw, rotation about the Z-axis.
55
     *
56
     * @var int $z
57
     */
58
    protected $z;
59
60
    /**
61
     * Constructor
62
     * 
63
     * @param float $x
0 ignored issues
show
Documentation introduced by
Should the type for parameter $x not be integer|double?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
64
     * @param float $y
0 ignored issues
show
Documentation introduced by
Should the type for parameter $y not be integer|double?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
65
     * @param float $z
0 ignored issues
show
Documentation introduced by
Should the type for parameter $z not be integer|double?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
66
     */
67 21
    public function __construct(float $x = 0, float $y = 0, float $z = 0)
68
    {
69 21
        $this->update($x, $y, $z);
70 21
    }
71
72
    /**
73
     * Get Component scripts
74
     *
75
     * {@inheritdoc}
76
     *
77
     * @return array
78
     */
79
    public function getScripts(): array
80
    {
81
        return array();
82
    }
83
84
    /**
85
     * Does component have DOM Atributes
86
     *
87
     * {@inheritdoc}
88
     *
89
     * @return bool
90
     */
91 1
    public function hasDOMAttributes(): bool
92
    {
93 1
        return ! empty(get_object_vars($this));
94
    }
95
96
    /**
97
     * Remove default DOMElement Attributes which are not required
98
     *
99
     * @return void
100
     */
101 1
    public function removeDefaultDOMAttributes()
102
    {
103 1
        if ($this->x === 0 && $this->y === 0 && $this->z === 0) {
104
            unset($this->x);
105
            unset($this->y);
106
            unset($this->z);
107
        }
108 1
    }
109
110
    /**
111
     * Get DOMAttr for the entity
112
     *
113
     * @return DOMAttr
114
     */
115 1
    public function getDOMAttributes(): DOMAttr
116
    {
117 1
        return new \DOMAttr('rotation', sprintf('%s %s %s', $this->x, $this->y, $this->z));
118
    }
119
120
    /**
121
     * Update rotation
122
     * 
123
     * A-Frame uses a right-handed coordinate system. When aligning our right hand’s thumb with a positive axis, 
124
     * our hand will curl in the positive direction of rotation.
125
     * 
126
     * @param float $x
0 ignored issues
show
Documentation introduced by
Should the type for parameter $x not be integer|double?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
127
     * @param float $y
0 ignored issues
show
Documentation introduced by
Should the type for parameter $y not be integer|double?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
128
     * @param float $z
0 ignored issues
show
Documentation introduced by
Should the type for parameter $z not be integer|double?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
129
     */
130 21
    public function update(float $x = 0, float $y = 0, float $z = 0)
131
    {
132 21
        $this->x = $x ?? 0;
0 ignored issues
show
Documentation Bug introduced by
It seems like $x ?? 0 can also be of type double. However, the property $x is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
133 21
        $this->y = $y ?? 0;
0 ignored issues
show
Documentation Bug introduced by
It seems like $y ?? 0 can also be of type double. However, the property $y is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
134 21
        $this->z = $z ?? 0;
0 ignored issues
show
Documentation Bug introduced by
It seems like $z ?? 0 can also be of type double. However, the property $z is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
135 21
    }
136
}
137