Switch to constructor property declaration where possible.

This commit is contained in:
flash 2024-08-01 23:56:53 +00:00
parent 3d0c81541d
commit 7ef130629b
41 changed files with 209 additions and 318 deletions

View file

@ -1 +1 @@
0.2408.12322
0.2408.12356

View file

@ -1,7 +1,7 @@
<?php
// ArrayIterator.php
// Created: 2022-02-03
// Updated: 2023-11-09
// Updated: 2024-08-01
namespace Index;
@ -11,15 +11,14 @@ use Iterator;
* Provides an Iterator implementation for normal arrays.
*/
class ArrayIterator implements Iterator {
private array $array;
private bool $wasValid = true;
/**
* @param array $array Array to iterate upon.
*/
public function __construct(array $array) {
$this->array = $array;
}
public function __construct(
private array $array
) {}
/**
* Returns the current element.

View file

@ -1,7 +1,7 @@
<?php
// CSRFP.php
// Created: 2021-06-11
// Updated: 2024-07-31
// Updated: 2024-08-01
namespace Index;
@ -19,10 +19,6 @@ class CSRFP {
private const NONCE_LENGTH = 8;
private const HASH_LENGTH = 32;
private string $secretKey;
private string $identity;
private int $tolerance;
/**
* Creates a new CSRFP instance.
*
@ -32,14 +28,10 @@ class CSRFP {
* @return CSRFP New CSRFP instance.
*/
public function __construct(
string $secretKey,
string $identity,
int $tolerance = self::TOLERANCE
) {
$this->secretKey = $secretKey;
$this->identity = $identity;
$this->tolerance = $tolerance;
}
private string $secretKey,
private string $identity,
private int $tolerance = self::TOLERANCE
) {}
private static function time(int $time = -1): int {
return ($time < 0 ? time() : $time) - self::EPOCH;

View file

@ -1,7 +1,7 @@
<?php
// ColourHSL.php
// Created: 2023-01-02
// Updated: 2023-11-09
// Updated: 2024-08-01
namespace Index\Colour;
@ -9,11 +9,6 @@ namespace Index\Colour;
* Represents a colour using Hue, Saturation and Lightness.
*/
class ColourHSL extends Colour {
private float $hue;
private float $saturation;
private float $lightness;
private float $alpha;
private int $red;
private int $green;
private int $blue;
@ -24,11 +19,15 @@ class ColourHSL extends Colour {
* @param float $lightness Lightness property.
* @param float $alpha Alpha property.
*/
public function __construct(float $hue, float $saturation, float $lightness, float $alpha = 1.0) {
$this->hue = $hue;
$this->saturation = max(0.0, min(1.0, $saturation));
$this->lightness = max(0.0, min(1.0, $lightness));
$this->alpha = max(0.0, min(1.0, $alpha));
public function __construct(
private float $hue,
private float $saturation,
private float $lightness,
private float $alpha = 1.0
) {
$saturation = max(0.0, min(1.0, $saturation));
$lightness = max(0.0, min(1.0, $lightness));
$alpha = max(0.0, min(1.0, $alpha));
$c = (1 - abs(2 * $lightness - 1)) * $saturation;
$x = $c * (1 - abs(fmod($hue / 60, 2) - 1));

View file

@ -1,7 +1,7 @@
<?php
// ColourNamed.php
// Created: 2023-01-02
// Updated: 2023-11-09
// Updated: 2024-08-01
namespace Index\Colour;
@ -9,7 +9,6 @@ namespace Index\Colour;
* Represents a named CSS colour.
*/
class ColourNamed extends Colour {
private string $name;
private int $red = 0;
private int $green = 0;
private int $blue = 0;
@ -18,8 +17,10 @@ class ColourNamed extends Colour {
/**
* @param string $name CSS colour name.
*/
public function __construct(string $name) {
$this->name = strtolower($name);
public function __construct(
private string $name
) {
$name = strtolower($name);
$raw = self::COLOURS[$name] ?? -1;
$this->transparent = $raw === -1;

View file

@ -1,7 +1,7 @@
<?php
// ColourRGB.php
// Created: 2023-01-02
// Updated: 2023-11-09
// Updated: 2024-08-01
namespace Index\Colour;
@ -9,22 +9,22 @@ namespace Index\Colour;
* Represents an RGB colour.
*/
class ColourRGB extends Colour {
private int $red;
private int $green;
private int $blue;
private float $alpha;
/**
* @param int $red Red property.
* @param int $green Green property.
* @param int $blue Blue property.
* @param float $alpha Alpha property.
*/
public function __construct(int $red, int $green, int $blue, float $alpha = 1.0) {
$this->red = max(0, min(255, $red));
$this->green = max(0, min(255, $green));
$this->blue = max(0, min(255, $blue));
$this->alpha = max(0.0, min(1.0, $alpha));
public function __construct(
private int $red,
private int $green,
private int $blue,
private float $alpha = 1.0
) {
$red = max(0, min(255, $red));
$green = max(0, min(255, $green));
$blue = max(0, min(255, $blue));
$alpha = max(0.0, min(1.0, $alpha));
}
public function getRed(): int {

View file

@ -9,15 +9,14 @@ namespace Index\Data;
* Container to avoid having many prepared instances of the same query.
*/
class DbStatementCache {
private IDbConnection $dbConn;
private array $stmts = [];
/**
* @param IDbConnection $dbConn Connection to use with this cache.
*/
public function __construct(IDbConnection $dbConn) {
$this->dbConn = $dbConn;
}
public function __construct(
private IDbConnection $dbConn
) {}
private static function hash(string $query): string {
return hash('xxh3', $query, true);

View file

@ -1,7 +1,7 @@
<?php
// MariaDBCharacterSetInfo.php
// Created: 2021-05-02
// Updated: 2022-02-02
// Updated: 2024-08-01
namespace Index\Data\MariaDB;
@ -13,17 +13,15 @@ use stdClass;
* @see https://www.php.net/manual/en/mysqli.get-charset
*/
class MariaDBCharacterSetInfo {
private stdClass $charSet;
/**
* Creates a new character set info instance.
*
* @param stdClass $charSet Anonymous object containing the information.
* @return MariaDBCharacterSetInfo Character set information class.
*/
public function __construct(stdClass $charSet) {
$this->charSet = $charSet;
}
public function __construct(
private stdClass $charSet
) {}
/**
* Returns the name of the current character set.

View file

@ -13,20 +13,6 @@ use Index\Net\{EndPoint,DnsEndPoint,IPAddress,IPEndPoint,UnixEndPoint};
* Describes a MariaDB or MySQL connection.
*/
class MariaDBConnectionInfo implements IDbConnectionInfo {
private EndPoint $endPoint;
private string $userName;
private string $password;
private string $dbName;
private ?string $charSet;
private ?string $initCommand;
private ?string $keyPath;
private ?string $certPath;
private ?string $certAuthPath;
private ?string $trustedCertsPath;
private ?string $cipherAlgos;
private bool $verifyCert;
private bool $useCompression;
/**
* Creates an instance of MariaDBConnectionInfo.
*
@ -46,37 +32,39 @@ class MariaDBConnectionInfo implements IDbConnectionInfo {
* @return MariaDBConnectionInfo A connection info instance representing the given information.
*/
public function __construct(
EndPoint $endPoint,
string $userName,
string $password,
string $dbName,
string|null $charSet,
string|null $initCommand,
string|null $keyPath,
string|null $certPath,
string|null $certAuthPath,
string|null $trustedCertsPath,
string|null $cipherAlgos,
bool $verifyCert,
bool $useCompression
private EndPoint $endPoint,
private string $userName,
private string $password,
private string $dbName,
private string|null $charSet,
private string|null $initCommand,
private string|null $keyPath,
private string|null $certPath,
private string|null $certAuthPath,
private string|null $trustedCertsPath,
private string|null $cipherAlgos,
private bool $verifyCert,
private bool $useCompression
) {
if(!($endPoint instanceof IPEndPoint)
&& !($endPoint instanceof DnsEndPoint)
&& !($endPoint instanceof UnixEndPoint))
throw new InvalidArgumentException('$endPoint must be of type IPEndPoint, DnsEndPoint or UnixEndPoint.');
$this->endPoint = $endPoint;
$this->userName = $userName;
$this->password = $password;
$this->dbName = $dbName;
$this->charSet = empty($charSet) ? null : $charSet;
$this->initCommand = empty($initCommand) ? null : $initCommand;
$this->keyPath = empty($keyPath) ? null : $keyPath;
$this->certPath = empty($certPath) ? null : $certPath;
$this->certAuthPath = empty($certAuthPath) ? null : $certAuthPath;
$this->trustedCertsPath = empty($trustedCertsPath) ? null : $trustedCertsPath;
$this->cipherAlgos = empty($cipherAlgos) ? null : $cipherAlgos;
$this->verifyCert = $verifyCert;
$this->useCompression = $useCompression;
if(empty($charSet))
$charSet = null;
if(empty($initCommand))
$initCommand = null;
if(empty($keyPath))
$keyPath = null;
if(empty($certPath))
$certPath = null;
if(empty($certAuthPath))
$certAuthPath = null;
if(empty($trustedCertsPath))
$trustedCertsPath = null;
if(empty($cipherAlgos))
$cipherAlgos = null;
}
/**

View file

@ -11,10 +11,6 @@ use Index\Data\{DbTools,DbType};
* Represents a bound parameter.
*/
class MariaDBParameter {
private int $ordinal;
private int $type;
private mixed $value;
/**
* Create a new MariaDBParameter.
*
@ -23,14 +19,15 @@ class MariaDBParameter {
* @param mixed $value Value to bind.
* @return MariaDBParameter A new parameter instance.
*/
public function __construct(int $ordinal, int $type, mixed $value) {
$this->ordinal = $ordinal;
public function __construct(
private int $ordinal,
private int $type,
private mixed $value
) {
if($type == DbType::AUTO)
$type = DbTools::detectType($value);
$this->type = $type;
$this->value = $type === DbType::NULL ? null : $value;
if($type === DbType::NULL)
$value = null;
}
/**

View file

@ -17,7 +17,6 @@ use Index\IO\{Stream,TempFileStream};
abstract class MariaDBResult implements IDbResult {
use DbResultTrait;
protected mysqli_stmt|mysqli_result $result;
protected array $currentRow = [];
/**
@ -26,9 +25,9 @@ abstract class MariaDBResult implements IDbResult {
* @param mysqli_stmt|mysqli_result $result A result to work with.
* @return MariaDBResult A result instance.
*/
public function __construct(mysqli_stmt|mysqli_result $result) {
$this->result = $result;
}
public function __construct(
protected mysqli_stmt|mysqli_result $result
) {}
/**
* Gets the number of available rows in this result.

View file

@ -15,7 +15,6 @@ use Index\IO\Stream;
* Represents a MariaDB/MySQL prepared statement.
*/
class MariaDBStatement implements IDbStatement {
private mysqli_stmt $statement;
private array $params = [];
/**
@ -24,9 +23,9 @@ class MariaDBStatement implements IDbStatement {
* @param mysqli_stmt $statement Underlying statement object.
* @return MariaDBStatement A new statement instance.
*/
public function __construct(mysqli_stmt $statement) {
$this->statement = $statement;
}
public function __construct(
private mysqli_stmt $statement
) {}
private static bool $constructed = false;
private static string $resultImplementation;

View file

@ -1,7 +1,7 @@
<?php
// MariaDBWarning.php
// Created: 2021-05-02
// Updated: 2022-02-27
// Updated: 2024-08-01
namespace Index\Data\MariaDB;
@ -13,10 +13,6 @@ use Index\XArray;
* Represents a MariaDB/MySQL warning.
*/
class MariaDBWarning implements Stringable {
private string $message;
private string $sqlState;
private int $code;
/**
* Creates a new MariaDBWarning object.
*
@ -25,11 +21,11 @@ class MariaDBWarning implements Stringable {
* @param int $code Error code of the warning.
* @return MariaDBWarning A new warning instance.
*/
public function __construct(string $message, string $sqlState, int $code) {
$this->message = $message;
$this->sqlState = $sqlState;
$this->code = $code;
}
public function __construct(
private string $message,
private string $sqlState,
private int $code
) {}
/**
* Gets the warning message string.

View file

@ -8,15 +8,15 @@ namespace Index\Data\Migration;
use Index\Data\IDbConnection;
class FsDbMigrationInfo implements IDbMigrationInfo {
private string $path;
private string $name;
private string $className;
/**
* @param string $path Filesystem path to the migration file.
*/
public function __construct(string $path) {
$this->path = $path;
public function __construct(
private string $path
) {
$this->name = $name = pathinfo($path, PATHINFO_FILENAME);
$dateTime = substr($name, 0, 17);

View file

@ -1,7 +1,7 @@
<?php
// SQLiteConnectionInfo.php
// Created: 2021-05-02
// Updated: 2022-02-28
// Updated: 2024-08-01
namespace Index\Data\SQLite;
@ -11,11 +11,6 @@ use Index\Data\IDbConnectionInfo;
* Represents information about a SQLite Client.
*/
class SQLiteConnectionInfo implements IDbConnectionInfo {
private string $fileName;
private bool $readOnly;
private bool $create;
private string $encryptionKey;
/**
* Creates a new SQLiteConnectionInfo instance.
*
@ -26,16 +21,11 @@ class SQLiteConnectionInfo implements IDbConnectionInfo {
* @return SQLiteConnectionInfo Information to create an SQLite database client.
*/
public function __construct(
string $fileName,
string $encryptionKey,
bool $readOnly,
bool $create
) {
$this->fileName = $fileName;
$this->encryptionKey = $encryptionKey;
$this->readOnly = $readOnly;
$this->create = $create;
}
private string $fileName,
private string $encryptionKey,
private bool $readOnly,
private bool $create
) {}
/**
* Gets the path of the SQLite database.

View file

@ -16,7 +16,6 @@ use Index\IO\{Stream,TempFileStream};
class SQLiteResult implements IDbResult {
use DbResultTrait;
private SQLite3Result $result;
private array $currentRow = [];
/**
@ -25,9 +24,9 @@ class SQLiteResult implements IDbResult {
* @param SQLite3Result $result Raw underlying result class.
* @return SQLiteResult A new SQLiteResult instance.
*/
public function __construct(SQLite3Result $result) {
$this->result = $result;
}
public function __construct(
private SQLite3Result $result
) {}
/**
* Gets the number of columns per row in the result.

View file

@ -16,8 +16,6 @@ use Index\IO\Stream;
* Represents a prepared SQLite SQL statement.
*/
class SQLiteStatement implements IDbStatement {
private SQLiteConnection $connection;
private SQLite3Stmt $statement;
private ?SQLite3Result $result = null;
/**
@ -27,10 +25,10 @@ class SQLiteStatement implements IDbStatement {
* @param SQLite3Stmt $statement The raw statement instance.
* @return SQLiteStatement A new statement instance.
*/
public function __construct(SQLiteConnection $connection, SQLite3Stmt $statement) {
$this->connection = $connection;
$this->statement = $statement;
}
public function __construct(
private SQLiteConnection $connection,
private SQLite3Stmt $statement
) {}
public function getParameterCount(): int {
return $this->statement->paramCount();

View file

@ -12,14 +12,12 @@ use Index\IO\{Stream,FileStream};
* Represents Bencoded body content for a HTTP message.
*/
class BencodedContent implements IHttpContent, IBencodeSerialisable {
private mixed $content;
/**
* @param mixed $content Content to be bencoded.
*/
public function __construct(mixed $content) {
$this->content = $content;
}
public function __construct(
private mixed $content
) {}
/**
* Retrieves unencoded content.

View file

@ -12,17 +12,14 @@ use Index\Http\HttpUploadedFile;
* Represents form body content for a HTTP message.
*/
class FormContent implements IHttpContent {
private array $postFields;
private array $uploadedFiles;
/**
* @param array<string, mixed> $postFields Form fields.
* @param array<string, HttpUploadedFile> $uploadedFiles Uploaded files.
*/
public function __construct(array $postFields, array $uploadedFiles) {
$this->postFields = $postFields;
$this->uploadedFiles = $uploadedFiles;
}
public function __construct(
private array $postFields,
private array $uploadedFiles
) {}
/**
* Retrieves a form field with filtering.

View file

@ -12,14 +12,12 @@ use Index\IO\{Stream,FileStream};
* Represents JSON body content for a HTTP message.
*/
class JsonContent implements IHttpContent, JsonSerializable {
private mixed $content;
/**
* @param mixed $content Content to be JSON encoded.
*/
public function __construct(mixed $content) {
$this->content = $content;
}
public function __construct(
private mixed $content
) {}
/**
* Retrieves unencoded content.

View file

@ -11,14 +11,12 @@ use Index\IO\{Stream,FileStream};
* Represents Stream body content for a HTTP message.
*/
class StreamContent implements IHttpContent {
private Stream $stream;
/**
* @param Stream $stream Stream that represents this message body.
*/
public function __construct(Stream $stream) {
$this->stream = $stream;
}
public function __construct(
private Stream $stream
) {}
/**
* Retrieves the underlying stream.

View file

@ -11,14 +11,12 @@ use Stringable;
* Represents string body content for a HTTP message.
*/
class StringContent implements IHttpContent {
private string $string;
/**
* @param string $string String that represents this message body.
*/
public function __construct(string $string) {
$this->string = $string;
}
public function __construct(
private string $string
) {}
/**
* Retrieves the underlying string.

View file

@ -11,15 +11,16 @@ use Stringable;
* Represents a generic HTTP header.
*/
class HttpHeader implements Stringable {
private string $name;
private array $lines;
/**
* @param string $name Name of the header.
* @param string ...$lines Lines of the header.
*/
public function __construct(string $name, string ...$lines) {
$this->name = $name;
public function __construct(
private string $name,
string ...$lines
) {
$this->lines = $lines;
}

View file

@ -11,12 +11,12 @@ use RuntimeException;
* Represents a collection of HTTP headers.
*/
class HttpHeaders {
private array $headers;
/**
* @param HttpHeader[] $headers HTTP header instances.
*/
public function __construct(array $headers) {
public function __construct(
private array $headers
) {
$real = [];
foreach($headers as $header)

View file

@ -13,20 +13,16 @@ use Index\Http\Content\{IHttpContent,BencodedContent,FormContent,JsonContent,Str
* Represents a base HTTP message.
*/
abstract class HttpMessage {
private string $version;
private HttpHeaders $headers;
private ?IHttpContent $content;
/**
* @param string $version HTTP message version.
* @param HttpHeaders $headers HTTP message headers.
* @param ?IHttpContent $content Body contents.
*/
public function __construct(string $version, HttpHeaders $headers, ?IHttpContent $content) {
$this->version = $version;
$this->headers = $headers;
$this->content = $content;
}
public function __construct(
private string $version,
private HttpHeaders $headers,
private ?IHttpContent $content
) {}
/**
* Retrieves the HTTP version of this message.

View file

@ -14,11 +14,6 @@ use Index\Http\Content\{IHttpContent,JsonContent,StreamContent,FormContent};
* Represents a HTTP request message.
*/
class HttpRequest extends HttpMessage {
private string $method;
private string $path;
private array $params;
private array $cookies;
/**
* @param string $version HTTP message version.
* @param string $method HTTP request method.
@ -30,18 +25,13 @@ class HttpRequest extends HttpMessage {
*/
public function __construct(
string $version,
string $method,
string $path,
array $params,
array $cookies,
private string $method,
private string $path,
private array $params,
private array $cookies,
HttpHeaders $headers,
?IHttpContent $content
) {
$this->method = $method;
$this->path = $path;
$this->params = $params;
$this->cookies = $cookies;
parent::__construct($version, $headers, $content);
}
@ -93,7 +83,7 @@ class HttpRequest extends HttpMessage {
public function getParam(string $name, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed {
if(!isset($this->params[$name]))
return null;
return filter_var($this->params[$name] ?? null, $filter, $options);
return filter_var($this->params[$name], $filter, $options);
}
/**
@ -126,7 +116,7 @@ class HttpRequest extends HttpMessage {
public function getCookie(string $name, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed {
if(!isset($this->cookies[$name]))
return null;
return filter_var($this->cookies[$name] ?? null, $filter, $options);
return filter_var($this->cookies[$name], $filter, $options);
}
/**

View file

@ -11,9 +11,6 @@ use Index\Http\Content\IHttpContent;
* Represents a HTTP response message.
*/
class HttpResponse extends HttpMessage {
private int $statusCode;
private string $statusText;
/**
* @param string $version HTTP message version.
* @param int $statusCode HTTP response status code.
@ -23,14 +20,11 @@ class HttpResponse extends HttpMessage {
*/
public function __construct(
string $version,
int $statusCode,
string $statusText,
private int $statusCode,
private string $statusText,
HttpHeaders $headers,
?IHttpContent $content
) {
$this->statusCode = $statusCode;
$this->statusText = $statusText;
parent::__construct($version, $headers, $content);
}

View file

@ -14,11 +14,6 @@ use Index\IO\{Stream,FileStream};
* Represents an uploaded file in a multipart/form-data request.
*/
class HttpUploadedFile implements ICloseable {
private int $errorCode;
private int $size;
private string $localFileName;
private string $suggestedFileName;
private MediaType $suggestedMediaType;
private bool $hasMoved = false;
private ?Stream $stream = null;
@ -30,20 +25,14 @@ class HttpUploadedFile implements ICloseable {
* @param MediaType|string $suggestedMediaType Mediatype included by the client.
*/
public function __construct(
int $errorCode,
int $size,
string $localFileName,
string $suggestedFileName,
MediaType|string $suggestedMediaType
private int $errorCode,
private int $size,
private string $localFileName,
private string $suggestedFileName,
private MediaType|string $suggestedMediaType
) {
if(!($suggestedMediaType instanceof MediaType))
$suggestedMediaType = MediaType::parse($suggestedMediaType);
$this->errorCode = $errorCode;
$this->size = $size;
$this->localFileName = $localFileName;
$this->suggestedFileName = $suggestedFileName;
$this->suggestedMediaType = $suggestedMediaType;
}
/**

View file

@ -1,7 +1,7 @@
<?php
// HandlerAttribute.php
// Created: 2024-03-28
// Updated: 2024-03-28
// Updated: 2024-08-01
namespace Index\Http\Routing;
@ -12,7 +12,12 @@ use ReflectionObject;
* Provides base for attributes that mark methods in a class as handlers.
*/
abstract class HandlerAttribute {
public function __construct(private string $path) {}
/**
* @param string $path Target path.
*/
public function __construct(
private string $path
) {}
/**
* Returns the target path.

View file

@ -1,7 +1,7 @@
<?php
// HttpRoute.php
// Created: 2024-03-28
// Updated: 2024-03-28
// Updated: 2024-08-01
namespace Index\Http\Routing;
@ -12,15 +12,15 @@ use Attribute;
*/
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
class HttpRoute extends HandlerAttribute {
private string $method;
/**
* @param string $method
* @param string $path
*/
public function __construct(string $method, string $path) {
public function __construct(
private string $method,
string $path
) {
parent::__construct($path);
$this->method = $method;
}
/**

View file

@ -22,8 +22,6 @@ class HttpRouter implements IRouter {
private array $contentHandlers = [];
private string $defaultCharSet;
private IErrorHandler $errorHandler;
/**
@ -32,11 +30,10 @@ class HttpRouter implements IRouter {
* @param bool $registerDefaultContentHandlers true to register default content handlers for JSON, Bencode, etc.
*/
public function __construct(
string $charSet = '',
private string $charSet = '',
IErrorHandler|string $errorHandler = 'html',
bool $registerDefaultContentHandlers = true,
) {
$this->defaultCharSet = $charSet;
$this->setErrorHandler($errorHandler);
if($registerDefaultContentHandlers)
@ -49,9 +46,9 @@ class HttpRouter implements IRouter {
* @return string Normalised character set name.
*/
public function getCharSet(): string {
if($this->defaultCharSet === '')
if($this->charSet === '')
return strtolower(mb_preferred_mime_name(mb_internal_encoding()));
return $this->defaultCharSet;
return $this->charSet;
}
/**

View file

@ -11,20 +11,18 @@ namespace Index\Http\Routing;
class ScopedRouter implements IRouter {
use RouterTrait;
private IRouter $router;
private string $prefix;
/**
* @param IRouter $router Underlying router.
* @param string $prefix Base path to use as a prefix.
*/
public function __construct(IRouter $router, string $prefix) {
public function __construct(
private IRouter $router,
private string $prefix
) {
if($router instanceof ScopedRouter)
$router = $router->getParentRouter();
$this->router = $router;
// todo: cleanup
$this->prefix = $prefix;
// TODO: cleanup prefix
}
private function getParentRouter(): IRouter {

View file

@ -110,17 +110,17 @@ class FileStream extends GenericStream {
*/
public const LOCK_NON_BLOCKING = LOCK_NB;
private int $lock;
/**
* @param string $path Path to the file.
* @param string $mode Stream mode to use to open the file.
* @param int $lock Locking method to use.
* @throws RuntimeException If the file could not be opened.
*/
public function __construct(string $path, string $mode, int $lock) {
$this->lock = $lock;
public function __construct(
string $path,
string $mode,
private int $lock
) {
try {
$stream = fopen($path, $mode);
} catch(ErrorException $ex) {

View file

@ -32,7 +32,9 @@ class GenericStream extends Stream {
'x+', 'x+b', 'c', 'cb', 'c+', 'c+b',
];
protected $stream; // resource
// resource
// can't seem to turn this one into a constructor property thing?
protected $stream;
private bool $canRead;
private bool $canWrite;

View file

@ -12,17 +12,12 @@ use Stringable;
* Implements a structure for representing and comparing media/mime types.
*/
class MediaType implements Stringable, IComparable, IEquatable {
private string $category = '';
private string $kind = '';
private string $suffix = '';
private array $params = [];
public function __construct(string $category, string $kind, string $suffix, array $params) {
$this->category = $category;
$this->kind = $kind;
$this->suffix = $suffix;
$this->params = $params;
}
public function __construct(
private string $category,
private string $kind,
private string $suffix,
private array $params
) {}
public function getCategory(): string {
return $this->category;

View file

@ -11,19 +11,17 @@ use InvalidArgumentException;
* A DNS end point.
*/
class DnsEndPoint extends EndPoint {
private string $host;
private int $port;
/**
* @param string $host DNS host name.
* @param int $port Network port, 0 to leave default.
* @throws InvalidArgumentException If $port is less than 0 or greater than 65535.
*/
public function __construct(string $host, int $port) {
public function __construct(
private string $host,
private int $port
) {
if($port < 0 || $port > 0xFFFF)
throw new InvalidArgumentException('$port is not a valid port number.');
$this->host = $host;
$this->port = $port;
}
/**

View file

@ -35,14 +35,14 @@ final class IPAddress implements JsonSerializable, Stringable, IEquatable {
*/
public const V6 = 6;
private string $raw;
private int $width;
/**
* @param string $raw Raw IP address data.
*/
public function __construct(string $raw) {
$this->raw = $raw;
public function __construct(
private string $raw
) {
$this->width = strlen($raw);
}

View file

@ -14,17 +14,14 @@ use Index\IEquatable;
* Represents a CIDR range of IP addresses.
*/
final class IPAddressRange implements JsonSerializable, Stringable, IEquatable {
private IPAddress $base;
private int $mask;
/**
* @param IPAddress $base Base IP address.
* @param int $mask CIDR mask.
*/
public function __construct(IPAddress $base, int $mask) {
$this->base = $base;
$this->mask = $mask;
}
public function __construct(
private IPAddress $base,
private int $mask
) {}
/**
* Retrieves the base IP address.

View file

@ -12,19 +12,17 @@ use Index\XString;
* Represents an IP address end point.
*/
class IPEndPoint extends EndPoint {
private IPAddress $address;
private int $port;
/**
* @param IPAddress $address IP address.
* @param int $port Network port, 0 to leave default.
* @throws InvalidArgumentException If $port is less than 0 or greater than 65535.
*/
public function __construct(IPAddress $address, int $port) {
public function __construct(
private IPAddress $address,
private int $port
) {
if($port < 0 || $port > 0xFFFF)
throw new InvalidArgumentException('$port is not a valid port number.');
$this->address = $address;
$this->port = $port;
}
/**

View file

@ -9,14 +9,12 @@ namespace Index\Net;
* Represents a UNIX socket path end point.
*/
class UnixEndPoint extends EndPoint {
private string $path;
/**
* @param string $socketPath Path to the socket file.
*/
public function __construct(string $socketPath) {
$this->path = $socketPath;
}
public function __construct(
private string $socketPath
) {}
/**
* Gets path to the socket file.
@ -24,20 +22,20 @@ class UnixEndPoint extends EndPoint {
* @return string Path to the socket file.
*/
public function getPath(): string {
return $this->path;
return $this->socketPath;
}
public function __serialize(): array {
return [$this->path];
return [$this->socketPath];
}
public function __unserialize(array $serialized): void {
$this->path = $serialized[0];
$this->socketPath = $serialized[0];
}
public function equals(mixed $other): bool {
return $other instanceof UnixEndPoint
&& $this->path === $other->path;
&& $this->socketPath === $other->socketPath;
}
/**
@ -56,6 +54,6 @@ class UnixEndPoint extends EndPoint {
}
public function __toString(): string {
return $this->path;
return $this->socketPath;
}
}

View file

@ -9,11 +9,6 @@ namespace Index\Performance;
* Represents a timing point.
*/
class TimingPoint {
private int $timePoint;
private int $duration;
private string $name;
private string $comment;
/**
* @param int $timePoint Starting tick count.
* @param int $duration Duration ticks count.
@ -21,16 +16,11 @@ class TimingPoint {
* @param string $comment Timing point comment.
*/
public function __construct(
int $timePoint,
int $duration,
string $name,
string $comment
) {
$this->timePoint = $timePoint;
$this->duration = $duration;
$this->name = $name;
$this->comment = $comment;
}
private int $timePoint,
private int $duration,
private string $name,
private string $comment
) {}
/**
* Gets the starting ticks count.