Skip to content

Commit bf0ea93

Browse files
authored
Merge pull request #269 from php-school/run-linting
Lint exercises before running
2 parents 6700b26 + 62a556f commit bf0ea93

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpSchool\PhpWorkshop\Exception;
6+
7+
use PhpSchool\PhpWorkshop\Result\FailureInterface;
8+
9+
class CouldNotRunException extends RuntimeException
10+
{
11+
private FailureInterface $failure;
12+
13+
public function __construct(FailureInterface $failure)
14+
{
15+
$this->failure = $failure;
16+
parent::__construct('Could not run exercise');
17+
}
18+
19+
public static function fromFailure(FailureInterface $failure): self
20+
{
21+
return new self($failure);
22+
}
23+
24+
public function getFailure(): FailureInterface
25+
{
26+
return $this->failure;
27+
}
28+
}

src/ExerciseDispatcher.php

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpSchool\PhpWorkshop\Check\CheckRepository;
88
use PhpSchool\PhpWorkshop\Check\ListenableCheckInterface;
9+
use PhpSchool\PhpWorkshop\Check\PhpLintCheck;
910
use PhpSchool\PhpWorkshop\Check\SimpleCheckInterface;
1011
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
1112
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
@@ -16,6 +17,8 @@
1617
use PhpSchool\PhpWorkshop\ExerciseRunner\RunnerManager;
1718
use PhpSchool\PhpWorkshop\Input\Input;
1819
use PhpSchool\PhpWorkshop\Output\OutputInterface;
20+
use PhpSchool\PhpWorkshop\Result\FailureInterface;
21+
use PhpSchool\PhpWorkshop\Exception\CouldNotRunException;
1922

2023
/**
2124
* This class is used to verify/run a student's solution to an exercise. It routes to the correct
@@ -179,6 +182,15 @@ public function verify(ExerciseInterface $exercise, Input $input): ResultAggrega
179182
public function run(ExerciseInterface $exercise, Input $input, OutputInterface $output): bool
180183
{
181184
$exercise->configure($this);
185+
186+
/** @var PhpLintCheck $lint */
187+
$lint = $this->checkRepository->getByClass(PhpLintCheck::class);
188+
$result = $lint->check($exercise, $input);
189+
190+
if ($result instanceof FailureInterface) {
191+
throw CouldNotRunException::fromFailure($result);
192+
}
193+
182194
$this->eventDispatcher->dispatch(new ExerciseRunnerEvent('run.start', $exercise, $input));
183195

184196
try {

test/ExerciseDispatcherTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpSchool\PhpWorkshop\Check\CheckInterface;
66
use PhpSchool\PhpWorkshop\Check\CheckRepository;
77
use PhpSchool\PhpWorkshop\Check\ListenableCheckInterface;
8+
use PhpSchool\PhpWorkshop\Check\PhpLintCheck;
89
use PhpSchool\PhpWorkshop\Check\SimpleCheckInterface;
910
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
1011
use PhpSchool\PhpWorkshop\Event\EventInterface;
@@ -538,7 +539,7 @@ public function testRun(): void
538539
$runnerManager,
539540
new ResultAggregator(),
540541
new EventDispatcher(new ResultAggregator()),
541-
new CheckRepository()
542+
new CheckRepository([new PhpLintCheck()])
542543
);
543544

544545
$this->assertTrue($exerciseDispatcher->run($exercise, $input, $output));

0 commit comments

Comments
 (0)