Skip to content

Commit

Permalink
% PHPSTAN refactoring classes
Browse files Browse the repository at this point in the history
  • Loading branch information
EdmondDantes committed Oct 27, 2024
1 parent f466c91 commit 57707c3
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 152 deletions.
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ parameters:
#- identifier: instanceof.alwaysTrue
#- identifier: instanceof.alwaysFalse
- identifier: identical.alwaysFalse
# - '#Strict comparison using === between true and false will always evaluate to false#'
# - '#Strict comparison using === between true and false will always evaluate to false#'
- '#Result of && is always false#'
10 changes: 6 additions & 4 deletions src/ArraySerializerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@

namespace IfCastle\Exceptions;

use PHPUnit\Event\Code\Throwable;

trait ArraySerializerTrait
{
/**
* The method defines the source of the exception.
*
*
* @return array<string, scalar>|string
*/
abstract protected function getSourceFor(\Throwable $e, bool $isString = false): array|string;

/**
* The method serialized errors BaseExceptionI to an array.
*
* @param array|BaseExceptionInterface $errors array of errors
* @param BaseExceptionInterface[]|\Throwable[]|BaseExceptionInterface $errors array of errors
* @return array<mixed|mixed[]>
*/
protected function errorsToArray(mixed $errors): array
{
Expand Down Expand Up @@ -48,7 +50,7 @@ protected function errorsToArray(mixed $errors): array
/**
* The method deserialized array of array to array of errors.
*
* @param array $array array of array
* @param array<scalar[]|array<scalar>> $array array of array
* @param string $class class for exception
*
* @return BaseException[]
Expand Down
15 changes: 13 additions & 2 deletions src/BaseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class BaseException extends \Exception implements BaseExceptionInterface
use HelperTrait;
use ArraySerializerTrait;
use TemplateHandlerTrait;

/**
* @param \Throwable|BaseExceptionInterface|null $throwable
*
* @return array<string, scalar|array<string, scalar[]>>
*/
public static function serializeToArray(\Throwable|BaseExceptionInterface|null $throwable = null): array
{
if ($throwable instanceof BaseExceptionInterface) {
Expand All @@ -59,6 +65,7 @@ public static function serializeToArray(\Throwable|BaseExceptionInterface|null $

/**
* Layout of the default properties.
* @var array<string, scalar>
*/
protected static array $baseProps = ['message' => '', 'code' => 0, 'previous' => null, 'template' => '', 'tags' => []];

Expand All @@ -69,6 +76,7 @@ public static function serializeToArray(\Throwable|BaseExceptionInterface|null $

/**
* Extra data to exception.
* @var array<string, scalar|object|array<string, scalar|object>>
*/
protected array $data = [];

Expand All @@ -80,11 +88,13 @@ public static function serializeToArray(\Throwable|BaseExceptionInterface|null $

/**
* Source of error.
* @var array<string, string>
*/
protected ?array $source = null;

/**
* Debug data.
* @var array<string, scalar|array<string, scalar>>
*/
protected array $debugData = [];

Expand Down Expand Up @@ -127,7 +137,7 @@ public static function serializeToArray(\Throwable|BaseExceptionInterface|null $
* In this case, exception acts as container,
* and inherits data from the $exception
*
* @param BaseExceptionInterface|\Throwable|array|string $exception Exception data
* @param BaseExceptionInterface|\Throwable|array<string, scalar|array<string, scalar>|scalar[]>|string $exception Exception data
* @param int $code Code
* @param \Throwable|null $previous Previous or aggregate exception
*/
Expand Down Expand Up @@ -421,6 +431,7 @@ public function toArray(): array
* Returns information about a value type.
*
* @param mixed $value Value
* @return string|array<string, scalar|mixed[]>
*/
protected function typeInfo(mixed $value): string|array
{
Expand All @@ -442,7 +453,7 @@ protected function isDebug(): bool
/**
* The method saved debug data if debug mode is active.
*
* @param array $data debug data
* @param array<string, scalar|mixed[]> $data debug data
* @return $this
*/
protected function setDebugData(array $data): static
Expand Down
24 changes: 16 additions & 8 deletions src/BaseExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Main interface for BaseException.
*/
interface BaseExceptionInterface extends \Stringable
interface BaseExceptionInterface extends \Throwable
{
/**
* The System is unusable.
Expand Down Expand Up @@ -64,19 +64,19 @@ interface BaseExceptionInterface extends \Stringable
*/
final public const int RESULT = 1;

public function getMessage();
public function getMessage(): string;

public function getPrevious();
public function getPrevious(): ?\Throwable;

public function getCode();

public function getFile();
public function getFile(): string;

public function getLine();
public function getLine(): int;

public function getTrace();
public function getTrace(): array;

public function getTraceAsString();
public function getTraceAsString(): string;

/**
* Template message.
Expand Down Expand Up @@ -142,6 +142,8 @@ public function getLevel(): int;
* ]
*
* Attention to the order of elements in the array is important!
*
* @return array<string, scalar>|null
*/
public function getSource(): ?array;

Expand All @@ -159,21 +161,27 @@ public function getPreviousException(): \Throwable|BaseExceptionInterface|null;

/**
* The method returns extra data for exception.
*
* @return array<string, scalar|scalar[]>
*/
public function getExceptionData(): array;

/**
* @param array $data The additional data
* @param array<string, scalar|scalar[]|mixed> $data The additional data
*/
public function appendData(array $data): static;

/**
* The method returns debug data for exception.
*
* @return array<string, scalar|scalar[]|mixed>
*/
public function getDebugData(): array;

/**
* The method serialized object to an array.
*
* @return array<string, scalar|scalar[]>
*/
public function toArray(): array;
}
2 changes: 1 addition & 1 deletion src/ClassNotExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ClassNotExist extends LoggableException
/**
* ClassNotExist.
*
* @param string|array $class Class name
* @param string|array<string, scalar|scalar[]> $class Class name
*/
public function __construct(array|string $class)
{
Expand Down
2 changes: 2 additions & 0 deletions src/ClientAvailableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function getClientMessage(): string;

/**
* Serialize exception for client.
*
* @return array<string, scalar|scalar[]>
*/
public function clientSerialize(): array;
}
5 changes: 5 additions & 0 deletions src/ClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
class ClientException extends BaseException implements ClientAvailableInterface
{
/**
* @param string $template
* @param array<string, scalar|scalar[]> $parameters
* @param array<string, scalar|scalar[]> $debugData
*/
public function __construct(string $template, array $parameters = [], array $debugData = [])
{
parent::__construct(['template' => $template] + $parameters);
Expand Down
95 changes: 34 additions & 61 deletions src/Errors/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

/**
* The class for encapsulating of PHP Errors
* as object BaseExceptionI.
* as object BaseExceptionInterface.
*/
class Error implements BaseExceptionInterface
class Error extends \ErrorException implements BaseExceptionInterface
{

/**
* Conformity between PHP-errors and BaseExceptionI.
*/
* Conformity between PHP-errors and BaseException
* @var array<int, int>
*/
protected static array $ERRORS =
[
E_ERROR => self::ERROR,
Expand All @@ -33,7 +35,10 @@ class Error implements BaseExceptionInterface
E_DEPRECATED => self::INFO,
E_USER_DEPRECATED => self::INFO,
];


/**
* @var mixed[]|null
*/
protected ?array $trace = null;

/**
Expand All @@ -45,8 +50,13 @@ class Error implements BaseExceptionInterface
* Fatal error flag.
*/
protected bool $isFatal = false;

public static function createFromLastError(?array $error = null): ?static

/**
* @param array<string, scalar>|null $error
*
* @return BaseExceptionInterface|null
*/
public static function createFromLastError(?array $error = null): ?BaseExceptionInterface
{
if ($error === null) {
return null;
Expand All @@ -65,9 +75,9 @@ public static function createFromLastError(?array $error = null): ?static
* @param string $file File
* @param int $line Line
*
* @return Error
* @return BaseExceptionInterface
*/
public static function createError(int $code, string $message, string $file, int $line): static
public static function createError(int $code, string $message, string $file, int $line): BaseExceptionInterface
{
if (!\array_key_exists($code, self::$ERRORS)) {
$code = self::ERROR;
Expand Down Expand Up @@ -107,64 +117,24 @@ public static function createError(int $code, string $message, string $file, int
}
}
}

/**
* Errors constructor.
*/
public function __construct(protected int $code, protected string $message, protected string $file, protected int $line) {}

#[\Override]
public function getMessage(): string
public function __construct(int $severity = \E_ERROR,
string $message = '',
?string $filename = null,
?int $line = null,
int $code = 0,
?\Throwable $previous = null
)
{
return $this->message;
parent::__construct($message, $code, $severity, $filename, $line, $previous);
}

#[\Override]
public function getPrevious(): null
{
return null;
}


#[\Override]
public function getTags(): array
{
return [];
}

#[\Override]
public function getCode(): int
{
return $this->code;
}

#[\Override]
public function getFile(): string
{
return $this->file;
}

#[\Override]
public function getLine(): int
{
return $this->line;
}

#[\Override]
public function getTrace(): ?array
{
return $this->trace;
}

#[\Override]
public function getTraceAsString(): string
{
if (empty($this->trace)) {
return '';
}

return \print_r($this->trace, true);
}


#[\Override]
public function isLoggable(): bool
{
Expand Down Expand Up @@ -211,7 +181,10 @@ public function getLevel(): int

return self::$ERRORS[$this->code];
}


/**
* @return array{source: string, type: string, function: string}
*/
#[\Override]
public function getSource(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/FatalRuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
namespace IfCastle\Exceptions;

/**
* Fatal exception with aspect: "Runtime".
* Fatal exception with an aspect: "Runtime".
*/
class FatalRuntimeException extends FatalException implements RuntimeExceptionInterface {}
2 changes: 1 addition & 1 deletion src/FatalSystemException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
namespace IfCastle\Exceptions;

/**
* Fatal exception with aspect: "System".
* Fatal exception with an aspect: "System".
*/
class FatalSystemException extends FatalException implements SystemExceptionInterface {}
Loading

0 comments on commit 57707c3

Please sign in to comment.