Completed
Pull Request — master (#535)
by Julius
02:47
created

CardApiController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Ryan Fletcher <[email protected]>
4
 *
5
 * @author Ryan Fletcher <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
 namespace OCA\Deck\Controller;
25
26
 use OCP\AppFramework\ApiController;
27
 use OCP\AppFramework\Http;
28
 use OCP\AppFramework\Http\DataResponse;
29
 use OCP\IRequest;
30
 use OCA\Deck\Service\CardService;
31
32
 /**
33
 * Class BoardApiController
34
 *
35
 * @package OCA\Deck\Controller
36
 */
37
class CardApiController extends ApiController {
38
	private $cardService;		
39
	private $userId;	
40
41
	/**
42
	 * @param string $appName
43
	 * @param IRequest $request
44
	 * @param CardService $cardService
45
	 * @param $userId
46
	 */
47
	public function __construct($appName, IRequest $request, CardService $cardService, $userId) {
48
		parent::__construct($appName, $request);		
49
		$this->cardService = $cardService;		
50
		$this->userId = $userId;		
51
	}
52
53
	/**
54
	 * @NoAdminRequired
55
	 * @CORS
56
	 * @NoCSRFRequired
57
	 *
58
	 * Get a specific card.
59
	 */
60
	public function get() {
61
		$card = $this->cardService->find($this->request->getParam('cardId'));
62
		return new DataResponse($card, HTTP::STATUS_OK);
63
	}
64
65
	/**
66
	 * @NoAdminRequired
67
	 * @CORS
68
	 * @NoCSRFRequired
69
	 *
70
	 * @params $title
71
	 * @params $type
72
	 * @params $order
73
	 * 
74
	 * Get a specific card.
75
	 */
76
	public function create($title, $type = 'plain', $order = 999) {
77
		$card = $this->cardService->create($title, $this->request->getParam('stackId'), $type, $order, $this->userId);
78
		return new DataResponse($card, HTTP::STATUS_OK);
79
	}
80
81
	/**
82
	 * @NoAdminRequired
83
	 * @CORS
84
	 * @NoCSRFRequired
85
	 *	 
86
	 * 
87
	 * Update a card
88
	 */
89
	public function update($title, $type, $order = 0, $description = '', $owner, $duedate = null) {
90
		$card = $this->cardService->update($this->request->getParam('cardId'), $title, $this->request->getParam('stackId'), $type, $order, $description, $owner, $duedate, 0);
91
		return new DataResponse($card, HTTP::STATUS_OK);
92
	}		
93
94
	/**
95
	 * @NoAdminRequired
96
	 * @CORS
97
	 * @NoCSRFRequired	 	 
98
	 * 
99
	 * Delete a specific card.
100
	 */
101
	public function delete() {
102
		$card = $this->cardService->delete($this->request->getParam('cardId'));
103
		return new DataResponse($card, HTTP::STATUS_OK);
104
	}
105
106
	/**
107
	 * @NoAdminRequired
108
	 * @CORS
109
	 * @NoCSRFRequired	 	 
110
	 * 
111
	 * Assign a label to a card.
112
	 */
113
	public function assignLabel($labelId) {
114
		$card = $this->cardService->assignLabel($this->request->getParam('cardId'), $labelId);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $card is correct as $this->cardService->assi...am('cardId'), $labelId) (which targets OCA\Deck\Service\CardService::assignLabel()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
115
		return new DataResponse($card, HTTP::STATUS_OK);
0 ignored issues
show
Documentation introduced by
$card is of type null, 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...
116
	}
117
118
	/**
119
	 * @NoAdminRequired
120
	 * @CORS
121
	 * @NoCSRFRequired	 	 
122
	 * 
123
	 * Assign a label to a card.
124
	 */
125
	public function removeLabel($labelId) {
126
		$card = $this->cardService->removeLabel($this->request->getParam('cardId'), $labelId);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $card is correct as $this->cardService->remo...am('cardId'), $labelId) (which targets OCA\Deck\Service\CardService::removeLabel()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
127
		return new DataResponse($card, HTTP::STATUS_OK);
0 ignored issues
show
Documentation introduced by
$card is of type null, 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...
128
	}
129
130
	/**
131
	 * @NoAdminRequired
132
	 * @CORS
133
	 * @NoCSRFRequired	 	 
134
	 * 
135
	 * Unassign a label to a card.
136
	 */
137
	public function unassignUser($userId) {
138
		$card = $this->cardService->unassignUser($this->request->getParam('cardId'), $userId);
139
		return new DataResponse($card, HTTP::STATUS_OK);
140
	}
141
142
	public function assignUser($userId) {
143
		$card = $this->cardService->assignUser($this->request->getParam('cardId'), $userId);;
144
		return new DataResponse($card, HTTP::STATUS_OK);
0 ignored issues
show
Bug introduced by
It seems like $card defined by $this->cardService->assi...ram('cardId'), $userId) on line 143 can also be of type false or null; however, OCP\AppFramework\Http\DataResponse::__construct() does only seem to accept array|object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
145
	}
146
147
	/**
148
	 * @NoAdminRequired
149
	 * @CORS
150
	 * @NoCSRFRequired	 	 
151
	 * 
152
	 * Unassign a label to a card.
153
	 */
154
	public function reorder($stackId, $order) {
155
		$card = $this->cardService->reorder($this->request->getParam('cardId'), $stackId, $order);
156
		return new DataResponse($card, HTTP::STATUS_OK);
157
	}
158
}