@@ -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}
0 commit comments