Skip to content

Commit e8e7e40

Browse files
authored
Merge pull request #63 from tomaj/typehints
Added typehints
2 parents 96928c7 + 98684e8 commit e8e7e40

33 files changed

+261
-331
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
77
#### Changed
88

99
* Update nette libs to version 3.0.0 (BC break)
10+
* Added typehints (BC break)
1011
* Pretty JSON output in API console - without escaping unicode and slashes
1112
* Replaced handler information array triplet (endpoint, handler, authorization) with Api
1213

UPGRADE.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# UPGRADE
2+
3+
## Upgrade from 1.x to 2.0.0
4+
5+
### Removed support for old PHP versions
6+
New version not supported PHP versions 5.6 and 7.0 and also hhvm. Please use it with newer versions of PHP (>7.1)
7+
8+
### Updated dependencies
9+
Version 2.0.0 requires nette packages in version 3.0, so probably you will have to upgrade whole your nette application
10+
11+
### Typehints
12+
There are some breaking changes because of typehints:
13+
14+
#### ApiAuthorizationInterface
15+
Add typehints to methods:
16+
- `authorized(): bool`
17+
- `getErrorMessage(): ?string`
18+
19+
#### ApiHandlerInterface
20+
Add typehints to methods:
21+
- `params(): array`
22+
- `handle(array $params): Tomaj\NetteApi\Response\ResponseInterface`
23+
24+
#### ApiLoggerInterface
25+
Add typehints to method:
26+
- `log(int $responseCode, string $requestMethod, string $requestHeader, string $requestUri, string $requestIp, string $requestAgent, int $responseTime): bool`
27+
28+
#### BearerTokenRepositoryInterface
29+
Add typehints to methods:
30+
- `validToken(string $token): bool`
31+
- `ipRestrictions(string $token): ?string`
32+
33+
#### IpDetectorInterface
34+
Add typehints to method:
35+
- `getRequestIp(): string`
36+
37+
#### ParamInterface
38+
Add typehints to methods:
39+
- `isValid(): bool`
40+
- `getKey(): string`
41+
42+
#### EndpointInterface
43+
Add typehints to methods:
44+
- `getMethod(): string`
45+
- `getVersion(): int`
46+
- `getPackage(): string`
47+
- `getApiAction(): ?string`
48+
- `getUrl(): string`
49+
50+
### Renamed methods
51+
Few methods have been renamed, please use their new versions:
52+
- `ApiDecider::addApiHandler()` -> `ApiDecider::addApi()`
53+
- `ApiDecider::getApiHandler()` -> `ApiDecider::getApi()`
54+
- `ApiDecider::getHandlers()` -> `ApiDecider::getApis()`
55+
56+
### Final methods
57+
BaseHandler now have few final methods:
58+
- `setEndpointIdentifier`
59+
- `getEndpoint`
60+
- `setupLinkGenerator`
61+
- `createLink`
62+
63+
### Removed params
64+
Parameters $parent and $name have been removed from ApiListingControl. New usage is:
65+
```
66+
new ApiListingControl($apiDecider)
67+
```
68+
69+
### Changed params
70+
Some parameters were strictly typed:
71+
- second parameter in `JsonApiResponse::__construct` (`$payload` formerly known as `$data`) is now `array`
72+
- fifth parameter in `JsonApiResponse::__construct` (`$expiration`) is now `DateTimeInteface` or `null`
73+
- fourth parameter in `InputParam::__construc` (`$availableValues`) is now `array` or `null`

src/Authorization/ApiAuthorizationInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ interface ApiAuthorizationInterface
99
*
1010
* @return boolean
1111
*/
12-
public function authorized();
12+
public function authorized(): bool;
1313

1414
/**
1515
* If authorization deny acces, this method should provide additional information
1616
* abount cause of restriction.
1717
*
18-
* @return string|boolean
18+
* @return string|null
1919
*/
20-
public function getErrorMessage();
20+
public function getErrorMessage(): ?string;
2121
}

src/Authorization/BearerTokenAuthorization.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class BearerTokenAuthorization implements ApiAuthorizationInterface
1313
private $tokenRepository;
1414

1515
/**
16-
* @var string|boolean
16+
* @var string|null
1717
*/
18-
private $errorMessage = false;
18+
private $errorMessage = null;
1919

2020
/**
2121
* @var IpDetectorInterface
@@ -37,7 +37,7 @@ public function __construct(BearerTokenRepositoryInterface $tokenRepository, IpD
3737
/**
3838
* {@inheritdoc}
3939
*/
40-
public function authorized()
40+
public function authorized(): bool
4141
{
4242
$token = $this->readAuthorizationToken();
4343
if (!$token) {
@@ -61,7 +61,7 @@ public function authorized()
6161
/**
6262
* {@inheritdoc}
6363
*/
64-
public function getErrorMessage()
64+
public function getErrorMessage(): ?string
6565
{
6666
return $this->errorMessage;
6767
}
@@ -77,12 +77,12 @@ public function getErrorMessage()
7777
*
7878
* @return boolean
7979
*/
80-
private function isValidIp($ipRestrictions)
80+
private function isValidIp(?string $ipRestrictions): bool
8181
{
82-
if ($ipRestrictions === false) {
82+
if ($ipRestrictions === null) {
8383
return false;
8484
}
85-
if ($ipRestrictions == '*' || $ipRestrictions == '' || $ipRestrictions === null) {
85+
if ($ipRestrictions === '*' || $ipRestrictions === '') {
8686
return true;
8787
}
8888
$ip = $this->ipDetector->getRequestIp();
@@ -108,36 +108,36 @@ private function isValidIp($ipRestrictions)
108108
* @param string $range is in IP/CIDR format eg 127.0.0.1/24
109109
* @return boolean
110110
*/
111-
private function ipInRange($ip, $range)
111+
private function ipInRange(string $ip, string $range): bool
112112
{
113113
list($range, $netmask) = explode('/', $range, 2);
114114
$range_decimal = ip2long($range);
115-
$ip_decimal = ip2long($ip);
116-
$wildcard_decimal = pow(2, (32 - $netmask)) - 1;
115+
$ipDecimal = ip2long($ip);
116+
$wildcard_decimal = pow(2, (32 - (int)$netmask)) - 1;
117117
$netmask_decimal = ~ $wildcard_decimal;
118-
return (($ip_decimal & $netmask_decimal) == ($range_decimal & $netmask_decimal));
118+
return (($ipDecimal & $netmask_decimal) == ($range_decimal & $netmask_decimal));
119119
}
120120

121121
/**
122122
* Read HTTP reader with authorization token
123123
* If everything is ok, it return token. In other situations returns false and set errorMessage.
124124
*
125-
* @return string|boolean
125+
* @return string|null
126126
*/
127-
private function readAuthorizationToken()
127+
private function readAuthorizationToken(): ?string
128128
{
129129
if (!isset($_SERVER['HTTP_AUTHORIZATION'])) {
130130
$this->errorMessage = 'Authorization header HTTP_Authorization is not set';
131-
return false;
131+
return null;
132132
}
133133
$parts = explode(' ', $_SERVER['HTTP_AUTHORIZATION']);
134-
if (count($parts) != 2) {
134+
if (count($parts) !== 2) {
135135
$this->errorMessage = 'Authorization header contains invalid structure';
136-
return false;
136+
return null;
137137
}
138-
if (strtolower($parts[0]) != 'bearer') {
138+
if (strtolower($parts[0]) !== 'bearer') {
139139
$this->errorMessage = 'Authorization header doesn\'t contains bearer token';
140-
return false;
140+
return null;
141141
}
142142
return $parts[1];
143143
}

src/Authorization/NoAuthorization.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ class NoAuthorization implements ApiAuthorizationInterface
77
/**
88
* {@inheritdoc}
99
*/
10-
public function authorized()
10+
public function authorized(): bool
1111
{
1212
return true;
1313
}
1414

1515
/**
1616
* {@inheritdoc}
1717
*/
18-
public function getErrorMessage()
18+
public function getErrorMessage(): ?string
1919
{
20-
return false;
20+
return null;
2121
}
2222
}

src/Component/ApiConsoleControl.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Nette\Application\UI\Control;
66
use Nette\Application\UI\Form;
77
use Nette\Http\IRequest;
8+
use Nette\Utils\ArrayHash;
89
use Tomaj\Form\Renderer\BootstrapRenderer;
910
use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface;
1011
use Tomaj\NetteApi\Authorization\BearerTokenAuthorization;
@@ -32,13 +33,13 @@ public function __construct(IRequest $request, EndpointIdentifier $endpoint, Api
3233
$this->request = $request;
3334
}
3435

35-
public function render()
36+
public function render(): void
3637
{
3738
$this->getTemplate()->setFile(__DIR__ . '/console.latte');
3839
$this->getTemplate()->render();
3940
}
4041

41-
protected function createComponentConsoleForm()
42+
protected function createComponentConsoleForm(): Form
4243
{
4344
$form = new Form();
4445

@@ -124,12 +125,12 @@ protected function createComponentConsoleForm()
124125
return $form;
125126
}
126127

127-
private function getLabel(InputParam $param)
128+
private function getLabel(InputParam $param): string
128129
{
129130
return ucfirst(str_replace('_', ' ', $param->getKey()));
130131
}
131132

132-
private function getParamLabel(InputParam $param)
133+
private function getParamLabel(InputParam $param): string
133134
{
134135
$title = $this->getLabel($param);
135136
if ($param->isRequired()) {
@@ -139,7 +140,7 @@ private function getParamLabel(InputParam $param)
139140
return $title;
140141
}
141142

142-
public function formSucceeded($form, $values)
143+
public function formSucceeded(Form $form, ArrayHash $values): void
143144
{
144145
$url = $values['api_url'];
145146

src/Component/ApiListingControl.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Tomaj\NetteApi\Component;
44

55
use Nette\Application\UI\Control;
6-
use Nette\ComponentModel\IContainer;
76
use Tomaj\NetteApi\ApiDecider;
87
use Closure;
98
use Exception;
@@ -17,25 +16,25 @@ class ApiListingControl extends Control
1716
/** @var Closure|null */
1817
private $clickCallback;
1918

20-
public function __construct(IContainer $parent, $name, ApiDecider $apiDecider)
19+
public function __construct(ApiDecider $apiDecider)
2120
{
2221
$this->apiDecider = $apiDecider;
2322
}
2423

25-
public function onClick(Closure $callback)
24+
public function onClick(Closure $callback): void
2625
{
2726
$this->clickCallback = $callback;
2827
}
2928

30-
public function render()
29+
public function render(): void
3130
{
3231
$apis = $this->apiDecider->getApis();
3332
$this->getTemplate()->add('apis', $this->groupApis($apis));
3433
$this->getTemplate()->setFile(__DIR__ . '/api_listing.latte');
3534
$this->getTemplate()->render();
3635
}
3736

38-
public function handleSelect($method, $version, $package, $apiAction)
37+
public function handleSelect(string $method, int $version, string $package, ?string $apiAction = null)
3938
{
4039
if (!$this->clickCallback) {
4140
throw new Exception('You have to set onClick callback to component!');
@@ -48,7 +47,7 @@ public function handleSelect($method, $version, $package, $apiAction)
4847
* @param Api[] $handlers
4948
* @return array
5049
*/
51-
private function groupApis($handlers)
50+
private function groupApis(array $handlers): array
5251
{
5352
$versionHandlers = [];
5453
foreach ($handlers as $handler) {

src/EndpointIdentifier.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,38 @@ class EndpointIdentifier implements EndpointInterface
1212

1313
private $apiAction;
1414

15-
public function __construct($method, $version, $package, $apiAction = '')
15+
public function __construct(string $method, int $version, string $package, ?string $apiAction = null)
1616
{
1717
$this->method = strtoupper($method);
1818
$this->version = $version;
1919
$this->package = $package;
2020
$this->apiAction = $apiAction;
2121
}
2222

23-
public function getMethod()
23+
public function getMethod(): string
2424
{
2525
return $this->method;
2626
}
2727

28-
public function getVersion()
28+
public function getVersion(): int
2929
{
3030
return $this->version;
3131
}
3232

33-
public function getPackage()
33+
public function getPackage(): string
3434
{
3535
return $this->package;
3636
}
3737

38-
public function getApiAction()
38+
public function getApiAction(): ?string
3939
{
40-
if ($this->apiAction == '') {
40+
if ($this->apiAction === '') {
4141
return null;
4242
}
4343
return $this->apiAction;
4444
}
4545

46-
public function getUrl()
46+
public function getUrl(): string
4747
{
4848
return "v{$this->version}/{$this->package}/{$this->apiAction}";
4949
}

src/EndpointInterface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
interface EndpointInterface
66
{
7-
public function getMethod();
7+
public function getMethod(): string;
88

9-
public function getVersion();
9+
public function getVersion(): int;
1010

11-
public function getPackage();
11+
public function getPackage(): string;
1212

13-
public function getApiAction();
13+
public function getApiAction(): ?string;
1414

15-
public function getUrl();
15+
public function getUrl(): string;
1616
}

src/Handlers/AlwaysOkHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
namespace Tomaj\NetteApi\Handlers;
44

55
use Tomaj\NetteApi\Response\JsonApiResponse;
6+
use Tomaj\NetteApi\Response\ResponseInterface;
67

78
class AlwaysOkHandler extends BaseHandler
89
{
910
/**
1011
* {@inheritdoc}
1112
*/
12-
public function handle($params)
13+
public function handle(array $params): ResponseInterface
1314
{
1415
return new JsonApiResponse(200, ['status' => 'ok']);
1516
}

0 commit comments

Comments
 (0)