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.

RESTController::getResponse()   C
last analyzed

Complexity

Conditions 14
Paths 61

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 14.3828

Importance

Changes 0
Metric Value
cc 14
nc 61
nop 2
dl 0
loc 43
ccs 21
cts 24
cp 0.875
crap 14.3828
rs 6.2666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * A controller for RESTful calls
4
 *
5
 * @package application/controllers
6
 * @author marius orcsik <[email protected]>
7
 * @date 2013.10.04
8
 */
9
namespace vsc\rest\application\controllers;
10
11
use vsc\application\controllers\ExceptionController;
12
use vsc\application\controllers\JsonController;
13
use vsc\application\processors\AuthenticatedProcessorInterface;
14
use vsc\application\processors\ProcessorA;
15
use vsc\presentation\requests\ContentType;
16
use vsc\presentation\responses\HttpResponseType;
17
use vsc\rest\application\processors\RESTProcessorA;
18
use vsc\presentation\requests\HttpRequestA;
19
use vsc\presentation\responses\ExceptionAuthenticationNeeded;
20
use vsc\presentation\responses\ExceptionResponseError;
21
use vsc\presentation\responses\HttpResponseA;
22
23
class RESTController extends JsonController {
24
	/**
25
	 * @param HttpRequestA $oRequest
26
	 * @param RESTProcessorA $oProcessor
0 ignored issues
show
Documentation introduced by
Should the type for parameter $oProcessor not be RESTProcessorA|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
27
	 * @returns HttpResponseA
28
	 * @throws \vsc\presentation\responses\ExceptionResponse
29
	 * @throws \vsc\presentation\responses\ExceptionResponseError
30
	 * @throws \vsc\presentation\views\ExceptionView
31
	 * @throws ExceptionResponseError
32
	 */
33 9
	public function getResponse(HttpRequestA $oRequest, $oProcessor = null) {
34 9
		$oModel = null;
0 ignored issues
show
Unused Code introduced by
$oModel is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
35
36
		try {
37 9
			if (!$oRequest->isGet()) {
38
				if ($oRequest->hasContentType() && !ContentType::isValidContentType($oRequest->getContentType())) {
39
					throw new ExceptionResponseError('Invalid request content type', HttpResponseType::UNSUPPORTED_MEDIA_TYPE);
40
				}
41
			}
42 9
			if (!ProcessorA::isValid($oProcessor)) {
43 2
				throw new ExceptionController('Invalid request processor');
44
			}
45
			/* @var RESTProcessorA $oProcessor */
46 7
			if (RESTProcessorA::isValid($oProcessor) && !$oProcessor->validRequestMethod($oRequest->getHttpMethod())) {
47 2
				throw new ExceptionResponseError('Invalid request method', HttpResponseType::METHOD_NOT_ALLOWED);
48
			}
49 5
			$oMap = $oProcessor->getMap();
50 5
			if ($oMap->requiresAuthentication()) {
51
				try {
52 4
					if ($oProcessor instanceof AuthenticatedProcessorInterface) {
53
						/* @var AuthenticatedProcessorInterface $oProcessor */
54 3
						if (!$oRequest->hasAuthenticationData()) {
55
							throw new ExceptionAuthenticationNeeded('This resource needs authentication');
56
						}
57
						// here we check that the request contains the same authentication type as the map
58 3
						if (($oRequest->getAuthentication()->getType() & $oMap->getAuthenticationType()) !== $oMap->getAuthenticationType()) {
59 1
							throw new ExceptionAuthenticationNeeded('Invalid authorization scheme. Supported schemes: ' . implode(', ', $oMap->getValidAuthenticationSchemas()));
60
						}
61 2
						if (!$oProcessor->handleAuthentication($oRequest->getAuthentication())) {
62 2
							throw new ExceptionAuthenticationNeeded('Invalid authentication data', 'testrealm');
63
						}
64
					} else {
65 1
						throw new ExceptionAuthenticationNeeded('This resource requires authentication but doesn\'t support any authorization scheme');
66
					}
67 4
				} catch (ExceptionAuthenticationNeeded $e) {
68 5
					return $this->getErrorResponse($e, $oRequest);
69
				}
70
			}
71 4
		} catch (\Exception $e) {
72 4
			return $this->getErrorResponse($e, $oRequest);
73
		}
74 1
		return parent::getResponse($oRequest, $oProcessor);
0 ignored issues
show
Bug introduced by
It seems like $oProcessor can also be of type object<vsc\application\p...atedProcessorInterface>; however, vsc\application\controll...trollerA::getResponse() does only seem to accept object<vsc\application\p...essors\ProcessorA>|null, 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...
75
	}
76
77
	/**
78
	 * Returns a view based on the
79
	 * @fixme
80
	 * @return \vsc\presentation\views\JsonView
81
	 */
82 1
	public function getDefaultView() {
83 1
		return parent::getDefaultView();
84
	}
85
}
86