Skip to content

Check code for errors and warnings. #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
4 changes: 2 additions & 2 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@
}),
CodePatcher::class => factory(function (ContainerInterface $c) {
$patch = (new Patch)
->withInsertion(new Insertion(Insertion::TYPE_BEFORE, 'ini_set("display_errors", 1);'))
->withInsertion(new Insertion(Insertion::TYPE_BEFORE, 'ini_set("display_errors", 0);'))
->withInsertion(new Insertion(Insertion::TYPE_BEFORE, 'error_reporting(E_ALL);'))
->withInsertion(new Insertion(Insertion ::TYPE_BEFORE, 'date_default_timezone_set("Europe/London");'));
->withInsertion(new Insertion(Insertion::TYPE_BEFORE, 'date_default_timezone_set("Europe/London");'));

return new CodePatcher($c->get(Parser::class), new Standard, $patch);
}),
Expand Down
77 changes: 40 additions & 37 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 39 additions & 3 deletions src/Exception/CodeExecutionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,50 @@
*/
class CodeExecutionException extends RuntimeException
{

/**
* @var string
*/
private $actual;
/**
* @var string
*/
private $errors;

/**
* CodeExecutionException constructor.
* @param string $reason
* @param string $actual
* @param string $errors
*/
public function __construct($reason, $actual = null, $errors = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename $actual to $output ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Params/property renamed. Methods unchanged, to match other similar methods.

{
$this->message = $reason;
$this->actual = $actual;
$this->errors = $errors;
}

/**
* @param Process $process
* @return static
*/
public static function fromProcess(Process $process)
{
$message = 'PHP Code failed to execute. Error: "%s"';
$processOutput = $process->getErrorOutput() ? $process->getErrorOutput() : $process->getOutput();
return new static(sprintf($message, $processOutput));
$message = "PHP Code failed to execute. Error: \n%s";
$processOutput = $process->getOutput();
$processErrorOutput = $process->getErrorOutput();
return new static(
sprintf($message, $processErrorOutput ?: $processOutput), $processOutput, $processErrorOutput
);
}

public function getActual()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add return types on these two methods

{
return $this->actual;
}

public function getErrors()
{
return $this->errors;
}
}
19 changes: 15 additions & 4 deletions src/ExerciseRunner/CgiRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private function checkRequest(RequestInterface $request, $fileName)

try {
$event = $this->eventDispatcher->dispatch(new CgiExecuteEvent('cgi.verify.user-execute.pre', $request));
$userResponse = $this->executePhpFile($fileName, $event->getRequest(), 'user');
list($userResponse, $userWarnings) = $this->executePhpFile($fileName, $event->getRequest(), 'user');
} catch (CodeExecutionException $e) {
$this->eventDispatcher->dispatch(new Event('cgi.verify.user-execute.fail', ['exception' => $e]));
return Failure::fromNameAndCodeExecutionFailure($this->getName(), $e);
Expand All @@ -109,8 +109,15 @@ private function checkRequest(RequestInterface $request, $fileName)
$solutionHeaders = $this->getHeaders($solutionResponse);
$userHeaders = $this->getHeaders($userResponse);

if ($solutionBody !== $userBody || $solutionHeaders !== $userHeaders) {
return new CgiOutRequestFailure($request, $solutionBody, $userBody, $solutionHeaders, $userHeaders);
if ($solutionBody !== $userBody || $solutionHeaders !== $userHeaders || $userWarnings) {
return new CgiOutRequestFailure(
$request,
$solutionBody,
$userBody,
$solutionHeaders,
$userHeaders,
$userWarnings
);
}

return new Success($this->getName());
Expand Down Expand Up @@ -153,7 +160,11 @@ private function executePhpFile($fileName, RequestInterface $request, $type)
$output = "HTTP/1.0 200 OK\r\n" . $output;
}

return ResponseSerializer::fromString($output);
return (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not keen on the mix of return types here, array vs string is there a way to make it return just one type, always array maybe with an empty string or something?

$type == 'user' ?
[ResponseSerializer::fromString($output), $process->getErrorOutput()]
: ResponseSerializer::fromString($output)
);
}

/**
Expand Down
Loading