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

Position::getDOMAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 20, 2016 - 11:21:15 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         Position.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 position component defines where an entity is placed in the scene’s world space.
31
 * It takes a coordinate value as three space-delimited numbers.
32
 *
33
 * All entities inherently have the position component.
34
 */
35 View Code Duplication
class Position 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...
36
{
37
38
    /**
39
     * Negative X axis extends left.
40
     *
41
     * Positive X Axis extends right.
42
     *
43
     * @var int $x
44
     */
45
    protected $x;
46
47
    /**
48
     * Negative Y axis extends up.
49
     * Positive Y Axis extends down.
50
     *
51
     * @var int $y
52
     */
53
    protected $y;
54
55
    /**
56
     * Negative Z axis extends in.
57
     * Positive Z Axis extends out.
58
     *
59
     * @var int $z
60
     */
61
    protected $z;
62
63
    /**
64
     * Constructor
65
     * 
66
     * @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...
67
     * @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...
68
     * @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...
69
     */
70 21
    public function __construct(float $x = 0, float $y = 0, float $z = 0)
71
    {
72 21
        $this->update($x, $y, $z);
73 21
    }
74
75
    /**
76
     * Get Component scripts
77
     *
78
     * {@inheritdoc}
79
     *
80
     * @return array
81
     */
82
    public function getScripts(): array
83
    {
84
        return array();
85
    }
86
87
    /**
88
     * Does component have DOM Atributes
89
     *
90
     * {@inheritdoc}
91
     *
92
     * @return bool
93
     */
94 1
    public function hasDOMAttributes(): bool
95
    {
96 1
        return ! empty(get_object_vars($this));
97
    }
98
99
    /**
100
     * Remove default DOMElement Attributes which are not required
101
     *
102
     * @return void
103
     */
104 1
    public function removeDefaultDOMAttributes()
105
    {
106 1
        if ($this->x === 0 && $this->y === 0 && $this->z === 0) {
107
            unset($this->x);
108
            unset($this->y);
109
            unset($this->z);
110
        }
111 1
    }
112
113
    /**
114
     * Get DOMAttr for the entity
115
     *
116
     * @return DOMAttr
117
     */
118 1
    public function getDOMAttributes(): DOMAttr
119
    {
120 1
        return new \DOMAttr('position', sprintf('%s %s %s', $this->x, $this->y, $this->z));
121
    }
122
123
    /**
124
     * Update coordinates
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
    }
136
}