Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catch RejectedItemExceptions and log them instead of stopping the pipeline #27

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 110 additions & 31 deletions src/Builder/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
use PhpParser\Builder;
use PhpParser\Node;

final readonly class Transformer implements StepBuilderInterface
final class Transformer implements StepBuilderInterface
{
public function __construct(private Builder|Node $mapper)
private ?Node\Expr $logger = null;

public function __construct(private readonly Builder|Node $mapper)
{
}

public function withLogger(Node\Expr $logger): self
{
$this->logger = $logger;

return $this;
}

Expand Down Expand Up @@ -54,6 +58,15 @@
),
flags: Node\Stmt\Class_::MODIFIER_PRIVATE
),
new Node\Param(
var: new Node\Expr\Variable(
name: 'logger'
),
type: new Node\Name\FullyQualified(
name: \Psr\Log\LoggerInterface::class
),
flags: Node\Stmt\Class_::MODIFIER_PUBLIC,
),
],
],
),
Expand All @@ -64,45 +77,108 @@
'stmts' => [
new Node\Stmt\Expression(
new Node\Expr\Assign(
var: new Node\Expr\Variable('line'),
var: new Node\Expr\Variable('input'),
expr: new Node\Expr\Yield_(null)
),
),
new Node\Stmt\Do_(
cond: new Node\Expr\Assign(
var: new Node\Expr\Variable('line'),
expr: new Node\Expr\Yield_(
new Node\Expr\New_(
class: new Node\Name\FullyQualified(
\Kiboko\Component\Bucket\AcceptanceResultBucket::class
),
args: [
new Node\Arg(
new Node\Expr\Variable('line'),
),
],
)
new Node\Stmt\While_(
cond: new Node\Expr\BinaryOp\NotIdentical(
left: new Node\Expr\Variable('input'),
right: new Node\Expr\ConstFetch(
new Node\Name('null')
)
),
stmts: [
new Node\Stmt\Expression(
new Node\Expr\Assign(
var: new Node\Expr\Variable('line'),
expr: new Node\Expr\FuncCall(
name: new Node\Expr\PropertyFetch(
var: new Node\Expr\Variable('this'),
name: new Node\Identifier('mapper'),
new Node\Stmt\TryCatch(
stmts: [
new Node\Stmt\Expression(
new Node\Expr\Assign(
var: new Node\Expr\Variable('line'),
expr: new Node\Expr\FuncCall(
name: new Node\Expr\PropertyFetch(
var: new Node\Expr\Variable('this'),
name: new Node\Identifier('mapper'),
),
args: [
new Node\Arg(
value: new Node\Expr\Variable('input')
),
new Node\Arg(
value: new Node\Expr\Variable('input')
),
]
),
),
args: [
new Node\Arg(
value: new Node\Expr\Variable('line')
),
],
catches: [
new Node\Stmt\Catch_(
types: [
new Node\Name\FullyQualified('Kiboko\\Contract\\Pipeline\\RejectedItemException'),
],
var: new Node\Expr\Variable('exception'),
stmts: [
new Node\Stmt\Expression(
new Node\Expr\MethodCall(
var: new Node\Expr\PropertyFetch(
var: new Node\Expr\Variable('this'),
name: new Node\Identifier('logger'),
),
name: new Node\Name('error'),

Check failure on line 127 in src/Builder/Transformer.php

View workflow job for this annotation

GitHub Actions / phpstan8

Parameter $name of class PhpParser\Node\Expr\MethodCall constructor expects PhpParser\Node\Expr|PhpParser\Node\Identifier|string, PhpParser\Node\Name given.

Check failure on line 127 in src/Builder/Transformer.php

View workflow job for this annotation

GitHub Actions / phpstan7

Parameter $name of class PhpParser\Node\Expr\MethodCall constructor expects PhpParser\Node\Expr|PhpParser\Node\Identifier|string, PhpParser\Node\Name given.

Check failure on line 127 in src/Builder/Transformer.php

View workflow job for this annotation

GitHub Actions / phpstan5

Parameter $name of class PhpParser\Node\Expr\MethodCall constructor expects PhpParser\Node\Expr|PhpParser\Node\Identifier|string, PhpParser\Node\Name given.

Check failure on line 127 in src/Builder/Transformer.php

View workflow job for this annotation

GitHub Actions / phpstan6

Parameter $name of class PhpParser\Node\Expr\MethodCall constructor expects PhpParser\Node\Expr|PhpParser\Node\Identifier|string, PhpParser\Node\Name given.
args: [

Check failure on line 128 in src/Builder/Transformer.php

View workflow job for this annotation

GitHub Actions / phpstan8

Parameter $args of class PhpParser\Node\Expr\MethodCall constructor expects array<PhpParser\Node\Arg|PhpParser\Node\VariadicPlaceholder>, array<int, PhpParser\Node\Arg|PhpParser\Node\Expr\Array_> given.

Check failure on line 128 in src/Builder/Transformer.php

View workflow job for this annotation

GitHub Actions / phpstan7

Parameter $args of class PhpParser\Node\Expr\MethodCall constructor expects array<PhpParser\Node\Arg|PhpParser\Node\VariadicPlaceholder>, array<int, PhpParser\Node\Arg|PhpParser\Node\Expr\Array_> given.
new Node\Arg(
new Node\Expr\MethodCall(
var: new Node\Expr\Variable('exception'),
name: new Node\Identifier('getMessage')
)
),
new Node\Expr\Array_([
new Node\Expr\ArrayItem(
value: new Node\Expr\Variable('input'),
key: new Node\Scalar\String_('input')
),
]),
]
)
),
new Node\Arg(
value: new Node\Expr\Variable('line')
new Node\Stmt\Expression(
new Node\Expr\Assign(
var: new Node\Expr\Variable('input'),
expr: new Node\Expr\Yield_(
new Node\Expr\New_(
class: new Node\Name\FullyQualified(
\Kiboko\Component\Bucket\RejectionResultBucket::class
),
args: [
new Node\Arg(
new Node\Expr\Variable('input'),
),
],
)
)
),
),
new Node\Stmt\Continue_(),
]
),
),
]
),
new Node\Stmt\Expression(
new Node\Expr\Assign(
var: new Node\Expr\Variable('input'),
expr: new Node\Expr\Yield_(
new Node\Expr\New_(
class: new Node\Name\FullyQualified(
\Kiboko\Component\Bucket\AcceptanceResultBucket::class
),
args: [
new Node\Arg(
new Node\Expr\Variable('line'),
),
],
)
)
)
),
],
),
Expand All @@ -114,7 +190,7 @@
),
args: [
new Node\Arg(
new Node\Expr\Variable('line'),
new Node\Expr\Variable('input'),
),
],
),
Expand All @@ -129,8 +205,11 @@
),
args: [
new Node\Arg(
$this->mapper instanceof Builder ? $this->mapper->getNode() : $this->mapper,

Check failure on line 208 in src/Builder/Transformer.php

View workflow job for this annotation

GitHub Actions / phpstan5

Parameter #1 $value of class PhpParser\Node\Arg constructor expects PhpParser\Node\Expr, PhpParser\Node given.

Check failure on line 208 in src/Builder/Transformer.php

View workflow job for this annotation

GitHub Actions / phpstan6

Parameter #1 $value of class PhpParser\Node\Arg constructor expects PhpParser\Node\Expr, PhpParser\Node given.
),
new Node\Arg(
$this->logger ?? new Node\Expr\New_(new Node\Name\FullyQualified(\Psr\Log\NullLogger::class))
),
],
);
}
Expand Down
Loading