Completed
Branch 0.3.x (efbbbe)
by Marko
02:32
created

Mixin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 88
Duplicated Lines 30.68 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
dl 27
loc 88
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ __call() 0 5 1
A component() 17 17 3
A __call() 10 10 2
B componentClosure() 0 26 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 20, 2016 - 9:12:11 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         Mixin.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\Assets;
25
26
use \AframeVR\Core\Exceptions\BadComponentCallException;
27
use \AframeVR\Interfaces\Core\Assets\MixinInterface;
28
use \AframeVR\Core\Helpers\AssetsAbstract;
29
use \Closure;
30
31
final class Mixin extends AssetsAbstract implements MixinInterface
32
{
33
34
    /**
35
     * Array of used components
36
     *
37
     * @var array $components
38
     */
39
    protected $components = array();
40
41
    /**
42
     * Load component for this entity
43
     *
44
     * @param string $component_name            
45
     * @throws \AframeVR\Core\Exceptions\BadComponentCallException
46
     * @return object|null
47
     */
48 4 View Code Duplication
    public function component(string $component_name)
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...
49
    {
50 4
        $component_name = strtolower($component_name);
51
        
52
        /* Does this mixin already have this component loaded */
53 4
        if (! array_key_exists($component_name, $this->components)) {
54 4
            $component = sprintf('\AframeVR\Core\Components\%s\%sComponent', ucfirst($component_name), ucfirst($component_name));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
55
            /* Does called component exist */
56 4
            if (class_exists($component)) {
57 3
                $this->components[$component_name] = $this->componentClosure();
58
            } else {
59 1
                throw new BadComponentCallException($component_name);
60
            }
61
        }
62
        
63 3
        return $this->components[$component_name] ?? null;
64
    }
65
66
    /**
67
     * Handle mixin components
68
     *
69
     * Since we might need to customize these to have
70
     * custom components loaded as $this->methosd aswell therefore
71
     * we have these placeholder magic methods here
72
     *
73
     * @param string $component_name            
74
     * @param array $args            
75
     */
76 4 View Code Duplication
    public function __call(string $component_name, array $args)
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...
77
    {
78 4
        if (! method_exists($this, $component_name)) {
79 4
            $this->{$component_name} = Closure::bind(function () use ($component_name) {
80 4
                return $this->component($component_name);
81 4
            }, $this, get_class());
82
        }
83
        
84 4
        return call_user_func($this->{$component_name}, $args);
85
    }
86
87
    /**
88
     * Create Closure to mock compnent to be aplied to entity using this mixin
89
     *
90
     * @return object
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use anonymous//src/Core/Assets/Mixin.php$0.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
91
     */
92
    protected function componentClosure()
93
    {
94
        return new class() {
95
96
            /**
97
             * Record all menthod / args to be called when mixin is used on some entity
98
             *
99
             * @var array
100
             */
101
            protected $component_methods = array();
102
103
            /**
104
             * Call passes all calls to no existing methods to self::methodProvider
105
             *
106
             * @param string $method            
107
             * @param array $args            
108
             * @throws InvalidComponentMethodException
109
             * @return object
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use anonymous//src/Core/Assets/Mixin.php$0.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
110
             */
111 3
            public function __call(string $method, $args)
112
            {
113 3
                $this->component_methods[$method] = $args;
114 3
                return $this;
115
            }
116
        };
117
    }
118
}
119