66
77use Kiboko \Component \Bucket \AcceptanceResultBucket ;
88use Kiboko \Component \Bucket \RejectionResultBucket ;
9+ use Kiboko \Component \Bucket \RejectionWithReasonResultBucket ;
10+ use Kiboko \Component \Satellite \Plugin \Filtering \DTO \Exclusion ;
911use Kiboko \Contract \Configurator \StepBuilderInterface ;
1012use Kiboko \Contract \Pipeline \TransformerInterface ;
1113use PhpParser \Builder ;
@@ -17,11 +19,9 @@ final class Reject implements StepBuilderInterface
1719 private ?Node \Expr $ rejection = null ;
1820 private ?Node \Expr $ state = null ;
1921 private ?Node \Expr $ rejection_serializer = null ;
20- /** @var list<?Node\Expr > */
22+ /** @var list<?Exclusion > */
2123 private array $ exclusions = [];
2224
23- public function __construct () {}
24-
2525 public function withLogger (Node \Expr $ logger ): self
2626 {
2727 $ this ->logger = $ logger ;
@@ -50,53 +50,46 @@ public function withRejectionSerializer(Node\Expr $rejection_serializer): self
5050 return $ this ;
5151 }
5252
53- public function withExclusions (Node \ Expr ...$ exclusions ): self
53+ public function withExclusions (Exclusion ...$ exclusions ): self
5454 {
5555 array_push ($ this ->exclusions , ...$ exclusions );
5656
5757 return $ this ;
5858 }
5959
60- private function buildExclusions (Node \ Expr ...$ exclusions ): Node \ Expr
60+ private function buildExclusions (Exclusion ...$ exclusions ): array
6161 {
62- if (\count ($ exclusions ) > 3 ) {
63- $ length = \count ($ exclusions );
64- $ middle = (int ) floor ($ length / 2 );
65- $ left = \array_slice ($ exclusions , 0 , $ middle );
66- $ right = \array_slice ($ exclusions , $ middle , $ length );
67-
68- return new Node \Expr \BinaryOp \BooleanAnd (
69- $ this ->buildExclusions (...$ left ),
70- $ this ->buildExclusions (...$ right ),
71- );
72- }
73-
74- if (\count ($ exclusions ) > 2 ) {
75- $ right = array_shift ($ exclusions );
76-
77- return new Node \Expr \BinaryOp \BooleanAnd (
78- $ this ->buildExclusions (...$ exclusions ),
79- $ right ,
80- );
81- }
82-
83- if (\count ($ exclusions ) > 1 ) {
84- $ left = array_pop ($ exclusions );
85- $ right = array_pop ($ exclusions );
86-
87- return new Node \Expr \BinaryOp \BooleanAnd (
88- $ left ,
89- $ right ,
62+ $ statements = [];
63+ foreach ($ exclusions as $ exclusion ) {
64+ $ statements [] = new Node \Stmt \If_ (
65+ $ exclusion ->when ,
66+ [
67+ 'stmts ' => [
68+ new Node \Stmt \Expression (
69+ new Node \Expr \Assign (
70+ new Node \Expr \Variable ('input ' ),
71+ new Node \Expr \Yield_ (
72+ new Node \Expr \New_ (
73+ $ exclusion ->reason ? new Node \Name \FullyQualified (RejectionWithReasonResultBucket::class) : new Node \Name \FullyQualified (RejectionResultBucket::class),
74+ [
75+ null !== $ this ->rejection_serializer ? new Node \Arg ($ this ->rejection_serializer ) : new Node \Arg (new Node \Expr \Variable ('input ' )),
76+ $ exclusion ->reason ? new Node \Arg ($ exclusion ->reason ) : new Node \Arg (
77+ new Node \Expr \ConstFetch (
78+ new Node \Name (null )
79+ ),
80+ ),
81+ ]
82+ ),
83+ ),
84+ ),
85+ ),
86+ new Node \Stmt \Continue_ (),
87+ ],
88+ ]
9089 );
9190 }
9291
93- if (\count ($ exclusions ) > 0 ) {
94- return array_pop ($ exclusions );
95- }
96-
97- return new Node \Expr \ConstFetch (
98- new Node \Name ('false ' ),
99- );
92+ return $ statements ;
10093 }
10194
10295 public function getNode (): Node
@@ -122,27 +115,7 @@ class: new Node\Stmt\Class_(null, [
122115 new Node \Name ('true ' ),
123116 ),
124117 [
125- new Node \Stmt \If_ (
126- $ this ->buildExclusions (...$ this ->exclusions ),
127- [
128- 'stmts ' => [
129- new Node \Stmt \Expression (
130- new Node \Expr \Assign (
131- new Node \Expr \Variable ('input ' ),
132- new Node \Expr \Yield_ (
133- new Node \Expr \New_ (
134- new Node \Name \FullyQualified (RejectionResultBucket::class),
135- [
136- null !== $ this ->rejection_serializer ? new Node \Arg ($ this ->rejection_serializer ) : new Node \Arg (new Node \Expr \Variable ('input ' )),
137- ]
138- ),
139- ),
140- ),
141- ),
142- new Node \Stmt \Continue_ (),
143- ],
144- ]
145- ),
118+ ...$ this ->buildExclusions (...$ this ->exclusions ),
146119 new Node \Stmt \Expression (
147120 new Node \Expr \Assign (
148121 new Node \Expr \Variable ('input ' ),
0 commit comments