Skip to content

Commit c804956

Browse files
committed
Adding type safety to any other class
1 parent 94e6024 commit c804956

32 files changed

+158
-151
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ DDNS(new API(__DIR__ . "/configurations", ".env-custom"), file_get_contents("/my
128128
### v1.1.4: Type safety
129129

130130
- Added type safety to the `DDNS.Helper.API.dnsserver.ente.php` class
131+
- Added type safety to all other classes
132+
- Fixed incorrect/Added API docs
131133

132134
### v1.1.3: Shell safe
133135

src/API.dnsserver.ente.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
class API {
2020

21-
private $protocol;
21+
private string $protocol;
2222
private $admin;
2323
private $allowed;
2424
private $apps;
@@ -32,20 +32,20 @@ class API {
3232
private $zones;
3333
private $ddns;
3434
private $log;
35-
private $conf;
36-
private $path;
37-
private $fullPath;
38-
private $env = [];
35+
private string $conf;
36+
private string $path;
37+
private string $fullPath;
38+
private array $env = [];
3939
const PREFIX_GET = "&token=";
4040
const PREFIX_POST = "?token=";
4141

42-
public function __construct($confPath, $name = null){
42+
public function __construct(string $confPath, ?string $name = null){
4343
$this->loader();
4444
$this->loadConf($confPath, $name);
4545
$this->setProtocol();
4646
}
4747

48-
private function setProtocol(){
48+
private function setProtocol(): void{
4949
if($this->env["USE_HTTPS"] == "true"){
5050
$this->protocol = "https";
5151
} else {
@@ -59,7 +59,7 @@ private function setProtocol(){
5959
* @param mixed $name The name of the .env file. (optional)
6060
* @return void
6161
*/
62-
private function loadConf($path, $name = null){
62+
private function loadConf(string $path, ?string $name = null): void{
6363
$this->conf = $name ?? ".env";
6464
if(!isset($_SERVER)){
6565
$path = __DIR__;
@@ -90,7 +90,7 @@ private function loadConf($path, $name = null){
9090

9191
}
9292

93-
public function loader(){
93+
public function loader(): void{
9494
require_once __DIR__ . "/endpoints/admin/Admin.php";
9595
require_once __DIR__ . "/endpoints/allowed/Allowed.php";
9696
require_once __DIR__ . "/endpoints/apps/Apps.php";
@@ -111,12 +111,12 @@ public function loader(){
111111
* `sendCall()` - Send a request to the Technitium API.
112112
* @param array $data The data to send to the API
113113
* @param string $endpoint The endpoint to send the data to, e.g. "admin/users/list"
114-
* @param mixed $method The HTTP method to use. Default is "POST".
115-
* @param mixed $skip Set to `true` to skip the authentication URI append.
116-
* @param mixed $bypass Set to `true` to bypass the endpoint check allowing to access not (yet) implemented methods.
114+
* @param string $method The HTTP method to use. Default is "POST".
115+
* @param bool $skip Set to `true` to skip the authentication URI append.
116+
* @param bool $bypass Set to `true` to bypass the endpoint check allowing to access not (yet) implemented methods.
117117
* @return array Returns the response from the API or an error (["status" => "error"]) as an array.
118118
*/
119-
public function sendCall($data, $endpoint, $method = "POST", $skip = false, $bypass = false){
119+
public function sendCall(array $data, string $endpoint, string $method = "POST", bool $skip = false, bool $bypass = false): array{
120120
$c = curl_init();
121121
$endpoint = $this->prepareEndpoint($endpoint, $bypass);
122122
if($this->env["USE_POST"]){
@@ -161,11 +161,11 @@ public function sendCall($data, $endpoint, $method = "POST", $skip = false, $byp
161161

162162
/**
163163
* `appendAuth()` - Append the authentication token to the URI.
164-
* @param mixed $m The HTTP method to use. Default is "POST".
165-
* @param mixed $skip Set to `true` to skip the authentication URI append, allowing the use of `API::getPermanentToken()`.
164+
* @param string $m The HTTP method to use. Default is "POST".
165+
* @param bool $skip Set to `true` to skip the authentication URI append, allowing the use of `API::getPermanentToken()`.
166166
* @return string Returns the authentication token URI string or an empty string.
167167
*/
168-
private function appendAuth($m = "POST", $skip = false){
168+
private function appendAuth(string $m = "POST", bool $skip = false): string{
169169
$this->loadConf($this->path, $this->conf);
170170
$authAppend = null;
171171
if($skip){
@@ -206,7 +206,7 @@ private function appendAuth($m = "POST", $skip = false){
206206
* This function is called when the token is not found in the .env file.
207207
* @return bool Returns `true` if the token was successfully written to the .env file.
208208
*/
209-
private function getPermanentToken(){
209+
private function getPermanentToken(): bool{
210210
Log::error_rep("Getting permanent token... | .env: " . $this->fullPath);
211211
$response = $this->sendCall([
212212
"user" => $this->env["USERNAME"],
@@ -234,10 +234,10 @@ private function getPermanentToken(){
234234
/**
235235
* `checkResponse()` - Check if the Technitium API response is valid.
236236
* If the response status contains either "error" or "invalid-token" it is considered invalid.
237-
* @param mixed $response The response returned by `API::sendCall()` function.
237+
* @param string $response The response returned by `API::sendCall()` function.
238238
* @return bool Returns `true` if the response is valid, otherwise `false`.
239239
*/
240-
private function checkResponse(string $response){
240+
private function checkResponse(string $response): bool{
241241
if(is_null($response)){
242242
return false;
243243
} else {
@@ -248,11 +248,11 @@ private function checkResponse(string $response){
248248

249249
/**
250250
* `prepareEndpoint()` - Generates the API URI for the endpoint in question.
251-
* @param mixed $endpoint The endpoint to generate the URI for.
252-
* @param mixed $bypass Set to `true` to bypass the endpoint check allowing to access not (yet) implemented methods.
251+
* @param string $endpoint The endpoint to generate the URI for.
252+
* @param bool $bypass Set to `true` to bypass the endpoint check allowing to access not (yet) implemented methods.
253253
* @return bool|string Returns the URI as string or `false` if the endpoint is not implemented.
254254
*/
255-
private function prepareEndpoint($endpoint, $bypass = false){
255+
private function prepareEndpoint(string $endpoint, bool $bypass = false): bool|string{
256256
if($bypass){
257257
return $this->protocol . "://" . $this->env["API_URL"] . "/api/" . $endpoint;
258258
}

src/endpoints/admin/Admin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ class admin {
2020

2121
private $groups;
2222

23-
public function __construct($api){
23+
public function __construct(\Technitium\DNSServer\API\API $api){
2424
$this->API = $api;
2525
$this->eloader();
2626
}
2727

28-
private function eloader(){
28+
private function eloader(): void{
2929
require_once __DIR__ . "/Groups.admin.php";
3030
require_once __DIR__ . "/Logs.admin.php";
3131
require_once __DIR__ . "/Permissions.admin.php";

src/endpoints/admin/Groups.admin.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class groups {
1010
public $API;
1111

12-
public function __construct($api){
12+
public function __construct(\Technitium\DNSServer\API\API $api){
1313
$this->API = $api;
1414
}
1515

@@ -18,7 +18,7 @@ public function __construct($api){
1818
* - `PERMISSIONS`: `Administration:View`
1919
* @return array|bool Returns the result array or an error array
2020
*/
21-
public function list(){
21+
public function list(): array|bool{
2222
$response = $this->API->sendCall([], "admin/groups/list");
2323
if($response["status"] == "ok"){
2424
return $response["response"];
@@ -34,7 +34,7 @@ public function list(){
3434
* @param bool $includeUsers Set to `true` to include the list of users in the group.
3535
* @return array|bool Returns the result array or false if the group was not found.
3636
*/
37-
public function get(string $name, bool $includeUsers = true){
37+
public function get(string $name, bool $includeUsers = true): array|bool{
3838
$includeUsers = $includeUsers ? "true" : "false";
3939
$response = $this->API->sendCall(["group" => $name, "includeUsers" => "true"], "admin/groups/get");
4040
if($response["response"]["name"] == $name){
@@ -50,7 +50,7 @@ public function get(string $name, bool $includeUsers = true){
5050
* @param string $description The description text for the group (optional).
5151
* @return bool Returns `true` if the group was created successfully, otherwise `false`.
5252
*/
53-
public function create(string $name, string $description = null){
53+
public function create(string $name, string $description = null): bool{
5454
$response = $this->API->sendCall(["group" => $name, "description" => $description], "admin/groups/create");
5555
return $response["response"]["name"] == $name;
5656
}
@@ -64,7 +64,7 @@ public function create(string $name, string $description = null){
6464
* @param array $members An array of users to add to the group (optional).
6565
* @return array|bool Returns the result array or `false` if the group was not found.
6666
*/
67-
public function set(string $name, string $newName = null, string $description = null, array $members = []){
67+
public function set(string $name, string $newName = null, string $description = null, array $members = []): array|bool{
6868
$response = $this->API->sendCall(["group" => $name, "newGroup" => $newName, "description" => $description, "members" => implode(",", $members)], "admin/groups/set");
6969
if($response["response"]["name"] == $name){
7070
return $response["response"];
@@ -78,7 +78,7 @@ public function set(string $name, string $newName = null, string $description =
7878
* @param string $name The name of the group to delete.
7979
* @return bool Returns the result array or an error array.
8080
*/
81-
public function delete(string $name){
81+
public function delete(string $name): bool{
8282
$response = $this->API->sendCall(["group" => $name], "admin/groups/delete");
8383
return $response["status"] == "ok";
8484
}

src/endpoints/admin/Logs.admin.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
class logs {
55
public $API;
66

7-
public function __construct($api){
7+
public function __construct(\Technitium\DNSServer\API\API $api){
88
$this->API = $api;
99
}
1010

1111
/**
1212
* `list()` - Returns a list of all log files.
1313
* @return array|bool Returns the result array or `false` if the group was not found.
1414
*/
15-
public function list(){
15+
public function list(): array|bool{
1616
$response = $this->API->sendCall([], "admin/logs/list");
1717
if($response["status"] == "ok"){
1818
return $response["response"];
@@ -39,7 +39,7 @@ public function list(){
3939
* - `qclass` (optional): The DNS class (QCLASS) in the request question section to filter the logs.
4040
* @return array Returns the result array or `false` if the group was not found.
4141
*/
42-
public function query($data){
42+
public function query($data): array{
4343
return $this->API->sendCall($data, "admin/logs/query");
4444
}
4545

@@ -49,7 +49,7 @@ public function query($data){
4949
* @param int $limit The number of mb to limit the download to.
5050
* @return array|bool Returns the result array or `false` if the group was not found.
5151
*/
52-
public function download(string $fileName, int $limit = 0){
52+
public function download(string $fileName, int $limit = 0): array|bool{
5353
$response = $this->API->sendCall(["fileName" => $fileName, "limit" => $limit], "admin/logs/download");
5454
if($response["status"] == "ok"){
5555
return $response["response"];
@@ -63,7 +63,7 @@ public function download(string $fileName, int $limit = 0){
6363
* @param string $log The name of the file to delete.
6464
* @return bool Returns `true` if the file was deleted or `false` if the file was not found.
6565
*/
66-
public function delete(string $log){
66+
public function delete(string $log): bool{
6767
$response = $this->API->sendCall(["log" => $log], "admin/logs/delete");
6868
return $response["status"] == "ok";
6969
}
@@ -72,7 +72,7 @@ public function delete(string $log){
7272
* `deleteAll()` - Deletes all log files.
7373
* @return bool Returns `true` if the files were deleted or `false` if the files were not found.
7474
*/
75-
public function deleteAll(){
75+
public function deleteAll(): bool{
7676
$response = $this->API->sendCall([], "admin/logs/deleteAll");
7777
return $response["status"] == "ok";
7878
}

src/endpoints/admin/Permissions.admin.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class permissions {
99
public $API;
1010

11-
public function __construct($api){
11+
public function __construct(\Technitium\DNSServer\API\API $api){
1212
$this->API = $api;
1313
}
1414

@@ -17,7 +17,7 @@ public function __construct($api){
1717
* - `PERMISSIONS`: `Administration:View`
1818
* @return array|bool Returns the result array or `false` if the group was not found.
1919
*/
20-
public function list(){
20+
public function list(): array|bool{
2121
$response = $this->API->sendCall([], "admin/permissions/list");
2222
if($response["status"] == "ok"){
2323
return $response["response"];
@@ -32,7 +32,7 @@ public function list(){
3232
* @param bool $includeUsersAndGroups Set to `true` to include the list of users and groups with permissions. (optional)
3333
* @return array|bool Returns the result array or `false` if the group was not found.
3434
*/
35-
public function get(string $section, bool $includeUsersAndGroups = false){
35+
public function get(string $section, bool $includeUsersAndGroups = false): array|bool{
3636
if($includeUsersAndGroups){ $includeUsersAndGroups = "true"; } else { $includeUsersAndGroups = "false"; }
3737
$response = $this->API->sendCall(["section" => $section, "includeUsersAndGroups" => $includeUsersAndGroups], "admin/permissions/get");
3838
if($response["status"] == "ok"){
@@ -49,7 +49,7 @@ public function get(string $section, bool $includeUsersAndGroups = false){
4949
* @param string $groupPermissions The permissions to set for groups (pipe seperated table data with each row containing group names and bools for view, modify and delete permission, e.g. `Administrators|true|true|false|Example Group|true|false|false`). (optional)
5050
* @return array|bool Returns the result array or `false` if the group was not found.
5151
*/
52-
public function set(string $section, string $userPermissions = null, string $groupPermissions = null){
52+
public function set(string $section, string $userPermissions = null, string $groupPermissions = null): array|bool{
5353
$response = $this->API->sendCall(["section" => $section, "userPermissions" => $userPermissions, "groupPermissions" => $groupPermissions], "admin/permissions/set");
5454
if($response["response"]["section"] == $section){
5555
return $response["response"];

src/endpoints/admin/Sessions.admin.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class sessions {
55

66
public $API;
77

8-
public function __construct($api){
8+
public function __construct(\Technitium\DNSServer\API\API $api){
99
$this->API = $api;
1010
}
1111

@@ -16,7 +16,7 @@ public function __construct($api){
1616
* @param string $tokenName The name of the token to create.
1717
* @return array|bool Returns the result array or `false` if the request failed.
1818
*/
19-
public function createToken(string $username, string $tokenName){
19+
public function createToken(string $username, string $tokenName): array|bool{
2020
$response = $this->API->sendCall(["user" => $username, "tokenName" => $tokenName], "admin/sessions/create", "POST");
2121
if($response["status"] == "ok"){
2222
return $response["response"];
@@ -31,7 +31,7 @@ public function createToken(string $username, string $tokenName){
3131
* @param string $partialToken The partial token to delete (Can be obtained from `list()`).
3232
* @return bool Returns `true` if the request was successful, `false` otherwise.
3333
*/
34-
public function delete(string $partialToken){
34+
public function delete(string $partialToken): bool{
3535
$response = $this->API->sendCall(["partialToken" => $partialToken], "admin/sessions/delete", "POST");
3636
return $response["status"] == "ok";
3737
}
@@ -41,7 +41,7 @@ public function delete(string $partialToken){
4141
* - `PERMISSIONS`: `Administration:View`
4242
* @return array|bool Returns the list of session tokens or `false` if the request failed.
4343
*/
44-
public function list(){
44+
public function list(): array|bool{
4545
$response = $this->API->sendCall([], "admin/sessions/list", "GET");
4646
if($response["status"] == "ok"){
4747
return $response["response"];

src/endpoints/admin/Users.admin.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class users {
55
public $API;
66

7-
public function __construct($api){
7+
public function __construct(\Technitium\DNSServer\API\API $api){
88
$this->API = $api;
99
}
1010

@@ -13,7 +13,7 @@ public function __construct($api){
1313
* - `PERMISSIONS`: `Administration:View`
1414
* @return array|bool Returns the list of users or `false` if the request failed.
1515
*/
16-
public function list(){
16+
public function list(): array|bool{
1717
$response = $this->API->sendCall([], "admin/users/list", "GET");
1818
if($response["status"] == "ok"){
1919
return $response["response"];
@@ -30,7 +30,7 @@ public function list(){
3030
* @param string $displayName The display name of the user to create.
3131
* @return bool Returns `true` if the request was successful, `false` otherwise.
3232
*/
33-
public function create(string $username, string $password, string $displayName = ""){
33+
public function create(string $username, string $password, string $displayName = ""): bool{
3434
$response = $this->API->sendCall(["user" => $username, "pass" => $password, "displayName" => $displayName], "admin/users/create", "POST");
3535
return $response["status"] == "ok";
3636
}
@@ -48,7 +48,7 @@ public function create(string $username, string $password, string $displayName =
4848
* @param array $memberOfGroups The groups the user should be a member of.
4949
* @return bool Returns `true` if the request was successful, `false` otherwise.
5050
*/
51-
public function set(string $username, string $displayName = "", string $newUser = "", bool $disabled = false, int $sessionTimeoutSeconds = 0, string $newPassword = "", int $iterations = 5, array $memberOfGroups = []){
51+
public function set(string $username, string $displayName = "", string $newUser = "", bool $disabled = false, int $sessionTimeoutSeconds = 0, string $newPassword = "", int $iterations = 5, array $memberOfGroups = []): bool{
5252
if($disabled){$disabled = "true";}else{$disabled = "false";}
5353
$response = $this->API->sendCall(["user" => $username, "displayName" => $displayName, "newUser" => $newUser, "disabled" => $disabled, "sessionTimeoutSeconds" => $sessionTimeoutSeconds, "newPass" => $newPassword, "iterations" => $iterations, "memberOfGroups" => implode(",", $memberOfGroups)], "admin/users/set", "POST");
5454
return $response["status"] == "ok";
@@ -60,7 +60,7 @@ public function set(string $username, string $displayName = "", string $newUser
6060
* @param string $username The username of the user to delete.
6161
* @return bool Returns `true` if the request was successful, `false` otherwise.
6262
*/
63-
public function delete(string $username){
63+
public function delete(string $username): bool{
6464
$response = $this->API->sendCall(["user" => $username], "admin/users/delete", "POST");
6565
return $response["status"] == "ok";
6666
}
@@ -72,7 +72,7 @@ public function delete(string $username){
7272
* @param bool $includeGroups Whether to include the groups the user is a member of.
7373
* @return array|bool Returns the user details or `false` if the request failed.
7474
*/
75-
public function get(string $username, bool $includeGroups = false){
75+
public function get(string $username, bool $includeGroups = false): array|bool{
7676
if($includeGroups){$includeGroups = "true";} else {$includeGroups = "false";}
7777
$response = $this->API->sendCall(["user" => $username, "includeGroups" => $includeGroups], "admin/users/get", "GET");
7878
if($response["status"] == "ok"){

0 commit comments

Comments
 (0)