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.

ApproveControl::handleApprove()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Lookyman\NetteOAuth2Server\UI;
5
6
use League\OAuth2\Server\AuthorizationServer;
7
use League\OAuth2\Server\Exception\OAuthServerException;
8
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
9
use Nette\Application\AbortException;
10
use Nette\Application\IResponse;
11
use Nette\Application\UI\Control;
12
use Nette\Http\IResponse as HttpResponse;
13
use Nette\Http\Session;
14
use Nextras\Application\UI\SecuredLinksControlTrait;
15
use Psr\Log\LoggerAwareInterface;
16
use Psr\Log\LoggerAwareTrait;
17
18
/**
19
 * @method void onResponse(IResponse $response)
20
 */
21
class ApproveControl extends Control implements LoggerAwareInterface
22
{
23
	use LoggerAwareTrait;
24
	use Psr7Trait;
25
	use SecuredLinksControlTrait;
26
27
	/**
28
	 * @var callable[]
29
	 */
30
	public $onResponse;
31
32
	/**
33
	 * @var AuthorizationRequest
34
	 */
35
	private $authorizationRequest;
36
37
	/**
38
	 * @var AuthorizationServer
39
	 */
40
	private $authorizationServer;
41
42
	/**
43
	 * @var Session
44
	 */
45
	private $session;
46
47
	/**
48
	 * @var string
49
	 */
50
	private $templateFile;
51
52
	/**
53
	 * @param AuthorizationServer $authorizationServer
54
	 * @param Session $session
55
	 * @param AuthorizationRequest $authorizationRequest
56
	 */
57
	public function __construct(AuthorizationServer $authorizationServer, Session $session, AuthorizationRequest $authorizationRequest)
58
	{
59
		$this->authorizationServer = $authorizationServer;
60
		$this->session = $session;
61
		$this->authorizationRequest = $authorizationRequest;
62
		$this->templateFile = __DIR__ . '/templates/approve.latte';
63
	}
64
65
	public function render()
66
	{
67
		$this->template->setFile($this->templateFile);
0 ignored issues
show
Documentation introduced by
The property $template is declared private in Nette\Application\UI\Control. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
		$this->template->authorizationRequest = $this->authorizationRequest;
0 ignored issues
show
Documentation introduced by
The property $template is declared private in Nette\Application\UI\Control. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
Accessing authorizationRequest on the interface Nette\Application\UI\ITemplate suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
69
		$this->template->render();
0 ignored issues
show
Documentation introduced by
The property $template is declared private in Nette\Application\UI\Control. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
70
	}
71
72
	/**
73
	 * @secured
74
	 */
75
	public function handleApprove()
76
	{
77
		$this->authorizationRequest->setAuthorizationApproved(true);
78
		$this->completeAuthorizationRequest();
79
	}
80
81
	/**
82
	 * @secured
83
	 */
84
	public function handleDeny()
85
	{
86
		$this->authorizationRequest->setAuthorizationApproved(false);
87
		$this->completeAuthorizationRequest();
88
	}
89
90
	private function completeAuthorizationRequest()
91
	{
92
		$this->session->getSection(OAuth2Presenter::SESSION_NAMESPACE)->remove();
93
94
		$response = $this->createResponse();
95
		try {
96
			$this->onResponse($this->authorizationServer->completeAuthorizationRequest($this->authorizationRequest, $response));
0 ignored issues
show
Documentation introduced by
$this->authorizationServ...tionRequest, $response) is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a object<Nette\Application\IResponse>.

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...
97
98
		} catch (AbortException $e) {
99
			throw $e;
100
101
		} catch (OAuthServerException $e) {
102
			$this->onResponse($e->generateHttpResponse($response));
0 ignored issues
show
Documentation introduced by
$e->generateHttpResponse($response) is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a object<Nette\Application\IResponse>.

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...
103
104
		} catch (\Exception $e) {
105
			if ($this->logger) {
106
				$this->logger->error($e->getMessage(), ['exception' => $e]);
107
			}
108
			$body = $this->createStream();
109
			$body->write('Unknown error');
110
			$this->onResponse($response->withStatus(HttpResponse::S500_INTERNAL_SERVER_ERROR)->withBody($body));
111
		}
112
	}
113
114
	/**
115
	 * @param string $file
116
	 */
117
	public function setTemplateFile(string $file)
118
	{
119
		$this->templateFile = $file;
120
	}
121
}
122