From 2af099db2e930cb92251af28bfb3c7f5d5754e33 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Mon, 30 May 2016 18:22:48 +0200 Subject: [PATCH] Preserve order of insertions and transformers --- src/CodePatcher.php | 50 ++++++++++++++++++++++++++++++--------------- src/Patch.php | 23 +++++---------------- test/PatchTest.php | 8 ++++---- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/CodePatcher.php b/src/CodePatcher.php index e395c738..a0240df8 100644 --- a/src/CodePatcher.php +++ b/src/CodePatcher.php @@ -71,30 +71,46 @@ public function patch(ExerciseInterface $exercise, $code) private function applyPatch(Patch $patch, $code) { $statements = $this->parser->parse($code); - foreach ($patch->getInsertions() as $insertion) { - try { - $codeToInsert = $insertion->getCode(); - $codeToInsert = sprintf('parser->parse($codeToInsert); - } catch (Error $e) { - //we should probably log this and have a dev mode or something + foreach ($patch->getModifiers() as $modifier) { + if ($modifier instanceof CodeInsertion) { + $statements = $this->applyCodeInsertion($modifier, $statements); continue; } - switch ($insertion->getType()) { - case CodeInsertion::TYPE_BEFORE: - array_unshift($statements, ...$additionalStatements); - break; - case CodeInsertion::TYPE_AFTER: - array_push($statements, ...$additionalStatements); - break; + if (is_callable($modifier)) { + $statements = $modifier($statements); + continue; } } - foreach ($patch->getTransformers() as $transformer) { - $statements = $transformer($statements); + return $this->printer->prettyPrintFile($statements); + } + + /** + * @param CodeInsertion $codeInsertion + * @param array $statements + * @return array + */ + private function applyCodeInsertion(CodeInsertion $codeInsertion, array $statements) + { + try { + $codeToInsert = $codeInsertion->getCode(); + $codeToInsert = sprintf('parser->parse($codeToInsert); + } catch (Error $e) { + //we should probably log this and have a dev mode or something + return $statements; + } + + switch ($codeInsertion->getType()) { + case CodeInsertion::TYPE_BEFORE: + array_unshift($statements, ...$additionalStatements); + break; + case CodeInsertion::TYPE_AFTER: + array_push($statements, ...$additionalStatements); + break; } - return $this->printer->prettyPrintFile($statements); + return $statements; } } diff --git a/src/Patch.php b/src/Patch.php index d3e63488..9036e3f1 100644 --- a/src/Patch.php +++ b/src/Patch.php @@ -11,15 +11,10 @@ */ class Patch { - /** - * @var CodeInsertion[] - */ - private $insertions = []; - /** * @var array */ - private $transformers = []; + private $modifications = []; /** * @param CodeInsertion $insertion @@ -28,7 +23,7 @@ class Patch public function withInsertion(CodeInsertion $insertion) { $new = clone $this; - $new->insertions[] = $insertion; + $new->modifications[] = $insertion; return $new; } @@ -39,23 +34,15 @@ public function withInsertion(CodeInsertion $insertion) public function withTransformer(Closure $closure) { $new = clone $this; - $new->transformers[] = $closure; + $new->modifications[] = $closure; return $new; } /** * @return array */ - public function getInsertions() - { - return $this->insertions; - } - - /** - * @return array - */ - public function getTransformers() + public function getModifiers() { - return $this->transformers; + return $this->modifications; } } diff --git a/test/PatchTest.php b/test/PatchTest.php index c7c9b58c..1a263f44 100644 --- a/test/PatchTest.php +++ b/test/PatchTest.php @@ -20,8 +20,8 @@ public function testWithInsertion() $new = $patch->withInsertion($insertion); $this->assertNotSame($patch, $new); - $this->assertEmpty($patch->getInsertions()); - $this->assertEquals([$insertion], $new->getInsertions()); + $this->assertEmpty($patch->getModifiers()); + $this->assertEquals([$insertion], $new->getModifiers()); } public function testWithTransformer() @@ -32,7 +32,7 @@ public function testWithTransformer() }; $new = $patch->withTransformer($transformer); $this->assertNotSame($patch, $new); - $this->assertEmpty($patch->getTransformers()); - $this->assertEquals([$transformer], $new->getTransformers()); + $this->assertEmpty($patch->getModifiers()); + $this->assertEquals([$transformer], $new->getModifiers()); } }