Skip to content

Commit

Permalink
Fixed PHPStan issues
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wright <tom@inflatablecookie.com>
  • Loading branch information
betterthanclay committed Jul 23, 2024
1 parent 316d406 commit cc9b565
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 47 deletions.
43 changes: 26 additions & 17 deletions src/Blueprint/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

namespace DecodeLabs\Chronos\Blueprint;

use Carbon\CarbonInterval;
use DateTimeInterface;
use DecodeLabs\Chronos\Blueprint;
use DecodeLabs\Chronos\BlueprintTrait;
use DecodeLabs\Chronos\Priority;
use DecodeLabs\Dictum;
use DecodeLabs\Chronos\Runtime\Parameter;
use DecodeLabs\Exceptional;

/**
* @phpstan-import-type ParameterValue from Parameter
*/
class Action implements Blueprint
{
use BlueprintTrait;
Expand All @@ -25,27 +26,27 @@ class Action implements Blueprint
protected ?string $return = null;

/**
* @var array<string,Parameter>
* @phpstan-var array<string,Parameter<ParameterValue>>
*/
protected array $parameters = [];

/**
* @param array<string|int|float|bool|array<string>|DateTimeInterface|Parameter> $parameters
* @phpstan-param array<string,ParameterValue|Parameter<ParameterValue>> $parameters
*/
public function __construct(
?string $signature = null,
?string $initiator = null,
?string $return = null,
array $parameters = [],
) {
if($signature !== null) {
if ($signature !== null) {
[
'initiator' => $initiator,
'return' => $return,
] = $this->parseSignature($signature);
}

if($initiator === null) {
if ($initiator === null) {
throw Exceptional::InvalidArgument(
'Action signature is missing initiator'
);
Expand All @@ -58,6 +59,8 @@ public function __construct(

/**
* Parse signature
*
* @return array{initiator: string, return: string|null}
*/
public function parseSignature(
string $signature
Expand All @@ -77,8 +80,8 @@ public function getSignature(): string
{
$output = $this->initiator;

if($this->return !== null) {
$output .= ':'.$this->return;
if ($this->return !== null) {
$output .= ':' . $this->return;
}

return $output;
Expand All @@ -90,9 +93,9 @@ public function getSignature(): string
public function setInitiator(
string $initiator
): void {
if(!preg_match('/^([A-Z][a-zA-Z0-9]+)\.([A-Z][a-zA-Z0-9]+)$/', $initiator)) {
if (!preg_match('/^([A-Z][a-zA-Z0-9]+)\.([A-Z][a-zA-Z0-9]+)$/', $initiator)) {
throw Exceptional::InvalidArgument(
'Invalid action initiator: '.$initiator
'Invalid action initiator: ' . $initiator
);
}

Expand All @@ -113,12 +116,12 @@ public function getInitiator(): string
public function setReturn(
?string $return
): void {
if(
if (
$return !== null &&
!preg_match('/^\$?[a-zA-Z0-9]+$/', $return)
) {
throw Exceptional::InvalidArgument(
'Invalid action return: '.$return
'Invalid action return: ' . $return
);
}

Expand All @@ -137,20 +140,20 @@ public function getReturn(): ?string
/**
* Set parameters
*
* @param array<string,string|int|float|bool|array<string>|DateTimeInterface|Parameter> $parameters
* @phpstan-param array<string,ParameterValue|Parameter<ParameterValue>> $parameters
*/
public function setParameters(
array $parameters
): void {
foreach($parameters as $name => $parameter) {
foreach ($parameters as $name => $parameter) {
$this->addParameter($name, $parameter);
}
}

/**
* Get parameters
*
* @return array<string,Parameter>
* @phpstan-return array<string,Parameter<ParameterValue>>
*/
public function getParameters(): array
{
Expand All @@ -159,12 +162,14 @@ public function getParameters(): array

/**
* Add parameter
*
* @phpstan-param ParameterValue|Parameter<ParameterValue> $parameter
*/
public function addParameter(
string $name,
string|int|float|bool|array|DateTimeInterface|Parameter $parameter
): void {
if(!$parameter instanceof Parameter) {
if (!$parameter instanceof Parameter) {
$parameter = new Parameter($parameter);
}

Expand All @@ -173,6 +178,8 @@ public function addParameter(

/**
* Get parameter
*
* @return Parameter<ParameterValue>|null
*/
public function getParameter(
string $id
Expand All @@ -182,6 +189,8 @@ public function getParameter(

/**
* Export for serialization
*
* @return array<string,mixed>
*/
public function jsonSerialize(): array
{
Expand Down
8 changes: 5 additions & 3 deletions src/Blueprint/Program.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function setSteps(
): void {
$this->steps = [];

foreach($steps as $step) {
foreach ($steps as $step) {
$this->addStep($step);
}
}
Expand All @@ -144,9 +144,9 @@ public function addStep(
): void {
$id = $step->getId();

if(isset($this->steps[$id])) {
if (isset($this->steps[$id])) {
throw Exceptional::InvalidArgument(
'Step '.$id.' is already defined'
'Step ' . $id . ' is already defined'
);
}

Expand All @@ -165,6 +165,8 @@ public function getStep(

/**
* Export for serialization
*
* @return array<string,mixed>
*/
public function jsonSerialize(): array
{
Expand Down
8 changes: 6 additions & 2 deletions src/Blueprint/Step.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public function setAwaits(

/**
* Get awaits
*
* @return array<string,CarbonInterval|null>
*/
public function getAwaits(): array
{
Expand All @@ -111,7 +113,7 @@ public function addAwait(
string $id,
string|CarbonInterval|null $duration
): void {
if($duration !== null) {
if ($duration !== null) {
$duration = CarbonInterval::make($duration);
}

Expand Down Expand Up @@ -139,12 +141,14 @@ public function willAwait(

/**
* Export for serialization
*
* @return array<string,mixed>
*/
public function jsonSerialize(): array
{
$await = [];

foreach($this->await as $key => $value) {
foreach ($this->await as $key => $value) {
$await[$key] = $value ? (string)$value : null;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Blueprint/WithActionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,18 @@ public function addAction(

/**
* Export for serialization
*
* @return array<string,Action>
*/
public function jsonSerialize(): array
{
if(empty($this->actions)) {
if (empty($this->actions)) {
return [];
}

$output = [];

foreach($this->actions as $action) {
foreach ($this->actions as $action) {
$output[$action->getSignature()] = $action;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Blueprint/WithDurationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(
public function setDuration(
string|CarbonInterval|null $duration
): void {
if($duration !== null) {
if ($duration !== null) {
$duration = CarbonInterval::make($duration);
}

Expand All @@ -44,6 +44,8 @@ public function getDuration(): ?CarbonInterval

/**
* Export for serialization
*
* @return array<string,string>
*/
public function jsonSerialize(): array
{
Expand Down
6 changes: 4 additions & 2 deletions src/Blueprint/WithIdentityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public function __construct(
?string $name,
?string $description = null
) {
if(
if (
$id === null &&
$name === null
) {
throw Exceptional::InvalidArgument(
'Id or name must be provided to blueprints'
);
} elseif($id === null) {
} elseif ($id === null) {
$id = Dictum::slug($name);
} else {
$name = Dictum::name($id);
Expand Down Expand Up @@ -95,6 +95,8 @@ public function getDescription(): ?string

/**
* Export for serialization
*
* @return array<string,mixed>
*/
public function jsonSerialize(): array
{
Expand Down
3 changes: 2 additions & 1 deletion src/Blueprint/WithPriorityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace DecodeLabs\Chronos\Blueprint;

use DecodeLabs\Chronos\Blueprint;
use DecodeLabs\Chronos\Priority;

trait WithPriorityTrait
Expand Down Expand Up @@ -41,6 +40,8 @@ public function getPriority(): Priority

/**
* Export for serialization
*
* @return array<string,string>
*/
public function jsonSerialize(): array
{
Expand Down
4 changes: 0 additions & 4 deletions src/Blueprint/WithPublishing.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@

namespace DecodeLabs\Chronos\Blueprint;

use Carbon\CarbonInterval;
use DecodeLabs\Chronos\Blueprint;
use DecodeLabs\Chronos\BlueprintTrait;
use DecodeLabs\Chronos\Priority;
use DecodeLabs\Exceptional;

interface WithPublishing extends Blueprint
{
Expand Down
4 changes: 3 additions & 1 deletion src/Blueprint/WithPublishingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function setVersion(

public function getVersion(): string
{
if($this->version === null) {
if ($this->version === null) {
return '0.0.1';
}

Expand Down Expand Up @@ -80,6 +80,8 @@ public function getAuthorUrl(): ?string

/**
* Export for serialization
*
* @return array<string,mixed>
*/
public function jsonSerialize(): array
{
Expand Down
2 changes: 0 additions & 2 deletions src/BlueprintTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace DecodeLabs\Chronos;

use JsonSerializable;

trait BlueprintTrait
{
/**
Expand Down
File renamed without changes.
17 changes: 10 additions & 7 deletions src/Runtime/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use DecodeLabs\Chronos\Blueprint\ParameterType;

/**
* @phpstan-type ParameterValue string|int|float|bool|array<string>|DateTimeInterface
* @template T of string|int|float|bool|array<string>|DateTimeInterface
*/
class Parameter
Expand All @@ -21,31 +22,33 @@ class Parameter
* @var T
*/
protected string|int|float|bool|array|DateTimeInterface $value;

protected ParameterType $type;

/**
* @param T $value
*/
public function __construct(
string|int|float|bool|array|DateTimeInterface $value
) {
$this->value = $value;

if(is_string($value)) {
if(preg_match('/^\{\{([a-zA-Z0-9]+\}\}$/', $value, $matches)) {
if (is_string($value)) {
if (preg_match('/^\{\{([a-zA-Z0-9]+)\}\}$/', $value, $matches)) {
$this->value = $matches[1];
$this->type = ParameterType::Reference;
} else {
$this->type = ParameterType::String;
}
} elseif(
} elseif (
is_int($value) ||
is_float($value)
) {
$this->type = ParameterType::Number;
} elseif(is_bool($value)) {
} elseif (is_bool($value)) {
$this->type = ParameterType::Boolean;
} elseif(is_array($value)) {
} elseif (is_array($value)) {
$this->type = ParameterType::List;
} elseif($value instanceof DateTimeInterface) {
} elseif ($value instanceof DateTimeInterface) {
$this->type = ParameterType::Date;
}
}
Expand Down
Loading

0 comments on commit cc9b565

Please sign in to comment.