Refactor TempFile class field types and improve filename validation

This commit is contained in:
netkas 2024-09-29 21:15:01 -04:00
parent fbba022431
commit 2bf304c5ef

View file

@ -1,7 +1,5 @@
<?php <?php
/** @noinspection PhpMissingFieldTypeInspection */
namespace TempFile; namespace TempFile;
use Exception; use Exception;
@ -17,24 +15,24 @@
* *
* @var string[] * @var string[]
*/ */
private static $temporary_files = []; private static array $temporary_files = [];
/** /**
* Indicates whether the shutdown handler has been registered * Indicates whether the shutdown handler has been registered
* *
* @var bool * @var bool
*/ */
private static $shutdown_handler_registered = false; private static bool $shutdown_handler_registered = false;
/** /**
* @var string * @var string
*/ */
private $filename; private string $filename;
/** /**
* @var string * @var string
*/ */
private $filepath; private string $filepath;
/** /**
* Create a new temporary file with optional options: * Create a new temporary file with optional options:
@ -92,7 +90,19 @@
} }
$this->filename .= '.' . $options[Options::Extension]; $this->filename .= '.' . $options[Options::Extension];
$this->filename = preg_replace('/[^a-zA-Z0-9.\-_]/', '', $this->filename); $replaced = preg_replace('/[^a-zA-Z0-9.\-_]/', '', $this->filename);
if($replaced === false)
{
throw new InvalidArgumentException('The filename contains invalid characters');
}
if(is_array($replaced))
{
$replaced = implode('', $replaced);
}
$this->filename = $replaced;
if(isset($options[Options::Directory])) if(isset($options[Options::Directory]))
{ {