Skip to content

Commit

Permalink
chore: set phpstan to level 6 (#1)
Browse files Browse the repository at this point in the history
* chore: set phpstan to level 6

* fix: phpstan level 6 errors
  • Loading branch information
dkaser authored Oct 15, 2024
1 parent 75a068e commit c949996
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 36 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
parameters:
level: 5
level: 6
paths:
- src
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

function getPluginConfig() {
/**
* @return array<string, mixed>
*/
function getPluginConfig() : array
{
$config_file = '/boot/config/plugins/tailscale/tailscale.cfg';
$defaults_file = '/usr/local/emhttp/plugins/tailscale/settings.json';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function applyGRO()
function applyGRO() : void
{
$ip_route = json_decode(implode(run_command('ip -j route get 8.8.8.8')), true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function ip4_in_network($ip, $network)
function ip4_in_network(string $ip, string $network) : bool
{
if (strpos($network, '/') === false) {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function logmsg($message, $priority = LOG_INFO)
function logmsg(string $message) : void
{
$timestamp = date('Y/m/d H:i:s');
$filename = basename($_SERVER['PHP_SELF']);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

function run_command($command, $alwaysShow = false, $show = true)
/**
* @return array<string>
*/
function run_command(string $command, bool $alwaysShow = false, bool $show = true) : array
{
$output = array();
$retval = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php

function getTailscaleStatus()
function getTailscaleStatus() : object
{
exec("tailscale status --json", $out_status);
return json_decode(implode($out_status));
}

function getTailscalePrefs()
function getTailscalePrefs() : object
{
exec("tailscale debug prefs", $out_prefs);
return json_decode(implode($out_prefs));
}

function getTailscaleLock()
function getTailscaleLock() : object
{
exec("tailscale lock status -json=true", $out_status);
return json_decode(implode($out_status));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function _tr($message)
function _tr(string $message) : string
{
global $tailscale_lang;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function refreshWebGuiCert($restartIfChanged = true)
function refreshWebGuiCert(bool $restartIfChanged = true) : void
{
$status = getTailscaleStatus();

Expand Down
7 changes: 5 additions & 2 deletions src/usr/local/emhttp/plugins/tailscale/include/daily/ping.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

$endpoint = "https://plugin-usage.edacerton.win/";

function download_url($url)
function download_url(string $url) : string
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
Expand All @@ -21,7 +21,10 @@ function download_url($url)
return $out;
}

function send_usage($url, $content)
/**
* @param array<mixed> $content
*/
function send_usage(string $url, array $content) : int
{
$body = json_encode($content);
$token = download_url($url . '?connect');
Expand Down
5 changes: 2 additions & 3 deletions src/usr/local/emhttp/plugins/tailscale/include/get_log.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function getLog($log, $maxLines)
function getLog(string $log, int $max) : void
{
$allowed_files = ["/var/log/tailscale.log", "/var/log/tailscale-utils.log"];

Expand All @@ -13,7 +13,6 @@ function getLog($log, $maxLines)
return;
}

$max = intval($maxLines);
$lines = array_reverse(array_slice(file($log), -$max));

foreach ($lines as $line) {
Expand All @@ -24,7 +23,7 @@ function getLog($log, $maxLines)
ini_set('memory_limit', '512M'); // Increase memory limit

try {
getLog($_POST['log'], $_POST['max']);
getLog($_POST['log'], intval($_POST['max']));
} catch (Throwable $e) {
echo '<span class="text">', htmlspecialchars($e->getMessage()), "</span>";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

function getTailscaleNetbiosWarning($var)
/**
* @param array<mixed> $var
*/
function getTailscaleNetbiosWarning(array $var) : string
{
if (($var['USE_NETBIOS'] == "yes") && ($var['shareSMBEnabled'] != "no")) {
return "<span class='warn' style='text-align: center; font-size: 1.4em; font-weight: bold;'>" . _tr("warnings.netbios") . "</span>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
if ($in_array != $tailscale_config["INCLUDE_INTERFACE"]) {
if ($tailscale_config["INCLUDE_INTERFACE"]) {
$include_array[] = $ifname;
logmsg("{$ifname} added to include_interfaces", LOG_NOTICE);
logmsg("{$ifname} added to include_interfaces");
} else {
$include_array = array_diff($include_array, [$ifname]);
logmsg("{$ifname} removed from include_interfaces", LOG_NOTICE);
logmsg("{$ifname} removed from include_interfaces");
}
$write_file = true;
}
Expand All @@ -42,5 +42,5 @@
END;

file_put_contents($network_extra_file, $file);
logmsg("Updated network-extra.cfg", LOG_NOTICE);
logmsg("Updated network-extra.cfg");
}
21 changes: 12 additions & 9 deletions src/usr/local/emhttp/plugins/tailscale/include/tailscale-lock.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

function getTailscaleLockEnabled($lock)
function getTailscaleLockEnabled(object $lock) : bool
{
return $lock->Enabled;
}

function getTailscaleLockSigned($lock)
function getTailscaleLockSigned(object $lock) : bool
{
if ( ! getTailscaleLockEnabled($lock)) {
return false;
Expand All @@ -14,25 +14,25 @@ function getTailscaleLockSigned($lock)
return $lock->NodeKeySigned;
}

function getTailscaleLockNodekey($lock)
function getTailscaleLockNodekey(object $lock) : string
{
if ( ! getTailscaleLockEnabled($lock)) {
return false;
return "";
}

return $lock->NodeKey;
}

function getTailscaleLockPubkey($lock)
function getTailscaleLockPubkey(object $lock) : string
{
if ( ! getTailscaleLockEnabled($lock)) {
return false;
return "";
}

return $lock->PublicKey;
}

function getTailscaleLockSigning($lock)
function getTailscaleLockSigning(object $lock) : bool
{
if ( ! getTailscaleLockSigned($lock)) {
return false;
Expand All @@ -50,7 +50,10 @@ function getTailscaleLockSigning($lock)
return $isTrusted;
}

function getTailscaleLockPending($lock)
/**
* @return array<string, string>
*/
function getTailscaleLockPending(object $lock) : array
{
if ( ! getTailscaleLockSigning($lock)) {
return array();
Expand All @@ -65,7 +68,7 @@ function getTailscaleLockPending($lock)
return $pending;
}

function getTailscaleLockWarning($lock)
function getTailscaleLockWarning(object $lock) : string
{
if (getTailscaleLockEnabled($lock) && ( ! getTailscaleLockSigned($lock))) {
return "<span class='error' style='text-align: center; font-size: 1.4em; font-weight: bold;'>" . _tr("warnings.lock") . "</span>";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function apply_flag($setting, $flag)
function apply_flag(string $setting, string $flag) : void
{
global $tailscale_config;

Expand Down
10 changes: 5 additions & 5 deletions src/usr/local/emhttp/plugins/tailscale/include/webgui-info.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

function printRow($title, $value)
function printRow(string $title, string $value) : string
{
return "<tr><td>{$title}</td><td>{$value}</td></tr>" . PHP_EOL;
}

function printDash($title, $value)
function printDash(string $title, string $value) : string
{
return "<tr><td><span class='w26'>{$title}</span>{$value}</td></tr>" . PHP_EOL;
}

function getStatusInfo($status, $prefs, $lock)
function getStatusInfo(object $status, object $prefs, object $lock) : string
{
$tsVersion = isset($status->Version) ? $status->Version : _tr("unknown");
$keyExpiration = isset($status->Self->KeyExpiry) ? $status->Self->KeyExpiry : _tr("disabled");
Expand Down Expand Up @@ -48,7 +48,7 @@ function getStatusInfo($status, $prefs, $lock)
return $output;
}

function getConnectionInfo($status, $prefs)
function getConnectionInfo(object $status, object $prefs) : string
{
$hostName = isset($status->Self->HostName) ? $status->Self->HostName : _tr("unknown");
$dnsName = isset($status->Self->DNSName) ? $status->Self->DNSName : _tr("unknown");
Expand All @@ -70,7 +70,7 @@ function getConnectionInfo($status, $prefs)
return $output;
}

function getDashboardInfo($status)
function getDashboardInfo(object $status) : string
{
$hostName = isset($status->Self->HostName) ? $status->Self->HostName : _tr("Unknown");
$dnsName = isset($status->Self->DNSName) ? $status->Self->DNSName : _tr("Unknown");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

function getKeyExpirationWarning($status)
function getKeyExpirationWarning(object $status) : string
{
if (isset($status->Self->KeyExpiry)) {
$expiryTime = new DateTime($status->Self->KeyExpiry);
Expand Down

0 comments on commit c949996

Please sign in to comment.