Completed
Push — master ( 67ead9...61a703 )
by Daniel
12s
created

EditableOption   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 168
Duplicated Lines 13.69 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 31.58%

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 5
dl 23
loc 168
ccs 12
cts 38
cp 0.3158
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A allow_empty_values() 0 4 1
A set_allow_empty_values() 0 4 1
A canEdit() 0 4 1
A canDelete() 0 4 1
A getEscapedTitle() 0 4 1
A canView() 0 4 1
A canCreate() 10 11 2
A getCanCreateContext() 13 14 4
A canPublish() 0 4 1
A canUnpublish() 0 4 1
A getValue() 0 8 3
A onBeforeWrite() 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
/**
4
 * Base Class for EditableOption Fields such as the ones used in
5
 * dropdown fields and in radio check box groups
6
 *
7
 * @method EditableMultipleOptionField Parent()
8
 * @package userforms
9
 */
10
class EditableOption extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
{
12
13
	private static $default_sort = "Sort";
0 ignored issues
show
Unused Code introduced by
The property $default_sort is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
14
15
	private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
16
		"Name" => "Varchar(255)",
17
		"Title" => "Varchar(255)",
18
		"Default" => "Boolean",
19
        "Sort" => "Int",
20
        "Value" => "Varchar(255)",
21
	);
22
23
	private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
		"Parent" => "EditableMultipleOptionField",
25
	);
26
27
	private static $extensions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $extensions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
		"Versioned('Stage', 'Live')"
29
	);
30
31
	private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $summary_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
32
		'Title',
33
		'Default'
34
	);
35
36
    protected static $allow_empty_values = false;
37
38
	/**
39
     * Returns whether to allow empty values or not.
40
     *
41
     * @return boolean
42
     */
43 2
    public static function allow_empty_values()
44
    {
45 2
        return (bool) self::$allow_empty_values;
46
    }
47
48
    /**
49
     * Set whether to allow empty values.
50
     *
51
     * @param boolean $allow
52
     */
53
    public static function set_allow_empty_values($allow)
54
    {
55
        self::$allow_empty_values = (bool) $allow;
56
    }
57
58
    /**
59
	 * @param Member $member
60
	 *
61
	 * @return boolean
62
	 */
63
    public function canEdit($member = null)
64
    {
65
		return $this->Parent()->canEdit($member);
66
	}
67
68
	/**
69
	 * @param Member $member
70
	 *
71
	 * @return boolean
72
	 */
73
    public function canDelete($member = null)
74
    {
75
		return $this->canEdit($member);
76
	}
77
78
	/**
79
	 * @deprecated 5.0 Use "$Title.XML" in templates instead
80
	 * @return string
81
	 */
82
    public function getEscapedTitle()
83
    {
84
		return Convert::raw2att($this->Title);
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<EditableOption>. 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...
85
	}
86
87
	/**
88
     * @param Member $member
89
     * @return bool
90
     */
91
    public function canView($member = null)
92
    {
93
		return $this->Parent()->canView($member);
94
	}
95
96
	/**
97
	 * Return whether a user can create an object of this type
98
	 *
99
     * @param Member $member
100
     * @param array $context Virtual parameter to allow context to be passed in to check
0 ignored issues
show
Bug introduced by
There is no parameter named $context. 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...
101
	 * @return bool
102
	 */
103 View Code Duplication
    public function canCreate($member = null)
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...
104
    {
105
		// Check parent page
106
        $parent = $this->getCanCreateContext(func_get_args());
107
        if($parent) {
108
            return $parent->canEdit($member);
109
        }
110
111
        // Fall back to secure admin permissions
112
        return parent::canCreate($member);
113
	}
114
115
    /**
116
     * Helper method to check the parent for this object
117
     *
118
     * @param array $args List of arguments passed to canCreate
119
     * @return DataObject Some parent dataobject to inherit permissions from
120
     */
121 View Code Duplication
    protected function getCanCreateContext($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...
122
    {
123
        // Inspect second parameter to canCreate for a 'Parent' context
124
        if(isset($args[1]['Parent'])) {
125
            return $args[1]['Parent'];
126
        }
127
        // Hack in currently edited page if context is missing
128
        if(Controller::has_curr() && Controller::curr() instanceof CMSMain) {
129
            return Controller::curr()->currentPage();
130
        }
131
132
        // No page being edited
133
        return null;
134
    }
135
136
    /**
137
     * @param Member $member
138
     * @return bool
139
     */
140
    public function canPublish($member = null)
141
    {
142
        return $this->canEdit($member);
143
    }
144
145
    /**
146
     * @param Member $member
147
     * @return bool
148
     */
149
    public function canUnpublish($member = null)
150
    {
151
        return $this->canDelete($member);
152
    }
153
154
    /**
155
     * Fetches a value for $this->Value. If empty values are not allowed,
156
     * then this will return the title in the case of an empty value.
157
     *
158
     * @return string
159
     */
160 17
    public function getValue()
161
    {
162 17
        $value = $this->getField('Value');
163 17
        if (empty($value) && !self::allow_empty_values()) {
164 1
            return $this->Title;
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<EditableOption>. 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...
165
        }
166 17
        return $value;
167
    }
168
169 39
    protected function onBeforeWrite()
170
    {
171 39
        if (!$this->Sort) {
0 ignored issues
show
Documentation introduced by
The property Sort does not exist on object<EditableOption>. 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...
172 39
            $this->Sort = EditableOption::get()->max('Sort') + 1;
0 ignored issues
show
Documentation introduced by
The property Sort does not exist on object<EditableOption>. 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...
173
        }
174
175 39
        parent::onBeforeWrite();
176 39
    }
177
}
178