Completed
Push — stable10 ( d896d4...713e20 )
by Joas
26:40 queued 16:05
created

FlowOperations   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 19.35 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 18
loc 93
rs 10
wmc 10
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getOperations() 0 9 2
A addOperation() 9 9 2
A updateOperation() 9 9 2
A deleteOperation() 0 4 1
A prepareOperation() 0 15 2

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
 * @copyright Copyright (c) 2016 Morris Jobke <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\WorkflowEngine\Controller;
23
24
use OCA\WorkflowEngine\Manager;
25
use OCP\AppFramework\Controller;
26
use OCP\AppFramework\Http;
27
use OCP\AppFramework\Http\JSONResponse;
28
use OCP\IRequest;
29
30
class FlowOperations extends Controller {
31
32
	/** @var Manager */
33
	protected $manager;
34
35
	/**
36
	 * @param IRequest $request
37
	 * @param Manager $manager
38
	 */
39
	public function __construct(IRequest $request, Manager $manager) {
40
		parent::__construct('workflowengine', $request);
41
		$this->manager = $manager;
42
	}
43
44
	/**
45
	 * @NoCSRFRequired
46
	 *
47
	 * @param string $class
48
	 * @return JSONResponse
49
	 */
50
	public function getOperations($class) {
51
		$operations = $this->manager->getOperations($class);
52
53
		foreach ($operations as &$operation) {
54
			$operation = $this->prepareOperation($operation);
55
		}
56
57
		return new JSONResponse($operations);
58
	}
59
60
	/**
61
	 * @param string $class
62
	 * @param string $name
63
	 * @param array[] $checks
64
	 * @param string $operation
65
	 * @return JSONResponse The added element
66
	 */
67 View Code Duplication
	public function addOperation($class, $name, $checks, $operation) {
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...
68
		try {
69
			$operation = $this->manager->addOperation($class, $name, $checks, $operation);
70
			$operation = $this->prepareOperation($operation);
71
			return new JSONResponse($operation);
72
		} catch (\UnexpectedValueException $e) {
73
			return new JSONResponse($e->getMessage(), Http::STATUS_BAD_REQUEST);
0 ignored issues
show
Documentation introduced by
$e->getMessage() is of type string, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
		}
75
	}
76
77
	/**
78
	 * @param int $id
79
	 * @param string $name
80
	 * @param array[] $checks
81
	 * @param string $operation
82
	 * @return JSONResponse The updated element
83
	 */
84 View Code Duplication
	public function updateOperation($id, $name, $checks, $operation) {
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...
85
		try {
86
			$operation = $this->manager->updateOperation($id, $name, $checks, $operation);
87
			$operation = $this->prepareOperation($operation);
88
			return new JSONResponse($operation);
89
		} catch (\UnexpectedValueException $e) {
90
			return new JSONResponse($e->getMessage(), Http::STATUS_BAD_REQUEST);
0 ignored issues
show
Documentation introduced by
$e->getMessage() is of type string, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
		}
92
	}
93
94
	/**
95
	 * @param int $id
96
	 * @return JSONResponse
97
	 */
98
	public function deleteOperation($id) {
99
		$deleted = $this->manager->deleteOperation((int) $id);
100
		return new JSONResponse($deleted);
0 ignored issues
show
Documentation introduced by
$deleted is of type boolean, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
	}
102
103
	/**
104
	 * @param array $operation
105
	 * @return array
106
	 */
107
	protected function prepareOperation(array $operation) {
108
		$checkIds = json_decode($operation['checks']);
109
		$checks = $this->manager->getChecks($checkIds);
110
111
		$operation['checks'] = [];
112
		foreach ($checks as $check) {
113
			// Remove internal values
114
			unset($check['id']);
115
			unset($check['hash']);
116
117
			$operation['checks'][] = $check;
118
		}
119
120
		return $operation;
121
	}
122
}
123