Skip to content

Commit

Permalink
chore: rename all enum cases to PascalCase instead of SHOUT_CASE
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Mar 25, 2024
1 parent 24a777d commit cbe1356
Show file tree
Hide file tree
Showing 89 changed files with 451 additions and 417 deletions.
8 changes: 5 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

# Components

* [Psl\Async](../src/Psl/Async/README.md)
* [Psl\Default](../src/Psl/Default/README.md)
* [Psl\Range](../src/Psl/Range/README.md)
* [Psl\Async](../src/Psl/Async/README.md)
* [Psl\Range](../src/Psl/Range/README.md)

---

Expand Down Expand Up @@ -57,3 +56,6 @@
- [Psl\Unix](./component/unix.md)
- [Psl\Vec](./component/vec.md)

t/unix.md)
- [Psl\Vec](./component/vec.md)

2 changes: 1 addition & 1 deletion docs/component/html.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

#### `Enums`

- [Encoding](./../../src/Psl/Html/Encoding.php#L20)
- [Encoding](./../../src/Psl/Html/Encoding.php#L17)


4 changes: 2 additions & 2 deletions docs/documenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function regenerate_documentation(): void

$readme_template = File\read(__DIR__ . '/templates/README.template.md');
$readme = Str\replace($readme_template, '{{ list }}', $components_documentation);
$mode = Filesystem\is_file($readme) ? File\WriteMode::TRUNCATE : File\WriteMode::OPEN_OR_CREATE;
$mode = Filesystem\is_file($readme) ? File\WriteMode::Truncate : File\WriteMode::OpenOrCreate;
File\write(__DIR__ . '/README.md', $readme, $mode);

foreach ($components as $component) {
Expand Down Expand Up @@ -143,7 +143,7 @@ function document_component(string $component, string $index_link): void
), "\n"),
]);

$mode = Filesystem\is_file($current_filename) ? File\WriteMode::TRUNCATE : File\WriteMode::OPEN_OR_CREATE;
$mode = Filesystem\is_file($current_filename) ? File\WriteMode::Truncate : File\WriteMode::OpenOrCreate;
File\write($current_filename, $documentation, $mode);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Psl/Encoding/Base64/Internal/Base64.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ abstract class Base64
public static function encode(string $binary, bool $padding = true): string
{
$dest = '';
$binary_length = Str\length($binary, encoding: Str\Encoding::ASCII_8BIT);
$binary_length = Str\length($binary, encoding: Str\Encoding::Ascii8bit);

for ($i = 0; $i + 3 <= $binary_length; $i += 3) {
/** @var array<int, int> $chunk */
$chunk = unpack('C*', Str\slice($binary, $i, 3, encoding: Str\Encoding::ASCII_8BIT));
$chunk = unpack('C*', Str\slice($binary, $i, 3, encoding: Str\Encoding::Ascii8bit));
$byte0 = $chunk[1];
$byte1 = $chunk[2];
$byte2 = $chunk[3];
Expand All @@ -55,7 +55,7 @@ public static function encode(string $binary, bool $padding = true): string
*
* @var array<int, int> $chunk
*/
$chunk = unpack('C*', Str\slice($binary, $i, $binary_length - $i, encoding: Str\Encoding::ASCII_8BIT));
$chunk = unpack('C*', Str\slice($binary, $i, $binary_length - $i, encoding: Str\Encoding::Ascii8bit));
$byte0 = $chunk[1];
if ($i + 1 < $binary_length) {
$byte1 = $chunk[2];
Expand Down Expand Up @@ -92,7 +92,7 @@ public static function encode(string $binary, bool $padding = true): string
*/
public static function decode(string $base64, bool $explicit_padding = true): string
{
$base64_length = Str\length($base64, encoding: Str\Encoding::ASCII_8BIT);
$base64_length = Str\length($base64, encoding: Str\Encoding::Ascii8bit);
if ($base64_length === 0) {
return '';
}
Expand All @@ -105,13 +105,13 @@ public static function decode(string $base64, bool $explicit_padding = true): st

/** @psalm-suppress MissingThrowsDocblock */
$base64 = Str\trim_right($base64, '=');
$base64_length = Str\length($base64, encoding: Str\Encoding::ASCII_8BIT);
$base64_length = Str\length($base64, encoding: Str\Encoding::Ascii8bit);

$err = 0;
$dest = '';
for ($i = 0; $i + 4 <= $base64_length; $i += 4) {
/** @var array<int, int> $chunk */
$chunk = unpack('C*', Str\slice($base64, $i, 4, encoding: Str\Encoding::ASCII_8BIT));
$chunk = unpack('C*', Str\slice($base64, $i, 4, encoding: Str\Encoding::Ascii8bit));
$char0 = static::decode6Bits($chunk[1]);
$char1 = static::decode6Bits($chunk[2]);
$char2 = static::decode6Bits($chunk[3]);
Expand All @@ -130,7 +130,7 @@ public static function decode(string $base64, bool $explicit_padding = true): st
*
* @var array<int, int> $chunk
*/
$chunk = unpack('C*', Str\slice($base64, $i, $base64_length - $i, encoding: Str\Encoding::ASCII_8BIT));
$chunk = unpack('C*', Str\slice($base64, $i, $base64_length - $i, encoding: Str\Encoding::Ascii8bit));
$char0 = static::decode6Bits($chunk[1]);
if ($i + 2 < $base64_length) {
$char1 = static::decode6Bits($chunk[2]);
Expand Down
13 changes: 11 additions & 2 deletions src/Psl/Encoding/Base64/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum Variant implements DefaultInterface
*
* This variant is typically used in MIME messages and XML data.
*/
case Default;
case Standard;

/**
* A URL and filename safe Base64 encoding variant.
Expand Down Expand Up @@ -64,8 +64,17 @@ enum Variant implements DefaultInterface
*/
case DotSlashOrdered;

/**
* Provides the default variant for Base64 encoding.
*
* By default, this method returns the `Standard` variant, which is widely used across
* various applications, including MIME messages and XML data. It represents a safe
* and common choice for general-purpose encoding needs.
*
* @return static The `Standard` variant of Base64 encoding.
*/
public static function default(): static
{
return self::Default;
return self::Standard;
}
}
4 changes: 2 additions & 2 deletions src/Psl/Encoding/Base64/decode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
* the base64 characters range.
* @throws Exception\IncorrectPaddingException If the encoded string has an incorrect padding.
*/
function decode(string $base64, Variant $variant = Variant::Default, bool $explicit_padding = true): string
function decode(string $base64, Variant $variant = Variant::Standard, bool $explicit_padding = true): string
{
return match ($variant) {
Variant::Default => Internal\Base64::decode($base64, $explicit_padding),
Variant::Standard => Internal\Base64::decode($base64, $explicit_padding),
Variant::UrlSafe => Internal\Base64UrlSafe::decode($base64, $explicit_padding),
Variant::DotSlash => Internal\Base64DotSlash::decode($base64, $explicit_padding),
Variant::DotSlashOrdered => Internal\Base64DotSlashOrdered::decode($base64, $explicit_padding),
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/Encoding/Base64/encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
*
* @pure
*/
function encode(string $binary, Variant $variant = Variant::Default, bool $padding = true): string
function encode(string $binary, Variant $variant = Variant::Standard, bool $padding = true): string
{
return match ($variant) {
Variant::Default => Internal\Base64::encode($binary, $padding),
Variant::Standard => Internal\Base64::encode($binary, $padding),
Variant::UrlSafe => Internal\Base64UrlSafe::encode($binary, $padding),
Variant::DotSlash => Internal\Base64DotSlash::encode($binary, $padding),
Variant::DotSlashOrdered => Internal\Base64DotSlashOrdered::encode($binary, $padding),
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Encoding/Hex/decode.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function decode(string $hexadecimal): string
);
}

$hex_len = Str\length($hexadecimal, Str\Encoding::ASCII_8BIT);
$hex_len = Str\length($hexadecimal, Str\Encoding::Ascii8bit);
if (($hex_len & 1) !== 0) {
throw new Exception\RangeException(
'Expected an even number of hexadecimal characters.',
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/File/HandleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getSize(): int;
* Example:
*
* ```php
* $lock = $file->lock(LockType::SHARED);
* $lock = $file->lock(LockType::Shared);
* // lock has been acquired.
* $lock->release();
* ```
Expand All @@ -53,7 +53,7 @@ public function lock(LockType $type): Lock;
*
* ```php
* try {
* $lock = $file->tryLock(LockType::SHARED);
* $lock = $file->tryLock(LockType::Shared);
* // lock has been acquired.
* $lock->release();
* } catch(AlreadyLockedException) {
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/File/Internal/ResourceHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function tryLock(LockType $type): Lock
throw new Exception\AlreadyClosedException('Handle has already been closed.');
}

$operations = LOCK_NB | ($type === LockType::EXCLUSIVE ? LOCK_EX : LOCK_SH);
$operations = LOCK_NB | ($type === LockType::Exclusive ? LOCK_EX : LOCK_SH);
$success = @flock($this->stream, $operations, $would_block);
// @codeCoverageIgnoreStart
if ($would_block) {
Expand All @@ -118,7 +118,7 @@ public function tryLock(LockType $type): Lock
if (!$success) {
throw new File\Exception\RuntimeException(Str\format(
'Could not acquire %s lock for "%s".',
$type === LockType::EXCLUSIVE ? 'exclusive' : 'shared',
$type === LockType::Exclusive ? 'exclusive' : 'shared',
$this->getPath(),
));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/File/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ final class Lock
private bool $released = false;

/**
* @param (Closure(): void) $release
* @param (Closure(): void) $releaseCallback
*
* @internal use HandleInterface::lock() to create a lock.
* @internal use {@see HandleInterface::lock()} to create a lock.
*/
public function __construct(
public readonly LockType $type,
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/File/LockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ enum LockType
* commonly called a reader lock. The creation of a Lock will block until
* the lock is acquired.
*/
case SHARED;
case Shared;

/**
* Only a single process may possess an exclusive lock to a given file at a
* time. The creation of a Lock will block until the lock is acquired.
*/
case EXCLUSIVE;
case Exclusive;
}
4 changes: 2 additions & 2 deletions src/Psl/File/ReadWriteHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ final class ReadWriteHandle extends Internal\AbstractHandleWrapper implements Re
* @throws Exception\NotReadableException If $file exists, and is non-readable.
* @throws Exception\RuntimeException If unable to create the $file if it does not exist.
*/
public function __construct(string $file, WriteMode $write_mode = WriteMode::OPEN_OR_CREATE)
public function __construct(string $file, WriteMode $write_mode = WriteMode::OpenOrCreate)
{
$is_file = Filesystem\is_file($file);
if (!$is_file && Filesystem\exists($file)) {
throw Exception\NotFileException::for($file);
}

$must_create = $write_mode === WriteMode::MUST_CREATE;
$must_create = $write_mode === WriteMode::MustCreate;
if ($must_create && $is_file) {
throw Exception\AlreadyCreatedException::for($file);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/File/WriteHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ final class WriteHandle extends Internal\AbstractHandleWrapper implements WriteH
* @throws Exception\NotWritableException If $file is non-writable.
* @throws Exception\RuntimeException If unable to create the $file if it does not exist.
*/
public function __construct(string $file, WriteMode $write_mode = WriteMode::OPEN_OR_CREATE)
public function __construct(string $file, WriteMode $write_mode = WriteMode::OpenOrCreate)
{
$is_file = Filesystem\is_file($file);
if (!$is_file && Filesystem\exists($file)) {
throw Exception\NotFileException::for($file);
}

$must_create = $write_mode === WriteMode::MUST_CREATE;
$must_create = $write_mode === WriteMode::MustCreate;
if ($must_create && $is_file) {
throw Exception\AlreadyCreatedException::for($file);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Psl/File/WriteMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ enum WriteMode: string
* Open the file for writing only; place the file pointer at the beginning of
* the file.
*
* If the file exits, it is not truncated (as with `TRUNCATE`), and the call
* succeeds (unlike `MUST_CREATE`).
* If the file exits, it is not truncated (as with `Truncate`), and the call
* succeeds (unlike `MustCreate`).
*/
case OPEN_OR_CREATE = 'cb';
case OpenOrCreate = 'cb';

/**
* Open for writing only; place the file pointer at the beginning of the
* file and truncate the file to zero length. If the file does not exist,
* attempt to create it.
*/
case TRUNCATE = 'wb';
case Truncate = 'wb';

/**
* Open for writing only; place the file pointer at the end of the file. If
* the file does not exist, attempt to create it. In this mode, seeking has
* no effect, writes are always appended.
*/
case APPEND = 'ab';
case Append = 'ab';

/**
* Create and open for writing only; place the file pointer at the beginning
* of the file. If the file already exists, the filesystem call will throw an
* exception. If the file does not exist, attempt to create it.
*/
case MUST_CREATE = 'xb';
case MustCreate = 'xb';
}
2 changes: 1 addition & 1 deletion src/Psl/File/open_read_write.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @throws Exception\NotReadableException If $file exists, and is non-readable.
* @throws Exception\RuntimeException If unable to create the $file if it does not exist.
*/
function open_read_write(string $path, WriteMode $write_mode = WriteMode::OPEN_OR_CREATE): ReadWriteHandleInterface
function open_read_write(string $path, WriteMode $write_mode = WriteMode::OpenOrCreate): ReadWriteHandleInterface
{
return new ReadWriteHandle($path, $write_mode);
}
2 changes: 1 addition & 1 deletion src/Psl/File/open_write_only.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @throws Exception\NotWritableException If $file exists, and is non-writable.
* @throws Exception\RuntimeException If unable to create the $file if it does not exist.
*/
function open_write_only(string $file, WriteMode $mode = WriteMode::OPEN_OR_CREATE): WriteHandleInterface
function open_write_only(string $file, WriteMode $mode = WriteMode::OpenOrCreate): WriteHandleInterface
{
return new WriteHandle($file, $mode);
}
2 changes: 1 addition & 1 deletion src/Psl/File/read.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function read(string $file, int $offset = 0, ?int $length = null): string
{
try {
$handle = namespace\open_read_only($file);
$lock = $handle->lock(namespace\LockType::SHARED);
$lock = $handle->lock(namespace\LockType::Shared);

$handle->seek($offset);
$content = $handle->readAll($length);
Expand Down
4 changes: 2 additions & 2 deletions src/Psl/File/write.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
* @throws Exception\NotWritableException If $file exists, and is non-writable.
* @throws Exception\RuntimeException In case of an error.
*/
function write(string $file, string $content, WriteMode $mode = WriteMode::OPEN_OR_CREATE): void
function write(string $file, string $content, WriteMode $mode = WriteMode::OpenOrCreate): void
{
clearstatcache();

try {
$handle = File\open_write_only($file, $mode);
$lock = $handle->lock(File\LockType::EXCLUSIVE);
$lock = $handle->lock(File\LockType::Exclusive);

$handle->writeAll($content);

Expand Down
6 changes: 3 additions & 3 deletions src/Psl/Filesystem/copy.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ function copy(string $source, string $destination, bool $overwrite = false): voi
$source_handle = File\open_read_only($source);
$destination_handle = File\open_write_only(
$destination,
$destination_exists ? File\WriteMode::TRUNCATE : File\WriteMode::OPEN_OR_CREATE,
$destination_exists ? File\WriteMode::Truncate : File\WriteMode::OpenOrCreate,
);

$source_lock = $source_handle->lock(File\LockType::SHARED);
$destination_lock = $destination_handle->lock(File\LockType::EXCLUSIVE);
$source_lock = $source_handle->lock(File\LockType::Shared);
$destination_lock = $destination_handle->lock(File\LockType::Exclusive);

while ($chunk = $source_handle->read()) {
$destination_handle->writeAll($chunk);
Expand Down
Loading

0 comments on commit cbe1356

Please sign in to comment.