Address PHPStan warnings in 'classes/diskcache.php'.

This commit is contained in:
wn_ 2021-11-11 22:07:32 +00:00
parent 50997df57a
commit 2d5603b196
2 changed files with 52 additions and 29 deletions

View File

@ -1,9 +1,13 @@
<?php <?php
class DiskCache { class DiskCache {
private $dir; private string $dir;
// https://stackoverflow.com/a/53662733 /**
private $mimeMap = [ * https://stackoverflow.com/a/53662733
*
* @var array<string, string>
*/
private array $mimeMap = [
'video/3gpp2' => '3g2', 'video/3gpp2' => '3g2',
'video/3gp' => '3gp', 'video/3gp' => '3gp',
'video/3gpp' => '3gp', 'video/3gpp' => '3gp',
@ -190,21 +194,22 @@ class DiskCache {
'text/x-scriptzsh' => 'zsh' 'text/x-scriptzsh' => 'zsh'
]; ];
public function __construct($dir) { public function __construct(string $dir) {
$this->dir = Config::get(Config::CACHE_DIR) . "/" . basename(clean($dir)); $this->dir = Config::get(Config::CACHE_DIR) . "/" . basename(clean($dir));
} }
public function get_dir() { public function get_dir(): string {
return $this->dir; return $this->dir;
} }
public function make_dir() { public function make_dir(): bool {
if (!is_dir($this->dir)) { if (!is_dir($this->dir)) {
return mkdir($this->dir); return mkdir($this->dir);
} }
return false;
} }
public function is_writable($filename = "") { public function is_writable(?string $filename = null): bool {
if ($filename) { if ($filename) {
if (file_exists($this->get_full_path($filename))) if (file_exists($this->get_full_path($filename)))
return is_writable($this->get_full_path($filename)); return is_writable($this->get_full_path($filename));
@ -215,44 +220,55 @@ class DiskCache {
} }
} }
public function exists($filename) { public function exists(string $filename): bool {
return file_exists($this->get_full_path($filename)); return file_exists($this->get_full_path($filename));
} }
public function get_size($filename) { /**
* @return int|false -1 if the file doesn't exist, false if an error occurred, size in bytes otherwise
*/
public function get_size(string $filename) {
if ($this->exists($filename)) if ($this->exists($filename))
return filesize($this->get_full_path($filename)); return filesize($this->get_full_path($filename));
else else
return -1; return -1;
} }
public function get_full_path($filename) { public function get_full_path(string $filename): string {
return $this->dir . "/" . basename(clean($filename)); return $this->dir . "/" . basename(clean($filename));
} }
public function put($filename, $data) { /**
* @param mixed $data
*
* @return int|false Bytes written or false if an error occurred.
*/
public function put(string $filename, $data) {
return file_put_contents($this->get_full_path($filename), $data); return file_put_contents($this->get_full_path($filename), $data);
} }
public function touch($filename) { public function touch(string $filename): bool {
return touch($this->get_full_path($filename)); return touch($this->get_full_path($filename));
} }
public function get($filename) { public function get(string $filename): ?string {
if ($this->exists($filename)) if ($this->exists($filename))
return file_get_contents($this->get_full_path($filename)); return file_get_contents($this->get_full_path($filename));
else else
return null; return null;
} }
public function get_mime_type($filename) { /**
* @return false|null|string false if detection failed, null if the file doesn't exist, string mime content type otherwise
*/
public function get_mime_type(string $filename) {
if ($this->exists($filename)) if ($this->exists($filename))
return mime_content_type($this->get_full_path($filename)); return mime_content_type($this->get_full_path($filename));
else else
return null; return null;
} }
public function get_fake_extension($filename) { public function get_fake_extension(string $filename): string {
$mimetype = $this->get_mime_type($filename); $mimetype = $this->get_mime_type($filename);
if ($mimetype) if ($mimetype)
@ -261,7 +277,10 @@ class DiskCache {
return ""; return "";
} }
public function send($filename) { /**
* @return bool|int false if the file doesn't exist (or unreadable) or isn't audio/video, true if a plugin handled, otherwise int of bytes sent
*/
public function send(string $filename) {
$fake_extension = $this->get_fake_extension($filename); $fake_extension = $this->get_fake_extension($filename);
if ($fake_extension) if ($fake_extension)
@ -272,7 +291,7 @@ class DiskCache {
return $this->send_local_file($this->get_full_path($filename)); return $this->send_local_file($this->get_full_path($filename));
} }
public function get_url($filename) { public function get_url(string $filename): string {
return Config::get_self_url() . "/public.php?op=cached&file=" . basename($this->dir) . "/" . basename($filename); return Config::get_self_url() . "/public.php?op=cached&file=" . basename($this->dir) . "/" . basename($filename);
} }
@ -280,8 +299,7 @@ class DiskCache {
// this is called separately after sanitize() and plugin render article hooks to allow // this is called separately after sanitize() and plugin render article hooks to allow
// plugins work on original source URLs used before caching // plugins work on original source URLs used before caching
// NOTE: URLs should be already absolutized because this is called after sanitize() // NOTE: URLs should be already absolutized because this is called after sanitize()
static public function rewrite_urls($str) static public function rewrite_urls(string $str): string {
{
$res = trim($str); $res = trim($str);
if (!$res) return ''; if (!$res) return '';
@ -338,7 +356,7 @@ class DiskCache {
return $res; return $res;
} }
static function expire() { static function expire(): void {
$dirs = array_filter(glob(Config::get(Config::CACHE_DIR) . "/*"), "is_dir"); $dirs = array_filter(glob(Config::get(Config::CACHE_DIR) . "/*"), "is_dir");
foreach ($dirs as $cache_dir) { foreach ($dirs as $cache_dir) {
@ -362,14 +380,19 @@ class DiskCache {
} }
} }
/* this is essentially a wrapper for readfile() which allows plugins to hook /* */
output with httpd-specific "fast" implementation i.e. X-Sendfile or whatever else /**
* this is essentially a wrapper for readfile() which allows plugins to hook
hook function should return true if request was handled (or at least attempted to) * output with httpd-specific "fast" implementation i.e. X-Sendfile or whatever else
*
note that this can be called without user context so the plugin to handle this * hook function should return true if request was handled (or at least attempted to)
should be loaded systemwide in config.php */ *
function send_local_file($filename) { * note that this can be called without user context so the plugin to handle this
* should be loaded systemwide in config.php
*
* @return bool|int false if the file doesn't exist (or unreadable) or isn't audio/video, true if a plugin handled, otherwise int of bytes sent
*/
function send_local_file(string $filename) {
if (file_exists($filename)) { if (file_exists($filename)) {
if (is_writable($filename)) touch($filename); if (is_writable($filename)) touch($filename);

View File

@ -205,7 +205,7 @@ class UrlHelper {
/** /**
* @param array<string, bool|int|string>|string $options * @param array<string, bool|int|string>|string $options
* @return bool|string false if something went wrong, otherwise string contents * @return false|string false if something went wrong, otherwise string contents
*/ */
// TODO: max_size currently only works for CURL transfers // TODO: max_size currently only works for CURL transfers
// TODO: multiple-argument way is deprecated, first parameter is a hash now // TODO: multiple-argument way is deprecated, first parameter is a hash now