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

EditableOption::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
 * 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
	);
20
21
	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...
22
		"Parent" => "EditableMultipleOptionField",
23
	);
24
25
	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...
26
		"Versioned('Stage', 'Live')"
27
	);
28
29
	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...
30
		'Title',
31
		'Default'
32
	);
33
34
	/**
35
	 * @param Member $member
36
	 *
37
	 * @return boolean
38
	 */
39
	public function canEdit($member = null) {
40
		return $this->Parent()->canEdit($member);
41
	}
42
43
	/**
44
	 * @param Member $member
45
	 *
46
	 * @return boolean
47
	 */
48
	public function canDelete($member = null) {
49
		return $this->canEdit($member);
50
	}
51
52
	public function getEscapedTitle() {
53
		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...
54
	}
55
56
    /**
57
     * @param Member $member
58
     * @return bool
59
     */
60
	public function canView($member = null) {
61
		return $this->Parent()->canView($member);
62
	}
63
64
	/**
65
	 * Return whether a user can create an object of this type
66
	 *
67
     * @param Member $member
68
     * @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...
69
	 * @return bool
70
	 */
71 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...
72
		// Check parent page
73
        $parent = $this->getCanCreateContext(func_get_args());
74
        if($parent) {
75
            return $parent->canEdit($member);
76
        }
77
78
        // Fall back to secure admin permissions
79
        return parent::canCreate($member);
80
	}
81
82
    /**
83
     * Helper method to check the parent for this object
84
     *
85
     * @param array $args List of arguments passed to canCreate
86
     * @return DataObject Some parent dataobject to inherit permissions from
87
     */
88 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...
89
        // Inspect second parameter to canCreate for a 'Parent' context
90
        if(isset($args[1]['Parent'])) {
91
            return $args[1]['Parent'];
92
        }
93
        // Hack in currently edited page if context is missing
94
        if(Controller::has_curr() && Controller::curr() instanceof CMSMain) {
95
            return Controller::curr()->currentPage();
96
        }
97
98
        // No page being edited
99
        return null;
100
    }
101
102
    /**
103
     * @param Member $member
104
     * @return bool
105
     */
106
    public function canPublish($member = null) {
107
        return $this->canEdit($member);
108
    }
109
110
    /**
111
     * @param Member $member
112
     * @return bool
113
     */
114
    public function canUnpublish($member = null) {
115
        return $this->canDelete($member);
116
    }
117
}
118