Skip to content

Commit 13a0325

Browse files
committed
Allow Transformer instances in patches
1 parent 7673ce7 commit 13a0325

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/Patch.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@
55
namespace PhpSchool\PhpWorkshop;
66

77
use Closure;
8+
use PhpSchool\PhpWorkshop\Patch\Transformer;
89

910
/**
1011
* This class is responsible for storing the modifications that should
1112
* be made to a PHP file. That includes insertions and transformers.
1213
* A transformer is a simple closure which should receives an AST
1314
* representation of the student's solution and it should return the modified AST.
15+
*
16+
* Transformers can also be class implementing `Transformer`.
17+
*
1418
* An insertion is a block of code that can be inserted at the top or bottom of
1519
* the students solution.
1620
*/
1721
final class Patch
1822
{
1923
/**
20-
* @var array<CodeInsertion|Closure>
24+
* @var array<CodeInsertion|Closure|Transformer>
2125
*/
2226
private $modifications = [];
2327

@@ -35,22 +39,23 @@ public function withInsertion(CodeInsertion $insertion): self
3539
}
3640

3741
/**
38-
* Add a new transformer (`Closure`). `Patch` is immutable so a new instance is returned.
42+
* Add a new transformer (`Closure` OR `Transformer`). `Patch` is immutable so a new instance is returned.
3943
*
40-
* @param Closure $closure
44+
* @param Closure|Transformer $closure
4145
* @return self
4246
*/
43-
public function withTransformer(Closure $closure): self
47+
public function withTransformer($closure): self
4448
{
4549
$new = clone $this;
4650
$new->modifications[] = $closure;
4751
return $new;
4852
}
4953

5054
/**
51-
* Retrieve all the modifications including insertions (`CodeInsertion`'s) & transformers (`Closure`'s)
55+
* Retrieve all the modifications including insertions (`CodeInsertion`'s)
56+
* & transformers (`Closure`'s OR `Transformer`'s)
5257
*
53-
* @return array<CodeInsertion|Closure>
58+
* @return array<CodeInsertion|Closure|Transformer>
5459
*/
5560
public function getModifiers(): array
5661
{

test/PatchTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testWithInsertion(): void
1919
$this->assertEquals([$insertion], $new->getModifiers());
2020
}
2121

22-
public function testWithTransformer(): void
22+
public function testWithTransformerWithClosure(): void
2323
{
2424
$patch = new Patch();
2525
$transformer = function (array $statements) {
@@ -30,4 +30,42 @@ public function testWithTransformer(): void
3030
$this->assertEmpty($patch->getModifiers());
3131
$this->assertEquals([$transformer], $new->getModifiers());
3232
}
33+
34+
public function testWithTransformerWithTransformer(): void
35+
{
36+
$patch = new Patch();
37+
$transformer = new class implements Patch\Transformer {
38+
public function transform(array $ast): array
39+
{
40+
return $ast;
41+
}
42+
};
43+
44+
$new = $patch->withTransformer($transformer);
45+
$this->assertNotSame($patch, $new);
46+
$this->assertEmpty($patch->getModifiers());
47+
$this->assertEquals([$transformer], $new->getModifiers());
48+
}
49+
50+
public function testWithTransformerMultiple(): void
51+
{
52+
$transformer1 = new class implements Patch\Transformer {
53+
public function transform(array $ast): array
54+
{
55+
return $ast;
56+
}
57+
};
58+
$transformer2 = function (array $statements) {
59+
return $statements;
60+
};
61+
62+
$patch = new Patch();
63+
$new = $patch
64+
->withTransformer($transformer1)
65+
->withTransformer($transformer2);
66+
67+
$this->assertNotSame($patch, $new);
68+
$this->assertEmpty($patch->getModifiers());
69+
$this->assertEquals([$transformer1, $transformer2], $new->getModifiers());
70+
}
3371
}

0 commit comments

Comments
 (0)