Component_number   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 13.11 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 8
loc 61
rs 10
c 2
b 0
f 0
wmc 10
lcom 0
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A default_model() 0 18 1
A filter() 0 4 1
B validation() 8 17 6
A required_arguments() 0 4 1
A get_template_path() 0 4 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
3
namespace Amarkal\UI;
4
5
/**
6
 * Implements a number UI component.
7
 */
8
class Component_number
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
    public $component_type = 'number';
16
    
17
    public function default_model() 
18
    {
19
        return array(
20
            'name'          => '',
21
            'id'            => '',
22
            'disabled'      => false,
23
            'size'          => null,
24
            'min'           => null,
25
            'max'           => null,
26
            'step'          => null,
27
            'required'      => false,
28
            'readonly'      => false,
29
            'default'       => null,
30
            'filter'        => array( $this, 'filter' ),
31
            'validation'    => array( $this, 'validation' ),
32
            'show'          => null
33
        );
34
    }
35
    
36
    public function filter($v)
37
    {
38
        return floatval($v);
39
    }
40
    
41
    public function validation($v,&$e)
42
    {
43
        $max = $this->max;
0 ignored issues
show
Documentation introduced by
The property max does not exist on object<Amarkal\UI\Component_number>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
44
        $min = $this->min;
0 ignored issues
show
Documentation introduced by
The property min does not exist on object<Amarkal\UI\Component_number>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
45
        
46 View Code Duplication
        if(null !== $max && $v > $max)
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...
47
        {
48
            $e = sprintf( __("Value must be less than %d",'amarkal'), $max);
49
        }
50
51 View Code Duplication
        if(null !== $min && $v < $min) 
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...
52
        {
53
            $e = sprintf( __("Value must be greater than %d",'amarkal'), $min);
54
        }
55
56
        return $e ? false : true;
57
    }
58
    
59
    public function required_arguments()
60
    {
61
        return array('name');
62
    }
63
    
64
    public function get_template_path() 
65
    {
66
        return __DIR__.'/template.phtml';
67
    }
68
}