Completed
Pull Request — master (#454)
by Michael
33:16
created

EditableOption::allow_empty_values()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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
	private static $default_sort = "Sort";
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 $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...
13
14
	private static $db = 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 $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...
15
		"Name" => "Varchar(255)",
16
		"Title" => "Varchar(255)",
17
		"Default" => "Boolean",
18
        "Sort" => "Int",
19
        "Value" => "Varchar(255)",
20
	);
21
22
	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...
23
		"Parent" => "EditableMultipleOptionField",
24
	);
25
26
	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...
27
		"Versioned('Stage', 'Live')"
28
	);
29
30
	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...
31
		'Title',
32
		'Default'
33
	);
34
35
    protected static $allow_empty_values = false;
36
37
    /**
38
     * Returns whether to allow empty values or not.
39
     *
40
     * @return boolean
41
     */
42
    public static function allow_empty_values() {
43
        return (bool) self::$allow_empty_values;
44
    }
45
46
    /**
47
     * Set whether to allow empty values.
48
     *
49
     * @param boolean $allow
50
     */
51
    public static function set_allow_empty_values($allow) {
52
        self::$allow_empty_values = (bool) $allow;
53
    }
54
55
	/**
56
	 * @param Member $member
57
	 *
58
	 * @return boolean
59
	 */
60
	public function canEdit($member = null) {
61
		return $this->Parent()->canEdit($member);
62
	}
63
64
	/**
65
	 * @param Member $member
66
	 *
67
	 * @return boolean
68
	 */
69
	public function canDelete($member = null) {
70
		return $this->canEdit($member);
71
	}
72
73
	public function getEscapedTitle() {
74
		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...
75
	}
76
77
    /**
78
     * @param Member $member
79
     * @return bool
80
     */
81
	public function canView($member = null) {
82
		return $this->Parent()->canView($member);
83
	}
84
85
	/**
86
	 * Return whether a user can create an object of this type
87
	 *
88
     * @param Member $member
89
     * @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...
90
	 * @return bool
91
	 */
92 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...
93
		// Check parent page
94
        $parent = $this->getCanCreateContext(func_get_args());
95
        if($parent) {
96
            return $parent->canEdit($member);
97
        }
98
99
        // Fall back to secure admin permissions
100
        return parent::canCreate($member);
101
	}
102
103
    /**
104
     * Helper method to check the parent for this object
105
     *
106
     * @param array $args List of arguments passed to canCreate
107
     * @return DataObject Some parent dataobject to inherit permissions from
108
     */
109 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...
110
        // Inspect second parameter to canCreate for a 'Parent' context
111
        if(isset($args[1]['Parent'])) {
112
            return $args[1]['Parent'];
113
        }
114
        // Hack in currently edited page if context is missing
115
        if(Controller::has_curr() && Controller::curr() instanceof CMSMain) {
116
            return Controller::curr()->currentPage();
117
        }
118
119
        // No page being edited
120
        return null;
121
    }
122
123
    /**
124
     * @param Member $member
125
     * @return bool
126
     */
127
    public function canPublish($member = null) {
128
        return $this->canEdit($member);
129
    }
130
131
    /**
132
     * @param Member $member
133
     * @return bool
134
     */
135
    public function canUnpublish($member = null) {
136
        return $this->canDelete($member);
137
    }
138
139
    /**
140
     * Fetches a value for $this->Value. If empty values are not allowed,
141
     * then this will return the title in the case of an empty value.
142
     *
143
     * @return string
144
     */
145
    public function getValue()
146
    {
147
        $value = $this->getField('Value');
148
        if(empty($value) && !self::allow_empty_values()) {
149
            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...
150
        }
151
        return $value;
152
    }
153
}
154