Completed
Push — master ( a4d7e3...20918f )
by Askupa
01:31
created

Component_composite   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 207
Duplicated Lines 10.14 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 21
loc 207
rs 10
c 0
b 0
f 0
wmc 28
lcom 1
cbo 2

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __set() 0 14 3
A set_value() 0 8 2
A set_name() 0 7 2
A default_model() 0 13 1
A required_arguments() 0 4 1
A get_template_path() 0 4 1
A parse_template() 0 7 1
A get_name() 0 8 2
A filter() 9 14 4
B validation() 12 16 5
A on_created() 0 7 2
A create_component() 0 16 2
A get_component() 0 8 2

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
3
namespace Amarkal\UI;
4
5
/**
6
 * Implements a Composite UI component.
7
 */
8
class Component_composite
9
extends AbstractComponent
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
10
implements ValueComponentInterface, 
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
The first item in a multi-line implements list must be on the line following the implements keyword
Loading history...
11
           DisableableComponentInterface,
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 11 found
Loading history...
12
           FilterableComponentInterface,
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 11 found
Loading history...
13
           ValidatableComponentInterface
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 11 found
Loading history...
14
{
15
    /**
16
     * The list of child components.
17
     * 
18
     * @var UI\AbstractComponent[] 
19
     */
20
    private $components;
21
    
22
    public $component_type = 'composite';
23
    
24
    /**
25
     * The __set magic method is overridden here to apply value & name changes to 
26
     * child components.
27
     */
28
    public function __set( $name, $value )
29
    {
30
        parent::__set($name, $value);
31
        
32
        if( 'value' === $name )
33
        {
34
            $this->set_value($value);
35
        }
36
37
        if( 'name' === $name )
38
        {
39
            $this->set_name($value);
40
        }
41
    }
42
    
43
    /**
44
     * Apply the value to each of the child components.
45
     * 
46
     * @param array $value
47
     * @return void
48
     */
49
    public function set_value( array $value )
50
    {
51
        foreach($value as $n => $v)
52
        {
53
            $component = $this->get_component($n);
54
            $component->value = $v;
55
        }
56
    }
57
58
    /**
59
     * Apply the new name to each of the child components.
60
     *
61
     * @param string $name
62
     * @return void
63
     */
64
    public function set_name( $name )
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
        foreach($this->components as $c)
67
        {
68
            $c->name_template = str_replace('{{parent_name}}', $this->get_name(), $c->composite_name_template);
69
        }
70
    }
71
    
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function default_model() 
76
    {
77
        return array(
78
            'name'          => '',
79
            'parent_name'   => '',
80
            'id'            => '',
81
            'disabled'      => false,
82
            'template'      => null,
83
            'components'    => array(),
84
            'filter'        => array($this, 'filter'),
85
            'validation'    => array($this, 'validation'),
86
        );
87
    }
88
    
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function required_arguments()
93
    {
94
        return array('name','components','template');
95
    }
96
    
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function get_template_path() 
101
    {
102
        return __DIR__.'/template.phtml';
103
    }
104
    
105
    /**
106
     * Parse the template by converting the name tokens into UI components.
107
     * 
108
     * @return string The parsed template
109
     */
110
    public function parse_template()
111
    {
112
        return preg_replace_callback('/\{\{([a-zA-Z\d-_]+)\}\}/', function($a){
113
            $component = $this->get_component($a[1]);
114
            return $component->render();
115
        }, $this->model['template']);
116
    }
117
    
118
    /**
119
     * If this is the root composite component, this will return the component's 
120
     * name. If this is a child composite component, this will return the
121
     * component's name as a key in the parent's array, i.e 'parent_name[child_name]'
122
     * 
123
     * @return string
124
     */
125
    public function get_name()
126
    {
127
        if('' !== $this->parent_name)
0 ignored issues
show
Documentation introduced by
The property parent_name does not exist on object<Amarkal\UI\Component_composite>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
128
        {
129
            return "{$this->parent_name}[{$this->name}]";
0 ignored issues
show
Documentation introduced by
The property parent_name does not exist on object<Amarkal\UI\Component_composite>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property name does not exist on object<Amarkal\UI\Component_composite>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
130
        }
131
        return $this->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Amarkal\UI\Component_composite>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
132
    }
133
134
    public function filter($v)
135
    {
136 View Code Duplication
        foreach($this->components as $component)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
137
        {
138
            if($component instanceof FilterableComponentInterface &&
139
               \is_callable($component->filter))
140
            {
141
                $n = $component->name;
142
                $v[$n] = $component->filter($v[$n]);
143
            }
144
        }
145
        
146
        return $v;
147
    }
148
149
    public function validation($v,&$e)
150
    {
151 View Code Duplication
        foreach($this->components as $component)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
152
        {
153
            if($component instanceof ValidatableComponentInterface &&
154
               \is_callable($component->validation))
155
            {
156
                $n = $component->name;
157
                if(!$component->validation($v[$n],$e))
158
                {
159
                    return false;
160
                }
161
            }
162
        }
163
        return true;
164
    }
165
    
166
    /**
167
     * Instantiate child UI components when created.
168
     */
169
    protected function on_created()
170
    {
171
        foreach( $this->model['components'] as $args )
172
        {
173
            $this->components[$args['name']] = $this->create_component($args);
174
        }
175
    }
176
    
177
    /**
178
     * 
179
     * @param type $args
180
     * @return type
181
     */
182
    private function create_component( $args )
183
    {
184
        $type = $args['type'];
185
        
186
        if('composite' === $type)
187
        {
188
            $args['parent_name'] = $this->get_name();
189
        }
190
        
191
        $c = \Amarkal\UI\ComponentFactory::create( $type, $args );
192
        
193
        // Apply the composite name template
194
        $c->name_template = str_replace('{{parent_name}}', $this->get_name(), $c->composite_name_template);
195
        
196
        return $c;
197
    }
198
    
199
    /**
200
     * Get a child component by name.
201
     * 
202
     * @param string $name
203
     * @return UI\AbstractComponent
204
     * @throws \RuntimeException If there's no child component corresponding to the given name
205
     */
206
    private function get_component( $name )
207
    {
208
        if(!array_key_exists($name, $this->components))
209
        {
210
            throw new \RuntimeException("Composite sub-component not found with name $name");
211
        }
212
        return $this->components[$name];
213
    }
214
}