|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpSchool\PHP8Appreciate\Exercise; |
| 6 | + |
| 7 | +use GuzzleHttp\Psr7\Request; |
| 8 | +use PhpParser\Node\Name; |
| 9 | +use PhpParser\Node\Stmt; |
| 10 | +use PhpParser\Node\Stmt\Class_; |
| 11 | +use PhpParser\Node\Stmt\TryCatch; |
| 12 | +use PhpParser\NodeFinder; |
| 13 | +use PhpParser\Parser; |
| 14 | +use PhpSchool\PhpWorkshop\Check\FunctionRequirementsCheck; |
| 15 | +use PhpSchool\PhpWorkshop\CodeInsertion; |
| 16 | +use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
| 17 | +use PhpSchool\PhpWorkshop\Exercise\CgiExercise; |
| 18 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
| 19 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
| 20 | +use PhpSchool\PhpWorkshop\Exercise\SubmissionPatchable; |
| 21 | +use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck; |
| 22 | +use PhpSchool\PhpWorkshop\ExerciseCheck\SelfCheck; |
| 23 | +use PhpSchool\PhpWorkshop\ExerciseDispatcher; |
| 24 | +use PhpSchool\PhpWorkshop\Input\Input; |
| 25 | +use PhpSchool\PhpWorkshop\Patch; |
| 26 | +use PhpSchool\PhpWorkshop\Result\Failure; |
| 27 | +use PhpSchool\PhpWorkshop\Result\ResultInterface; |
| 28 | +use PhpSchool\PhpWorkshop\Result\Success; |
| 29 | +use Psr\Http\Message\RequestInterface; |
| 30 | +use Stringable; |
| 31 | + |
| 32 | +class StringifyToDemystify extends AbstractExercise implements |
| 33 | + ExerciseInterface, |
| 34 | + CgiExercise, |
| 35 | + SubmissionPatchable, |
| 36 | + FunctionRequirementsExerciseCheck, |
| 37 | + SelfCheck |
| 38 | +{ |
| 39 | + public function __construct(private Parser $parser) |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | + public function getName(): string |
| 44 | + { |
| 45 | + return 'Stringify to Demystify'; |
| 46 | + } |
| 47 | + |
| 48 | + public function getDescription(): string |
| 49 | + { |
| 50 | + return 'PHP 8\'s Stringable Interface'; |
| 51 | + } |
| 52 | + |
| 53 | + public function getType(): ExerciseType |
| 54 | + { |
| 55 | + return ExerciseType::CGI(); |
| 56 | + } |
| 57 | + |
| 58 | + public function configure(ExerciseDispatcher $dispatcher): void |
| 59 | + { |
| 60 | + $dispatcher->requireCheck(FunctionRequirementsCheck::class); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return array<RequestInterface> |
| 65 | + */ |
| 66 | + public function getRequests(): array |
| 67 | + { |
| 68 | + return array_map( |
| 69 | + fn () => new Request( |
| 70 | + 'POST', |
| 71 | + 'https://phpschool.io/api', |
| 72 | + [], |
| 73 | + json_encode($this->getRandRequest(), JSON_THROW_ON_ERROR) |
| 74 | + ), |
| 75 | + array_fill(0, random_int(3, 6), null) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @return array{'success': bool, 'status': int, 'error'?: string, 'body'?: string} |
| 81 | + * @throws \Exception |
| 82 | + */ |
| 83 | + public function getRandRequest(): array |
| 84 | + { |
| 85 | + return [ |
| 86 | + [ |
| 87 | + 'success' => true, |
| 88 | + 'status' => 200, |
| 89 | + 'body' => ['{fake: "data"}', '{stub: "payload"}'][random_int(0, 1)] |
| 90 | + ], |
| 91 | + [ |
| 92 | + 'success' => false, |
| 93 | + 'status' => 400, |
| 94 | + 'error' => ['Bad Request: Incorrect mime type', 'Bad Request: Invalid key "id"'][random_int(0, 1)] |
| 95 | + ], |
| 96 | + [ |
| 97 | + 'success' => false, |
| 98 | + 'status' => 401, |
| 99 | + 'error' => ['Unauthorized: Not authorised for this resource', 'Bad API token'][random_int(0, 1)] |
| 100 | + ], |
| 101 | + [ |
| 102 | + 'success' => false, |
| 103 | + 'status' => 403, |
| 104 | + 'error' => ['Forbidden: Access forbidden', 'Forbidden: ACL not granted for resource'][random_int(0, 1)] |
| 105 | + ], |
| 106 | + [ |
| 107 | + 'success' => false, |
| 108 | + 'status' => 500, |
| 109 | + 'error' => ['Server Error: Contact webmaster', 'Server Error: err_code: 3289327'][random_int(0, 1)] |
| 110 | + ], |
| 111 | + ][random_int(0, 4)]; |
| 112 | + } |
| 113 | + |
| 114 | + public function getPatch(): Patch |
| 115 | + { |
| 116 | + $code = <<<CODE |
| 117 | + function log_failure(\Stringable \$error) { |
| 118 | + echo \$error; |
| 119 | + } |
| 120 | + CODE; |
| 121 | + |
| 122 | + return (new Patch())->withInsertion(new CodeInsertion(CodeInsertion::TYPE_BEFORE, $code)); |
| 123 | + } |
| 124 | + |
| 125 | + public function getRequiredFunctions(): array |
| 126 | + { |
| 127 | + return ['log_failure']; |
| 128 | + } |
| 129 | + |
| 130 | + public function getBannedFunctions(): array |
| 131 | + { |
| 132 | + return []; |
| 133 | + } |
| 134 | + |
| 135 | + public function check(Input $input): ResultInterface |
| 136 | + { |
| 137 | + /** @var Stmt[] $statements */ |
| 138 | + $statements = $this->parser->parse((string) file_get_contents($input->getRequiredArgument('program'))); |
| 139 | + |
| 140 | + /** @var Class_|null $classStmt */ |
| 141 | + $classStmt = (new NodeFinder())->findFirstInstanceOf($statements, Class_::class); |
| 142 | + |
| 143 | + if (!$classStmt) { |
| 144 | + return new Failure($this->getName(), 'No class statement could be found'); |
| 145 | + } |
| 146 | + |
| 147 | + $implements = array_filter($classStmt->implements, fn (Name $name) => $name->toString() === Stringable::class); |
| 148 | + |
| 149 | + if (empty($implements)) { |
| 150 | + return new Failure($this->getName(), 'Your class should implement the Stringable interface'); |
| 151 | + } |
| 152 | + |
| 153 | + return new Success($this->getName()); |
| 154 | + } |
| 155 | +} |
0 commit comments