Completed
Push — master ( 4e5770...5da95a )
by Daniel
34:35
created

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
/**
5
 * Declares a condition that determines whether an email can be sent to a given recipient
6
 *
7
 * @method UserDefinedForm_EmailRecipient Parent()
8
 */
9
class UserDefinedForm_EmailRecipientCondition 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...
10
11
	/**
12
	 * List of options
13
	 *
14
	 * @config
15
	 * @var array
16
	 */
17
	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...
18
		"IsBlank" => "Is blank",
19
		"IsNotBlank" => "Is not blank",
20
		"Equals" => "Equals",
21
		"NotEquals" => "Doesn't equal"
22
	);
23
24
	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...
25
		'ConditionOption' => 'Enum("IsBlank,IsNotBlank,Equals,NotEquals")',
26
		'ConditionValue' => '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' => 'UserDefinedForm_EmailRecipient',
31
		'ConditionField' => 'EditableFormField'
32
	);
33
34
	/**
35
	 * Determine if this rule matches the given condition
36
	 *
37
	 * @param array $data
38
	 * @param Form $form
39
	 * @return bool
40
	 */
41
	public function matches($data, $form) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
		$fieldName = $this->ConditionField()->Name;
0 ignored issues
show
Documentation Bug introduced by
The method ConditionField does not exist on object<UserDefinedForm_EmailRecipientCondition>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
43
		$fieldValue = isset($data[$fieldName]) ? $data[$fieldName] : null;
44
		switch($this->ConditionOption) {
0 ignored issues
show
Documentation introduced by
The property ConditionOption does not exist on object<UserDefinedForm_EmailRecipientCondition>. 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...
45
			case 'IsBlank':
46
				return empty($fieldValue);
47
			case 'IsNotBlank':
48
				return !empty($fieldValue);
49
			default:
50
				$matches = is_array($fieldValue)
51
					? in_array($this->ConditionValue, $fieldValue)
0 ignored issues
show
Documentation introduced by
The property ConditionValue does not exist on object<UserDefinedForm_EmailRecipientCondition>. 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...
52
					: $this->ConditionValue === (string) $fieldValue;
0 ignored issues
show
Documentation introduced by
The property ConditionValue does not exist on object<UserDefinedForm_EmailRecipientCondition>. 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...
53
				return ($this->ConditionOption === 'Equals') === (bool)$matches;
0 ignored issues
show
Documentation introduced by
The property ConditionOption does not exist on object<UserDefinedForm_EmailRecipientCondition>. 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
    	/**
58
	 * Return whether a user can create an object of this type
59
	 *
60
     * @param Member $member
61
     * @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...
62
	 * @return bool
63
	 */
64 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...
65
		// Check parent page
66
        $parent = $this->getCanCreateContext(func_get_args());
67
        if($parent) {
68
            return $parent->canEdit($member);
69
        }
70
71
        // Fall back to secure admin permissions
72
        return parent::canCreate($member);
73
	}
74
75
    /**
76
     * Helper method to check the parent for this object
77
     *
78
     * @param array $args List of arguments passed to canCreate
79
     * @return SiteTree Parent page instance
80
     */
81 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...
82
        // Inspect second parameter to canCreate for a 'Parent' context
83
        if(isset($args[1]['Parent'])) {
84
            return $args[1]['Parent'];
85
        }
86
        // Hack in currently edited page if context is missing
87
        if(Controller::has_curr() && Controller::curr() instanceof CMSMain) {
88
            return Controller::curr()->currentPage();
89
        }
90
91
        // No page being edited
92
        return null;
93
    }
94
95
	/**
96
	 * @param Member
97
	 *
98
	 * @return boolean
99
	 */
100
	public function canView($member = null) {
101
		return $this->Parent()->canView($member);
102
	}
103
104
	/**
105
	 * @param Member
106
	 *
107
	 * @return boolean
108
	 */
109
	public function canEdit($member = null) {
110
		return $this->Parent()->canEdit($member);
111
	}
112
113
	/**
114
	 * @param Member
115
	 *
116
	 * @return boolean
117
	 */
118
	public function canDelete($member = null) {
119
		return $this->canEdit($member);
120
	}
121
}
122