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.
Completed
Push — master ( c2e63e...e620da )
by Lukáš
12s
created

ApprovePresenterTrait::createComponentApprove()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
c 0
b 0
f 0
rs 8.439
cc 6
eloc 19
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Lookyman\NetteOAuth2Server\UI;
5
6
use Lookyman\NetteOAuth2Server\Psr7\ApplicationPsr7ResponseInterface;
7
use Lookyman\NetteOAuth2Server\RedirectConfig;
8
use Lookyman\NetteOAuth2Server\Storage\IAuthorizationRequestSerializer;
9
use Lookyman\NetteOAuth2Server\User\UserEntity;
10
use Nette\Application\AbortException;
11
use Nette\Application\IResponse;
12
use Nette\Http\Session;
13
use Nette\Http\SessionSection;
14
use Nette\Security\User;
15
use Nextras\Application\UI\SecuredLinksPresenterTrait;
16
17
trait ApprovePresenterTrait
18
{
19
	use SecuredLinksPresenterTrait;
20
21
	/**
22
	 * @var IApproveControlFactory
23
	 * @inject
24
	 */
25
	public $approveControlFactory;
26
27
	/**
28
	 * @var IAuthorizationRequestSerializer
29
	 * @inject
30
	 */
31
	public $authorizationRequestSerializer;
32
33
	/**
34
	 * @var RedirectConfig
35
	 * @inject
36
	 */
37
	public $redirectConfig;
38
39
	/**
40
	 * @return ApproveControl
41
	 */
42
	protected function createComponentApprove(): ApproveControl
43
	{
44
		$control = $this->approveControlFactory->create();
45
46
		$control->onAuthorizationComplete[] = function () {
47
			$this->getSession(OAuth2Presenter::SESSION_NAMESPACE)->remove();
0 ignored issues
show
Bug introduced by
The method remove does only exist in Nette\Http\SessionSection, but not in Nette\Http\Session.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
48
		};
49
50
		$control->onResponse[] = function (ApplicationPsr7ResponseInterface $response) {
51
			$this->sendResponse($response);
52
		};
53
54
		$control->onAnchor[] = function (ApproveControl $control) {
55
			if (!$this->getUser()->isLoggedIn()) {
56
				$this->redirect(...$this->redirectConfig->getLoginDestination());
57
			}
58
59
			$data = $this->getSession(OAuth2Presenter::SESSION_NAMESPACE)->authorizationRequest;
60
			$authorizationRequest = $data ? $this->authorizationRequestSerializer->unserialize($data) : null;
61
			if (!$authorizationRequest) {
62
				return;
63
			}
64
65
			if (!$authorizationRequest->getUser()) {
66
				$authorizationRequest->setUser(new UserEntity($this->getUser()->getId()));
67
			}
68
			$control->setAuthorizationRequest($authorizationRequest);
69
70
			if ($authorizationRequest->isAuthorizationApproved()) {
71
				$control->completeAuthorizationRequest();
72
			}
73
		};
74
75
		return $control;
76
	}
77
78
	/**
79
	 * @param string|null $namespace
80
	 * @return Session|SessionSection
81
	 */
82
	abstract public function getSession($namespace = null);
83
84
	/**
85
	 * @return User
86
	 */
87
	abstract public function getUser();
88
89
	/**
90
	 * @param int $code [optional]
91
	 * @param string|null $destination
92
	 * @param array|mixed $args
93
	 * @throws AbortException
94
	 */
95
	abstract public function redirect($code, $destination = null, $args = []);
96
97
	/**
98
	 * @param IResponse $response
99
	 * @throws AbortException
100
	 */
101
	abstract public function sendResponse(IResponse $response);
102
}
103