GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

WorkflowFieldActionController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 16
loc 96
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handleAdd() 0 23 5
A handleItem() 16 16 3
A RootField() 0 4 1
A Link() 0 4 1
A sanitiseClassName() 0 4 1
A unsanitiseClassName() 0 4 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
namespace Symbiote\AdvancedWorkflow\FormFields;
4
5
use ReflectionClass;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Control\RequestHandler;
8
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowAction;
9
10
/**
11
 * Handles requests for creating or editing actions.
12
 *
13
 * @package silverstripe-advancedworkflow
14
 */
15
class WorkflowFieldActionController extends RequestHandler
16
{
17
    private static $url_handlers = 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...
18
        'new/$Class' => 'handleAdd',
19
        'item/$ID'   => 'handleItem'
20
    );
21
22
    private static $allowed_actions = 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...
23
        'handleAdd',
24
        'handleItem'
25
    );
26
27
    protected $parent;
28
    protected $name;
29
30
    public function __construct($parent, $name)
31
    {
32
        $this->parent = $parent;
33
        $this->name   = $name;
34
35
        parent::__construct();
36
    }
37
38
    public function handleAdd()
39
    {
40
        $class = $this->unsanitiseClassName($this->request->param('Class'));
41
42
        if (!class_exists($class) || !is_subclass_of($class, WorkflowAction::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Symbiote\AdvancedWorkfl...s\WorkflowAction::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
43
            $this->httpError(400);
44
        }
45
46
        $reflector = new ReflectionClass($class);
47
48
        if ($reflector->isAbstract() || !singleton($class)->canCreate()) {
49
            $this->httpError(400);
50
        }
51
52
        $record = new $class();
53
        $record->WorkflowDefID = $this->parent->Definition()->ID;
54
55
        return new WorkflowFieldItemController(
56
            $this,
57
            Controller::join_links('new', $this->sanitiseClassName($class)),
58
            $record
59
        );
60
    }
61
62 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...
63
    {
64
        $id     = $this->request->param('ID');
65
        $defn   = $this->parent->Definition();
66
        $action = $defn->Actions()->byID($id);
67
68
        if (!$action) {
69
            $this->httpError(404);
70
        }
71
72
        if (!$action->canEdit()) {
73
            $this->httpError(403);
74
        }
75
76
        return new WorkflowFieldItemController($this, "item/$id", $action);
77
    }
78
79
    public function RootField()
80
    {
81
        return $this->parent;
82
    }
83
84
    public function Link($action = null)
85
    {
86
        return Controller::join_links($this->parent->Link(), $this->name, $action);
87
    }
88
89
    /**
90
     * Sanitise a model class' name for inclusion in a link
91
     *
92
     * @param string $class
93
     * @return string
94
     */
95
    protected function sanitiseClassName($class)
96
    {
97
        return str_replace('\\', '-', $class);
98
    }
99
100
    /**
101
     * Unsanitise a model class' name from a URL param
102
     *
103
     * @param string $class
104
     * @return string
105
     */
106
    protected function unsanitiseClassName($class)
107
    {
108
        return str_replace('-', '\\', $class);
109
    }
110
}
111