-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCodeExistsCheck.php
More file actions
77 lines (63 loc) · 2.29 KB
/
CodeExistsCheck.php
File metadata and controls
77 lines (63 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
namespace PhpSchool\PhpWorkshop\Check;
use PhpParser\Error;
use PhpParser\ErrorHandler;
use PhpParser\Node\Stmt\InlineHTML;
use PhpParser\Parser;
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\ExecutionContext;
use PhpSchool\PhpWorkshop\Input\Input;
use PhpSchool\PhpWorkshop\Result\Failure;
use PhpSchool\PhpWorkshop\Result\ResultInterface;
use PhpSchool\PhpWorkshop\Result\Success;
class CodeExistsCheck implements SimpleCheckInterface
{
public function __construct(private Parser $parser) {}
public function getName(): string
{
return 'Code Exists Check';
}
/**
* @param ExecutionContext $context The current execution context, containing the exercise, input and working directories.
*
* Check solution provided contains code
* Note: We don't care if it's valid code at this point
*/
public function check(ExecutionContext $context): ResultInterface
{
$noopHandler = new class implements ErrorHandler {
public function handleError(Error $error): void {}
};
$code = (string) file_get_contents($context->getEntryPoint());
$statements = $this->parser->parse($code, $noopHandler);
$empty = null === $statements || empty($statements);
if (!$empty) {
$openingTag = is_array($statements) && count($statements) === 1 ? $statements[0] : null;
$empty = $openingTag instanceof InlineHTML && in_array($openingTag->value, ['<?php', '<?']);
}
if ($empty) {
return Failure::fromCheckAndReason($this, 'No code was found');
}
return Success::fromCheck($this);
}
/**
* This check can run on any exercise type.
*/
public function canRun(ExerciseType $exerciseType): bool
{
return in_array($exerciseType->getValue(), [ExerciseType::CGI, ExerciseType::CLI], true);
}
public function getExerciseInterface(): string
{
return ExerciseInterface::class;
}
/**
* This check must run before executing the solution because all solutions require code
*/
public function getPosition(): string
{
return SimpleCheckInterface::CHECK_BEFORE;
}
}