Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 33 additions & 17 deletions src/CodePatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<?php %s', preg_replace('/^\s*<\?php/', '', $codeToInsert));
$additionalStatements = $this->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('<?php %s', preg_replace('/^\s*<\?php/', '', $codeToInsert));
$additionalStatements = $this->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;
}
}
23 changes: 5 additions & 18 deletions src/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@
*/
class Patch
{
/**
* @var CodeInsertion[]
*/
private $insertions = [];

/**
* @var array
*/
private $transformers = [];
private $modifications = [];

/**
* @param CodeInsertion $insertion
Expand All @@ -28,7 +23,7 @@ class Patch
public function withInsertion(CodeInsertion $insertion)
{
$new = clone $this;
$new->insertions[] = $insertion;
$new->modifications[] = $insertion;
return $new;
}

Expand All @@ -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;
}
}
8 changes: 4 additions & 4 deletions test/PatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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());
}
}