WorkflowFieldTransitionController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 21.88 %

Coupling/Cohesion

Components 1
Dependencies 7
Metric Value
wmc 11
lcom 1
cbo 7
dl 14
loc 64
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handleAdd() 0 17 4
A handleItem() 14 14 4
A RootField() 0 3 1
A Link() 0 3 1
A __construct() 0 6 1

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
 * Handles requests for creating or editing transitions.
4
 *
5
 * @package silverstripe-advancedworkflow
6
 */
7
class WorkflowFieldTransitionController extends RequestHandler {
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...
8
9
	public static $url_handlers = array(
10
		'new/$ParentID!' => 'handleAdd',
11
		'item/$ID!'      => 'handleItem'
12
	);
13
14
	public static $allowed_actions = array(
15
		'handleAdd',
16
		'handleItem'
17
	);
18
19
	protected $parent;
20
	protected $name;
21
22
	public function __construct($parent, $name) {
23
		$this->parent = $parent;
24
		$this->name   = $name;
25
26
		parent::__construct();
27
	}
28
29
	public function handleAdd() {
30
		$parent = $this->request->param('ParentID');
31
		$action = WorkflowAction::get()->byID($this->request->param('ParentID'));
32
33
		if(!$action || $action->WorkflowDefID != $this->RootField()->Definition()->ID) {
34
			$this->httpError(404);
35
		}
36
37
		if(!singleton('WorkflowTransition')->canCreate()) {
38
			$this->httpError(403);
39
		}
40
41
		$transition = new WorkflowTransition();
42
		$transition->ActionID = $action->ID;
0 ignored issues
show
Documentation introduced by
The property ActionID does not exist on object<WorkflowTransition>. 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
44
		return new WorkflowFieldItemController($this, "new/$parent", $transition);
45
	}
46
47 View Code Duplication
	public function handleItem() {
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...
48
		$id    = $this->request->param('ID');
49
		$trans = WorkflowTransition::get()->byID($id);
50
51
		if(!$trans || $trans->Action()->WorkflowDefID != $this->RootField()->Definition()->ID) {
0 ignored issues
show
Bug introduced by
The method Action() does not exist on DataObject. Did you maybe mean getCMSActions()?

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...
52
			$this->httpError(404);
53
		}
54
55
		if(!$trans->canEdit()) {
56
			$this->httpError(403);
57
		}
58
59
		return new WorkflowFieldItemController($this, "item/$id", $trans);
60
	}
61
62
	public function RootField() {
63
		return $this->parent;
64
	}
65
66
	public function Link($action = null) {
67
		return Controller::join_links($this->parent->Link(), $this->name, $action);
68
	}
69
70
}