Completed
Pull Request — master (#292)
by Jason
09:05
created

OptionGroup   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 103
Duplicated Lines 19.42 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 6
dl 20
loc 103
rs 10
c 0
b 0
f 0
ccs 0
cts 64
cp 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 8 1
B requireDefaultRecords() 20 24 5
A getCMSValidator() 0 3 1
A canView() 0 3 1
A canCreate() 0 3 1
A onBeforeDelete() 0 14 3
A canEdit() 0 11 2
A canDelete() 0 3 1
A validate() 0 14 3

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
 * @package FoxyStripe
5
 *
6
 */
7
8
class OptionGroup extends DataObject{
9
10
	private static $db = array(
11
		'Title' => 'Varchar(100)'
12
	);
13
14
    private static $singular_name = 'Product Option Group';
15
	private static $plural_name = 'Product Option Groups';
16
	private static $description = 'Groups of product options, e.g. size, color, etc';
17
18
	function getCMSFields(){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
20
		$fields = parent::getCMSFields();
21
22
		$this->extend('getCMSFields', $fields);
23
24
		return $fields;
25
	}
26
27
	public function requireDefaultRecords() {
28
		parent::requireDefaultRecords();
29
		// create a catch-all group
30 View Code Duplication
		if(!OptionGroup::get()->filter(array('Title' => 'Options'))->first()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
31
			$do = new OptionGroup();
32
			$do->Title = "Options";
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OptionGroup>. 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...
33
			$do->write();
34
		}
35 View Code Duplication
		if(!OptionGroup::get()->filter(array('Title' => 'Size'))->first()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
36
            $do = new OptionGroup();
37
            $do->Title = "Size";
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OptionGroup>. 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...
38
            $do->write();
39
        }
40 View Code Duplication
        if(!OptionGroup::get()->filter(array('Title' => 'Color'))->first()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
41
            $do = new OptionGroup();
42
            $do->Title = "Color";
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OptionGroup>. 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...
43
            $do->write();
44
        }
45 View Code Duplication
        if(!OptionGroup::get()->filter(array('Title' => 'Type'))->first()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
46
            $do = new OptionGroup();
47
            $do->Title = "Type";
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OptionGroup>. 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...
48
            $do->write();
49
        }
50
	}
51
52
	public function getCMSValidator() {
53
		return new RequiredFields(array('Title'));
54
	}
55
56
	public function validate(){
57
		$result = parent::validate();
58
59
		$title = $this->Title;
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OptionGroup>. 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...
60
		$firstChar = substr($title, 0, 1);
61
		if(preg_match('/[^a-zA-Z]/', $firstChar)){
62
			$result->error('The first character of the Title can only be a letter', 'bad');
63
		}
64
		if(preg_match('/[^a-zA-Z]\s/', $title)){
65
			$result->error('Please only use letters, numbers and spaces in the title', 'bad');
66
		}
67
68
		return $result;
69
	}
70
71
	public function onBeforeDelete(){
72
		parent::onBeforeDelete();
73
74
		//make sure that if we delete this option group, we reassign the group's option items to the 'None' group.
75
		$items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID));
76
77
		if(isset($items)){
78
			$noneGroup = OptionGroup::get()->filter(array('Title' => 'Options'))->first();
79
			foreach($items as $item){
80
				$item->ProductOptionGroupID = $noneGroup->ID;
81
				$item->write();
82
			}
83
		}
84
	}
85
86
	public function canView($member = false) {
87
		return true;
88
	}
89
90
	public function canEdit($member = null) {
91
		switch($this->Title){
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<OptionGroup>. 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...
92
			case 'Options':
93
				return false;
94
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
95
			default:
96
				return Permission::check('Product_CANCRUD');
97
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
98
		}
99
100
	}
101
102
	public function canDelete($member = null) {
103
		return $this->canEdit();
104
	}
105
106
	public function canCreate($member = null) {
107
		return Permission::check('Product_CANCRUD');
108
	}
109
110
}
111