Skip to content

Commit

Permalink
chevere 4
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Nov 17, 2023
1 parent 3d97c6c commit 3c7f567
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 105 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
],
"require": {
"amphp/parallel": "^1.4",
"chevere/chevere": "^4.0.x-dev"
"chevere/chevere": "^4.0.x-dev",
"ramsey/uuid": "^4.7"
},
"require-dev": {
"chevere/var-dump": "^0.10.x-dev",
Expand Down
8 changes: 5 additions & 3 deletions src/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
use Chevere\DataStructure\Map;
use Chevere\DataStructure\Traits\MapTrait;
use Chevere\DataStructure\Vector;
use Chevere\Throwable\Exceptions\InvalidArgumentException;
use Chevere\Workflow\Interfaces\GraphInterface;
use Chevere\Workflow\Interfaces\JobInterface;
use InvalidArgumentException;
use function Chevere\Message\message;

final class Graph implements GraphInterface
Expand Down Expand Up @@ -172,8 +172,10 @@ private function assertNotSelfDependency(string $job, VectorInterface $vector):
}

throw new InvalidArgumentException(
message('Cannot declare job %job% as a self-dependency')
->withCode('%job%', $job)
(string) message(
'Cannot declare job **%job%** as a self-dependency',
job: $job
)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/WorkflowInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

use Chevere\Parameter\Interfaces\ParameterInterface;
use Chevere\Parameter\Interfaces\ParametersInterface;
use Chevere\Throwable\Exceptions\OverflowException;
use Countable;
use OverflowException;

/**
* Describes the component in charge of defining a collection of chained tasks.
Expand Down
39 changes: 22 additions & 17 deletions src/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@

namespace Chevere\Workflow;

use ArgumentCountError;
use BadMethodCallException;
use Chevere\Action\Interfaces\ActionInterface;
use Chevere\DataStructure\Interfaces\VectorInterface;
use Chevere\DataStructure\Vector;
use Chevere\Parameter\Interfaces\ParameterInterface;
use Chevere\Parameter\Interfaces\ParametersInterface;
use Chevere\String\StringAssert;
use Chevere\Throwable\Errors\ArgumentCountError;
use Chevere\Throwable\Exceptions\BadMethodCallException;
use Chevere\Throwable\Exceptions\OverflowException;
use Chevere\Workflow\Interfaces\JobInterface;
use Chevere\Workflow\Interfaces\ResponseReferenceInterface;
use Chevere\Workflow\Interfaces\VariableInterface;
use OverflowException;
use function Chevere\Action\getParameters;
use function Chevere\Message\message;
use function Chevere\Parameter\assertNamedArgument;
Expand Down Expand Up @@ -76,8 +76,10 @@ public function withRunIf(ResponseReferenceInterface|VariableInterface ...$conte
foreach ($context as $item) {
if ($known->contains($item->__toString())) {
throw new OverflowException(
message('Condition %condition% is already defined')
->withCode('%condition%', $item->__toString())
(string) message(
'Condition `%condition%` is already defined',
condition: $item->__toString()
)
);
}
$new->inferDependencies($item);
Expand Down Expand Up @@ -147,9 +149,11 @@ private function setArguments(mixed ...$argument): void
}
if ($missing !== []) {
throw new BadMethodCallException(
message('Missing argument(s) [%arguments%] for %action%')
->withCode('%arguments%', implode(', ', $missing))
->withCode('%action%', $this->action::class)
(string) message(
'Missing argument(s) [`%arguments%`] for `%action%`',
arguments: implode(', ', $missing),
action: $this->action::class
)
);
}
$this->arguments = $values;
Expand All @@ -169,10 +173,12 @@ private function assertArgumentsCount(array $arguments): void
$parameters = $parameters === '' ? '' : "[{$parameters}]";

throw new ArgumentCountError(
message('%symbol% requires %countRequired% argument(s) %parameters%')
->withCode('%symbol%', $this->action::class . '::run')
->withCode('%countRequired%', strval($countRequired))
->withCode('%parameters%', $parameters)
(string) message(
'`%symbol%` requires %countRequired% argument(s) `%parameters%`',
symbol: $this->action::class . '::run',
countRequired: strval($countRequired),
parameters: $parameters
)
);
}
}
Expand Down Expand Up @@ -214,11 +220,10 @@ private function assertDependencies(string ...$dependencies): void
$uniques = array_unique($dependencies);
if ($uniques !== $dependencies) {
throw new OverflowException(
message('Job dependencies must be unique (repeated %dependencies%)')
->withCode(
'%dependencies%',
implode(', ', array_diff_assoc($dependencies, $uniques))
)
(string) message(
'Job dependencies must be unique (repeated **%dependencies%**)',
dependencies: implode(', ', array_diff_assoc($dependencies, $uniques))
)
);
}
foreach ($dependencies as $dependency) {
Expand Down
92 changes: 55 additions & 37 deletions src/Jobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
use Chevere\Parameter\Interfaces\BoolParameterInterface;
use Chevere\Parameter\Interfaces\ParameterInterface;
use Chevere\Parameter\Interfaces\ParametersAccessInterface;
use Chevere\Throwable\Errors\TypeError;
use Chevere\Throwable\Exceptions\InvalidArgumentException;
use Chevere\Throwable\Exceptions\OutOfBoundsException;
use Chevere\Throwable\Exceptions\OverflowException;
use Chevere\Workflow\Interfaces\GraphInterface;
use Chevere\Workflow\Interfaces\JobInterface;
use Chevere\Workflow\Interfaces\JobsInterface;
use Chevere\Workflow\Interfaces\ResponseReferenceInterface;
use Chevere\Workflow\Interfaces\VariableInterface;
use InvalidArgumentException;
use OutOfBoundsException;
use OverflowException;
use Throwable;
use TypeError;
use function Chevere\Action\getParameters;
use function Chevere\Message\message;
use function Chevere\Parameter\bool;
Expand Down Expand Up @@ -112,8 +112,10 @@ private function addMap(string $name, JobInterface $job): void
{
if ($this->map->has($name)) {
throw new OverflowException(
message('Job name %name% has been already added.')
->withCode('%name%', $name)
(string) message(
'Job name `%name%` has been already added.',
name: $name
)
);
}
$this->map = $this->map->withPut($name, $job);
Expand Down Expand Up @@ -177,9 +179,10 @@ private function handleArguments(string $job, JobInterface $item): void
$this->mapParameter($job, $argument, $collection, $parameter, $value);
} catch (Throwable $e) {
throw new $e(
message($e->getMessage())
->withTranslate('%parameter%', $argument)
->withTranslate('%job%', $job)
strtr($e->getMessage(), [
'%parameter%' => $argument,
'%job%' => $job,
])
);
}
}
Expand Down Expand Up @@ -207,17 +210,21 @@ private function mapParameter(
if ($value->key() !== null) {
if (! $accept instanceof ParametersAccessInterface) {
throw new TypeError(
message('Reference %reference% doesn\'t accept parameters')
->withCode('%reference%', strval($value))
(string) message(
"Reference **%reference%** doesn't accept parameters",
reference: strval($value)
)
);
}
$accept->parameters()->get($value->key());
}
} catch (OutOfBoundsException) {
throw new OutOfBoundsException(
message('%subject% %key% not found at job %job%')
->withTranslate('%subject%', $subject)
->withTranslate('%key%', $identifier)
(string) message(
'%subject% **%key%** not found at job **%job%**',
subject: $subject,
key: $identifier
)
);
}
}
Expand All @@ -231,25 +238,28 @@ private function mapParameter(
$stored = $map->get($identifier);
if ($stored::class !== $parameter::class) {
throw new TypeError(
message('%subject% %key% is of type %type%, parameter %parameter% expects %expected% at job %job%')
->withCode('%type%', $stored->type()->primitive())
->withCode('%expected%', $parameter->type()->primitive())
->withTranslate('%subject%', $subject)
->withTranslate('%key%', $identifier)
(string) message(
'%subject% **%key%** is of type `%type%`, parameter **%parameter%** expects `%expected%` at job **%job%**',
type: $stored->type()->primitive(),
expected: $parameter->type()->primitive(),
subject: $subject,
key: $identifier
)
);
}

try {
$stored->assertCompatible($parameter);
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException(
message('%subject% %key% conflict for parameter %parameter% on Job %job% (%message%).')
->withCode('%subject%', $subject)
->withCode('%key%', $identifier)
->withCode('%parameter%', $argument)
->withTranslate('%subject%', $subject)
->withTranslate('%job%', $job)
->withTranslate('%message%', $e->getMessage())
(string) message(
'%subject% **%key%** conflict for parameter **%parameter%** on job **%job%** (%message%).',
subject: $subject,
key: $identifier,
parameter: $argument,
job: $job,
message: $e->getMessage()
)
);
}
}
Expand All @@ -264,8 +274,10 @@ private function handleRunIfReference(mixed $runIf): void
if ($runIf->key() !== null) {
if (! $accept instanceof ParametersAccessInterface) {
throw new TypeError(
message('Reference %reference% doesn\'t accept parameters')
->withCode('%reference%', strval($runIf))
(string) message(
'Reference **%reference%** doesn\'t accept parameters',
reference: strval($runIf)
)
);
}
$accept = $accept->parameters()->get($runIf->key());
Expand All @@ -275,8 +287,10 @@ private function handleRunIfReference(mixed $runIf): void
}

throw new TypeError(
message('Reference %reference% must be of type bool')
->withCode('%reference%', strval($runIf))
(string) message(
'Reference **%reference%** must be of type `bool`',
reference: strval($runIf)
)
);
}

Expand All @@ -298,10 +312,12 @@ private function handleRunIfVariable(string $name, mixed $runIf): void
$parameter = $this->variables->get($runIf->__toString());
if (! ($parameter instanceof BoolParameterInterface)) {
throw new TypeError(
message('Variable %variable% (previously declared as %type%) is not of type boolean at Job %job%')
->withCode('%variable%', $runIf->__toString())
->withCode('%type%', $parameter->type()->primitive())
->withCode('%job%', $name)
(string) message(
'Variable **%variable%** (previously declared as `%type%`) is not of type `bool` at Job **%job%**',
variable: $runIf->__toString(),
type: $parameter->type()->primitive(),
job: $name,
)
);
}
}
Expand All @@ -316,9 +332,11 @@ private function assertDependencies(string $job): void
);

throw new OutOfBoundsException(
message('Job %job% has undeclared dependencies: %dependencies%')
->withCode('%job%', $job)
->withCode('%dependencies%', implode(', ', $missing))
(string) message(
'Job **%job%** has undeclared dependencies: `%dependencies%`',
job: $job,
dependencies: implode(', ', $missing),
)
);
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
use Chevere\Parameter\Arguments;
use Chevere\Parameter\Interfaces\ArgumentsInterface;
use Chevere\Parameter\Interfaces\CastInterface;
use Chevere\Throwable\Exceptions\OverflowException;
use Chevere\Workflow\Interfaces\RunInterface;
use Chevere\Workflow\Interfaces\WorkflowInterface;
use OverflowException;
use Ramsey\Uuid\Uuid;
use function Chevere\Message\message;
use function Chevere\Parameter\assertArgument;
Expand Down Expand Up @@ -121,7 +121,9 @@ private function assertNoSkipOverflow(string $job, MessageInterface $message): v
{
if ($this->skip->contains($job)) {
throw new OverflowException(
$message->withCode('%job%', $job)
strtr($message->__toString(), [

Check warning on line 124 in src/Run.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ private function assertNoSkipOverflow(string $job, MessageInterface $message) : void { if ($this->skip->contains($job)) { - throw new OverflowException(strtr($message->__toString(), ['%job%' => $job])); + throw new OverflowException(strtr($message->__toString(), [])); } } }

Check warning on line 124 in src/Run.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ private function assertNoSkipOverflow(string $job, MessageInterface $message) : void { if ($this->skip->contains($job)) { - throw new OverflowException(strtr($message->__toString(), ['%job%' => $job])); + throw new OverflowException(strtr($message->__toString(), [])); } } }
'%job%' => $job,
])
);
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
use Amp\Promise;
use Chevere\Action\Interfaces\ActionInterface;
use Chevere\Parameter\Interfaces\CastInterface;
use Chevere\Throwable\Exceptions\InvalidArgumentException;
use Chevere\Throwable\Exceptions\OutOfBoundsException;
use Chevere\Workflow\Interfaces\JobInterface;
use Chevere\Workflow\Interfaces\ResponseReferenceInterface;
use Chevere\Workflow\Interfaces\RunInterface;
use Chevere\Workflow\Interfaces\RunnerInterface;
use Chevere\Workflow\Interfaces\VariableInterface;
use InvalidArgumentException;
use OutOfBoundsException;
use Throwable;
use function Amp\Parallel\Worker\enqueueCallable;
use function Amp\Promise\all;
Expand Down Expand Up @@ -118,10 +118,12 @@ private function getActionResponse(

throw new InvalidArgumentException(
previous: $e,
message: message('%message% at %fileLine% for action %action%')
->withTranslate('%message%', $e->getMessage())
->withCode('%fileLine%', $fileLine)
->withCode('%action%', $action::class)
message: (string) message(
'%message% at `%fileLine%` for action `%action%`',
message: $e->getMessage(),
fileLine: $fileLine,
action: $action::class,
)
);
}
// @codeCoverageIgnoreEnd
Expand Down
8 changes: 5 additions & 3 deletions src/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace Chevere\Workflow;

use Chevere\Regex\Regex;
use Chevere\Throwable\Exceptions\InvalidArgumentException;
use Chevere\Workflow\Interfaces\VariableInterface;
use InvalidArgumentException;
use function Chevere\Message\message;

final class Variable implements VariableInterface
Expand All @@ -27,8 +27,10 @@ public function __construct(
->match($name);
if ($matches === []) {
throw new InvalidArgumentException(
message('Invalid variable name %name%')
->withCode('%name%', $name)
(string) message(
'Invalid variable name `%name%`',
name: $name
)
);
}
}
Expand Down
Loading

0 comments on commit 3c7f567

Please sign in to comment.