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 |
||
7 | class WorkflowFieldActionController extends RequestHandler { |
||
|
|||
8 | |||
9 | public static $url_handlers = array( |
||
10 | 'new/$Class' => '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 | $class = $this->request->param('Class'); |
||
31 | |||
32 | if(!class_exists($class) || !is_subclass_of($class, 'WorkflowAction')) { |
||
33 | $this->httpError(400); |
||
34 | } |
||
35 | |||
36 | $reflector = new ReflectionClass($class); |
||
37 | |||
38 | if($reflector->isAbstract() || !singleton($class)->canCreate()) { |
||
39 | $this->httpError(400); |
||
40 | } |
||
41 | |||
42 | $record = new $class(); |
||
43 | $record->WorkflowDefID = $this->parent->Definition()->ID; |
||
44 | |||
45 | return new WorkflowFieldItemController($this, "new/$class", $record); |
||
46 | } |
||
47 | |||
48 | View Code Duplication | public function handleItem() { |
|
49 | $id = $this->request->param('ID'); |
||
50 | $defn = $this->parent->Definition(); |
||
51 | $action = $defn->Actions()->byID($id); |
||
52 | |||
53 | if(!$action) { |
||
54 | $this->httpError(404); |
||
55 | } |
||
56 | |||
57 | if(!$action->canEdit()) { |
||
58 | $this->httpError(403); |
||
59 | } |
||
60 | |||
61 | return new WorkflowFieldItemController($this, "item/$id", $action); |
||
62 | } |
||
63 | |||
64 | public function RootField() { |
||
67 | |||
68 | public function Link($action = null) { |
||
71 | |||
72 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.