Completed
Push — master ( be4ea7...74ddb0 )
by Marko
8s
created

EntityChildrenFactory::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jul 5, 2016 - 3:05:38 AM
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         EntityChildrenFactory.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\Core\Entity;
27
use \AframeVR\Core\Exceptions\BadPrimitiveCallException;
28
29
/**
30
 * @method \AframeVR\Core\Entity entity(string $id)
31
 * @method \AframeVR\Extras\Primitives\Box box(string $id)
32
 * @method \AframeVR\Extras\Primitives\Camera camera(string $id)
33
 * @method \AframeVR\Extras\Primitives\Circle circle(string $id)
34
 * @method \AframeVR\Extras\Primitives\Colladamodel colladamodel(string $id)
35
 * @method \AframeVR\Extras\Primitives\Cone cone(string $id)
36
 * @method \AframeVR\Extras\Primitives\Cursor cursor(string $id)
37
 * @method \AframeVR\Extras\Primitives\Curvedimage curvedimage(string $id)
38
 * @method \AframeVR\Extras\Primitives\Cylinder cylinder(string $id)
39
 * @method \AframeVR\Extras\Primitives\Image image(string $id)
40
 * @method \AframeVR\Extras\Primitives\Light light(string $id)
41
 * @method \AframeVR\Extras\Primitives\Objmodel objmodel(string $id)
42
 * @method \AframeVR\Extras\Primitives\Plane plane(string $id)
43
 * @method \AframeVR\Extras\Primitives\Ring ring(string $id)
44
 * @method \AframeVR\Extras\Primitives\Sky sky(string $id)
45
 * @method \AframeVR\Extras\Primitives\Sphere sphere(string $id)
46
 * @method \AframeVR\Extras\Primitives\Torus torus(string $id)
47
 * @method \AframeVR\Extras\Primitives\Video video(string $id)
48
 * @method \AframeVR\Extras\Primitives\Videosphere videosphere(string $id)
49
 */
50
51
class EntityChildrenFactory
52
{
53
    /**
54
     * Child entities
55
     *
56
     * @var array
57
     */
58
    private $childrens = array();
59
60
    /**
61
     * Get entity
62
     * 
63
     * @param string $a_type
64
     * @param string $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
65
     * @return AframeVR\Interfaces\EntityInterface
66
     */
67 77
    public function getEntity(string $a_type, string $entity_id)
68
    {
69 77
        $id = $id ?? 0;
0 ignored issues
show
Bug introduced by
The variable $id seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70 77
        return $this->childrens[$a_type][$entity_id] ?? $this->addEntity($a_type, $entity_id);
71
    }
72
    
73
    /**
74
     * Get all children for calling parent
75
     * @return array
76
     */
77 19
    public function getChildren()
78
    {
79 19
        return iterator_to_array($this->getChildrenRAW($this->childrens), false);
80
    }
81
    
82
    /**
83
     * Get entity
84
     * 
85
     * @param string $method
86
     * @param array $args
0 ignored issues
show
Bug introduced by
There is no parameter named $args. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
87
     * @return AframeVR\Interfaces\EntityInterface
88
     */
89 6
    public function __call(string $method, array $entity_id)
90
    {
91 6
        $entity_id = $entity_id[0] ?? 0;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $entity_id. This often makes code more readable.
Loading history...
92 6
        return $this->getEntity($method, $entity_id);
93
    }
94
    
95
    /**
96
     * Register new entity
97
     * 
98
     * Generally you should not call this method directly but if you want to extend
99
     * EntityChildrenFactory then you can still access it
100
     *
101
     * @param string $a_type            
102
     * @param string $id            
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
103
     * @throws BadShaderCallException
104
     * @return AframeVR\Interfaces\EntityInterface
105
     */
106 77
    protected function addEntity(string $a_type, string $entity_id)
107
    {
108 77
        $primitive = sprintf('\AframeVR\Extras\Primitives\%s', ucfirst($a_type));
109
        
110 77
        if($a_type === 'entity') {
111 55
            return $this->childrens[$a_type][$entity_id] = new Entity($entity_id);
112 27
        } elseif (class_exists($primitive)) {
113 27
            return $this->childrens[$a_type][$entity_id] = new $primitive($entity_id);
114
        } else {
115 1
            throw new BadPrimitiveCallException($a_type);
116
        }
117
    }
118
    
119
    /**
120
     * Generate array of children
121
     * 
122
     * @param array $array
123
     */
124 19
    protected function getChildrenRAW(array $array) {
125 19
        foreach ($array as $value) {
126 11
            if (is_array($value)) {
127 11
                yield from $this->getChildrenRAW($value);
128
            } else {
129 11
                yield $value;
130
            }
131
        }
132
    }
133
}