Completed
Pull Request — master (#441)
by Damian
33:28
created

EditableCustomRule::canCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/**
4
 * A custom rule for showing / hiding an EditableFormField
5
 * based the value of another EditableFormField.
6
 *
7
 * @method EditableFormField Parent()
8
 * @package userforms
9
 */
10
class EditableCustomRule 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 $condition_options = array(
0 ignored issues
show
Unused Code introduced by
The property $condition_options 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
		"IsBlank" => "Is blank",
14
		"IsNotBlank" => "Is not blank",
15
		"HasValue" => "Equals",
16
		"ValueNot" => "Doesn't equal",
17
		"ValueLessThan" => "Less than",
18
		"ValueLessThanEqual" => "Less than or equal",
19
		"ValueGreaterThan" => "Greater than",
20
		"ValueGreaterThanEqual" => "Greater than or equal"
21
	);
22
23
	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...
24
		'Display' => 'Enum("Show,Hide")',
25
		'ConditionOption' => 'Enum("IsBlank,IsNotBlank,HasValue,ValueNot,ValueLessThan,ValueLessThanEqual,ValueGreaterThan,ValueGreaterThanEqual")',
26
		'FieldValue' => 'Varchar'
27
	);
28
29
	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...
30
		'Parent' => 'EditableFormField',
31
		'ConditionField' => 'EditableFormField'
32
	);
33
34
	/**
35
	 * Built in extensions required
36
	 *
37
	 * @config
38
	 * @var array
39
	 */
40
	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...
41
		"Versioned('Stage', 'Live')"
42
	);
43
44
	/**
45
	 * Publish this custom rule to the live site
46
	 *
47
	 * Wrapper for the {@link Versioned} publish function
48
	 */
49
	public function doPublish($fromStage, $toStage, $createNewVersion = false) {
50
		$this->publish($fromStage, $toStage, $createNewVersion);
0 ignored issues
show
Bug introduced by
The method publish() does not exist on EditableCustomRule. Did you maybe mean doPublish()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
51
	}
52
53
	/**
54
	 * Delete this custom rule from a given stage
55
	 *
56
	 * Wrapper for the {@link Versioned} deleteFromStage function
57
	 */
58
	public function doDeleteFromStage($stage) {
59
		$this->deleteFromStage($stage);
0 ignored issues
show
Bug introduced by
The method deleteFromStage() does not exist on EditableCustomRule. Did you maybe mean doDeleteFromStage()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
60
	}
61
62
63
    /**
64
     * @param Member $member
65
     * @return bool
66
     */
67
	public function canDelete($member = null) {
68
		return $this->canEdit($member);
69
	}
70
71
    /**
72
     * @param Member $member
73
     * @return bool
74
     */
75
	public function canEdit($member = null) {
76
        return $this->Parent()->canEdit($member);
77
	}
78
79
    /**
80
     * @param Member $member
81
     * @return bool
82
     */
83
	public function canView($member = null) {
84
		return $this->Parent()->canView($member);
85
	}
86
87
	/**
88
	 * Return whether a user can create an object of this type
89
	 *
90
     * @param Member $member
91
     * @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...
92
	 * @return bool
93
	 */
94 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...
95
		// Check parent page
96
        $parent = $this->getCanCreateContext(func_get_args());
97
        if($parent) {
98
            return $parent->canEdit($member);
99
        }
100
101
        // Fall back to secure admin permissions
102
        return parent::canCreate($member);
103
	}
104
105
    /**
106
     * Helper method to check the parent for this object
107
     *
108
     * @param array $args List of arguments passed to canCreate
109
     * @return DataObject Some parent dataobject to inherit permissions from
110
     */
111 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...
112
        // Inspect second parameter to canCreate for a 'Parent' context
113
        if(isset($args[1]['Parent'])) {
114
            return $args[1]['Parent'];
115
        }
116
        // Hack in currently edited page if context is missing
117
        if(Controller::has_curr() && Controller::curr() instanceof CMSMain) {
118
            return Controller::curr()->currentPage();
119
        }
120
121
        // No page being edited
122
        return null;
123
    }
124
125
    /**
126
     * @param Member $member
127
     * @return bool
128
     */
129
    public function canPublish($member = null) {
130
        return $this->canEdit($member);
131
    }
132
133
    /**
134
     * @param Member $member
135
     * @return bool
136
     */
137
    public function canUnpublish($member = null) {
138
        return $this->canDelete($member);
139
    }
140
}
141