Skip to content

Commit 2af099d

Browse files
committed
Preserve order of insertions and transformers
1 parent 2075d9e commit 2af099d

File tree

3 files changed

+42
-39
lines changed

3 files changed

+42
-39
lines changed

src/CodePatcher.php

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,46 @@ public function patch(ExerciseInterface $exercise, $code)
7171
private function applyPatch(Patch $patch, $code)
7272
{
7373
$statements = $this->parser->parse($code);
74-
foreach ($patch->getInsertions() as $insertion) {
75-
try {
76-
$codeToInsert = $insertion->getCode();
77-
$codeToInsert = sprintf('<?php %s', preg_replace('/^\s*<\?php/', '', $codeToInsert));
78-
$additionalStatements = $this->parser->parse($codeToInsert);
79-
} catch (Error $e) {
80-
//we should probably log this and have a dev mode or something
74+
foreach ($patch->getModifiers() as $modifier) {
75+
if ($modifier instanceof CodeInsertion) {
76+
$statements = $this->applyCodeInsertion($modifier, $statements);
8177
continue;
8278
}
8379

84-
switch ($insertion->getType()) {
85-
case CodeInsertion::TYPE_BEFORE:
86-
array_unshift($statements, ...$additionalStatements);
87-
break;
88-
case CodeInsertion::TYPE_AFTER:
89-
array_push($statements, ...$additionalStatements);
90-
break;
80+
if (is_callable($modifier)) {
81+
$statements = $modifier($statements);
82+
continue;
9183
}
9284
}
9385

94-
foreach ($patch->getTransformers() as $transformer) {
95-
$statements = $transformer($statements);
86+
return $this->printer->prettyPrintFile($statements);
87+
}
88+
89+
/**
90+
* @param CodeInsertion $codeInsertion
91+
* @param array $statements
92+
* @return array
93+
*/
94+
private function applyCodeInsertion(CodeInsertion $codeInsertion, array $statements)
95+
{
96+
try {
97+
$codeToInsert = $codeInsertion->getCode();
98+
$codeToInsert = sprintf('<?php %s', preg_replace('/^\s*<\?php/', '', $codeToInsert));
99+
$additionalStatements = $this->parser->parse($codeToInsert);
100+
} catch (Error $e) {
101+
//we should probably log this and have a dev mode or something
102+
return $statements;
103+
}
104+
105+
switch ($codeInsertion->getType()) {
106+
case CodeInsertion::TYPE_BEFORE:
107+
array_unshift($statements, ...$additionalStatements);
108+
break;
109+
case CodeInsertion::TYPE_AFTER:
110+
array_push($statements, ...$additionalStatements);
111+
break;
96112
}
97113

98-
return $this->printer->prettyPrintFile($statements);
114+
return $statements;
99115
}
100116
}

src/Patch.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,10 @@
1111
*/
1212
class Patch
1313
{
14-
/**
15-
* @var CodeInsertion[]
16-
*/
17-
private $insertions = [];
18-
1914
/**
2015
* @var array
2116
*/
22-
private $transformers = [];
17+
private $modifications = [];
2318

2419
/**
2520
* @param CodeInsertion $insertion
@@ -28,7 +23,7 @@ class Patch
2823
public function withInsertion(CodeInsertion $insertion)
2924
{
3025
$new = clone $this;
31-
$new->insertions[] = $insertion;
26+
$new->modifications[] = $insertion;
3227
return $new;
3328
}
3429

@@ -39,23 +34,15 @@ public function withInsertion(CodeInsertion $insertion)
3934
public function withTransformer(Closure $closure)
4035
{
4136
$new = clone $this;
42-
$new->transformers[] = $closure;
37+
$new->modifications[] = $closure;
4338
return $new;
4439
}
4540

4641
/**
4742
* @return array
4843
*/
49-
public function getInsertions()
50-
{
51-
return $this->insertions;
52-
}
53-
54-
/**
55-
* @return array
56-
*/
57-
public function getTransformers()
44+
public function getModifiers()
5845
{
59-
return $this->transformers;
46+
return $this->modifications;
6047
}
6148
}

test/PatchTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public function testWithInsertion()
2020
$new = $patch->withInsertion($insertion);
2121

2222
$this->assertNotSame($patch, $new);
23-
$this->assertEmpty($patch->getInsertions());
24-
$this->assertEquals([$insertion], $new->getInsertions());
23+
$this->assertEmpty($patch->getModifiers());
24+
$this->assertEquals([$insertion], $new->getModifiers());
2525
}
2626

2727
public function testWithTransformer()
@@ -32,7 +32,7 @@ public function testWithTransformer()
3232
};
3333
$new = $patch->withTransformer($transformer);
3434
$this->assertNotSame($patch, $new);
35-
$this->assertEmpty($patch->getTransformers());
36-
$this->assertEquals([$transformer], $new->getTransformers());
35+
$this->assertEmpty($patch->getModifiers());
36+
$this->assertEquals([$transformer], $new->getModifiers());
3737
}
3838
}

0 commit comments

Comments
 (0)