Skip to content

Commit

Permalink
refactor(concerns): improve magic methods
Browse files Browse the repository at this point in the history
- Refactor magic methods in the `ConcreteMagic` trait to explicitly specify return types.
- Use PHPDoc annotations to suppress inspection warnings for magic methods in the `ConcreteMagic` trait.
- Change visibility of the `normalizeOptions`, `normalizeOption`, `getDefaultSoarBinary`, and `createProcess` methods in the `HasOptions`, `HasSoarBinary`, and `WithRunable` traits from private to protected.
- Add `@noinspection PhpUnused` annotations to the `dumpSoarDocblock` and `dumpSoarConfig` methods in the `ComposerScript` class.
- Add `@noinspection PhpUnused` annotation to the `escapeCommand` method in the `EscapeArg` class.
- Add `@noinspection PhpUnused` annotation to the `OS` class.
  • Loading branch information
guanguans committed Jan 16, 2024
1 parent ac5ab21 commit 361cc04
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 11 deletions.
11 changes: 7 additions & 4 deletions src/Concerns/ConcreteMagic.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
trait ConcreteMagic
{
public function __sleep()
public function __sleep(): array
{
return ['options', 'soarBinary'];
}
Expand Down Expand Up @@ -49,12 +49,15 @@ public function __wakeup(): void
// $this->setSoarBinary($data['soarBinary']);
// }

public function __debugInfo()
public function __debugInfo(): array
{
return get_object_vars($this) + ['commandLine' => (string) $this];
}

public static function __set_state(array $properties)
/**
* @noinspection MagicMethodsValidityInspection
*/
public static function __set_state(array $properties): self
{
return new static($properties['options'], $properties['soarBinary']);
}
Expand All @@ -64,7 +67,7 @@ public static function __set_state(array $properties)
*
* @noinspection MagicMethodsValidityInspection
*/
public function __toString()
public function __toString(): string
{
return $this->createProcess()->getCommandLine();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Concerns/HasOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,7 @@ protected function getNormalizedOptions(): array
/**
* @throws \Guanguans\SoarPHP\Exceptions\InvalidOptionException
*/
private function normalizeOptions(array $options): array
protected function normalizeOptions(array $options): array
{
return array_reduce_with_keys($options, function (array $normalizedOptions, $value, $key): array {
if ($normalizedOption = $this->normalizeOption($key, $value)) {
Expand All @@ -1548,7 +1548,7 @@ private function normalizeOptions(array $options): array
*
* @throws \Guanguans\SoarPHP\Exceptions\InvalidOptionException
*/
private function normalizeOption($key, $value): string
protected function normalizeOption($key, $value): string
{
$converter = function ($value) {
\is_callable($value) and ! (\is_string($value) && \function_exists($value)) and $value = $value($this);
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/HasSoarBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function getEscapedSoarBinary(): string
return escape_argument($this->soarBinary);
}

private function getDefaultSoarBinary(): string
protected function getDefaultSoarBinary(): string
{
if (OS::isWindows()) {
return __DIR__.'/../../bin/soar.windows-amd64'; // @codeCoverageIgnore
Expand Down
6 changes: 3 additions & 3 deletions src/Concerns/WithRunable.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function setProcessTapper(?callable $processTapper): self
}

/**
* @param array|string $withOptions
* @param array<string>|string $withOptions
*
* @throws InvalidOptionException
*/
Expand All @@ -53,11 +53,11 @@ public function run($withOptions = [], ?callable $callback = null): string
}

/**
* @param array|string $withOptions
* @param array<string>|string $withOptions
*
* @throws InvalidOptionException
*/
private function createProcess($withOptions = []): Process
protected function createProcess($withOptions = []): Process
{
$process = \is_string($withOptions)
? Process::fromShellCommandline("{$this->getEscapedSoarBinary()} {$this->getHydratedEscapedNormalizedOptions()} $withOptions")
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Soar.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
interface Soar
{
/**
* @param array|string $withOptions
* @param array<string>|string $withOptions
*/
public function run($withOptions = [], ?callable $callback = null): string;

Expand Down
2 changes: 2 additions & 0 deletions src/Support/ComposerScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ComposerScript
{
/**
* @throws InvalidOptionException
* @noinspection PhpUnused
*/
public static function dumpSoarDocblock(Event $event): int
{
Expand Down Expand Up @@ -94,6 +95,7 @@ public static function dumpSoarDocblock(Event $event): int

/**
* @throws InvalidOptionException
* @noinspection PhpUnused
*/
public static function dumpSoarConfig(Event $event): int
{
Expand Down
1 change: 1 addition & 0 deletions src/Support/EscapeArg.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public static function escape($arg, bool $meta = true, bool $module = false): st
* @param bool $meta Additionally escape cmd.exe meta characters
*
* @return string The escaped command line
* @noinspection PhpUnused
*/
public static function escapeCommand(array $args, bool $meta = true): string
{
Expand Down
2 changes: 2 additions & 0 deletions src/Support/OS.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

/** @noinspection PhpUnused */

declare(strict_types=1);

/**
Expand Down

0 comments on commit 361cc04

Please sign in to comment.