|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpSchool\PHP8Appreciate\Exercise; |
| 6 | + |
| 7 | +use PhpParser\Node\Expr\FuncCall; |
| 8 | +use PhpParser\NodeFinder; |
| 9 | +use PhpParser\Parser; |
| 10 | +use PhpSchool\PhpWorkshop\Check\FunctionRequirementsCheck; |
| 11 | +use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
| 12 | +use PhpSchool\PhpWorkshop\Exercise\CliExercise; |
| 13 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
| 14 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
| 15 | +use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait; |
| 16 | +use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck; |
| 17 | +use PhpSchool\PhpWorkshop\ExerciseCheck\SelfCheck; |
| 18 | +use PhpSchool\PhpWorkshop\Input\Input; |
| 19 | +use PhpSchool\PhpWorkshop\Result\Failure; |
| 20 | +use PhpSchool\PhpWorkshop\Result\FailureInterface; |
| 21 | +use PhpSchool\PhpWorkshop\Result\ResultInterface; |
| 22 | +use PhpSchool\PhpWorkshop\Result\Success; |
| 23 | + |
| 24 | +class HaveTheLastSay extends AbstractExercise implements |
| 25 | + ExerciseInterface, |
| 26 | + CliExercise, |
| 27 | + FunctionRequirementsExerciseCheck, |
| 28 | + SelfCheck |
| 29 | +{ |
| 30 | + use TemporaryDirectoryTrait; |
| 31 | + |
| 32 | + public function __construct(private Parser $parser) |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + public function getName(): string |
| 37 | + { |
| 38 | + return 'Have the Last Say'; |
| 39 | + } |
| 40 | + |
| 41 | + public function getDescription(): string |
| 42 | + { |
| 43 | + return 'Use named arguments to specify the last to a specific parameter'; |
| 44 | + } |
| 45 | + |
| 46 | + public function getArgs(): array |
| 47 | + { |
| 48 | + $file = $this->getTemporaryPath(); |
| 49 | + |
| 50 | + $countries = [ |
| 51 | + ['UK', 'London'], |
| 52 | + ['Austria', 'Vienna'], |
| 53 | + ['France', 'Paris'], |
| 54 | + ['Turkey', 'Istanbul'], |
| 55 | + ['Morocco', 'Rabat'], |
| 56 | + ['Georgia', 'Tbilisi'], |
| 57 | + ['Kyrgyzstan', 'Bishkek'], |
| 58 | + ['Serbia', 'Belgrade'], |
| 59 | + ['Uzbekistan', 'Tashkent'], |
| 60 | + ['Belarus', 'Minsk'], |
| 61 | + ]; |
| 62 | + |
| 63 | + file_put_contents( |
| 64 | + $file, |
| 65 | + collect($this->getRandomCountries($countries))->map(fn ($row) => implode("|", $row))->implode("\n") |
| 66 | + ); |
| 67 | + |
| 68 | + return [ |
| 69 | + [$file] |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param array<array{0: string, 1: string}> $countries |
| 75 | + * @return array<array{0: string, 1: string}> $countries |
| 76 | + */ |
| 77 | + private function getRandomCountries(array $countries): array |
| 78 | + { |
| 79 | + return array_intersect_key( |
| 80 | + $countries, |
| 81 | + array_flip(array_rand($countries, random_int(3, 7))) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + public function tearDown(): void |
| 86 | + { |
| 87 | + unlink($this->getTemporaryPath()); |
| 88 | + } |
| 89 | + |
| 90 | + public function getType(): ExerciseType |
| 91 | + { |
| 92 | + return new ExerciseType(ExerciseType::CLI); |
| 93 | + } |
| 94 | + |
| 95 | + public function getRequiredFunctions(): array |
| 96 | + { |
| 97 | + return ['fopen', 'fgetcsv']; |
| 98 | + } |
| 99 | + |
| 100 | + public function getBannedFunctions(): array |
| 101 | + { |
| 102 | + return []; |
| 103 | + } |
| 104 | + |
| 105 | + public function check(Input $input): ResultInterface |
| 106 | + { |
| 107 | + $statements = $this->parser->parse((string) file_get_contents($input->getRequiredArgument('program'))); |
| 108 | + |
| 109 | + if (null === $statements || empty($statements)) { |
| 110 | + return Failure::fromNameAndReason($this->getName(), 'No code was found'); |
| 111 | + } |
| 112 | + |
| 113 | + $check = new FunctionRequirementsCheck($this->parser); |
| 114 | + $result = $check->check($this, $input); |
| 115 | + |
| 116 | + if ($result instanceof FailureInterface) { |
| 117 | + return $result; |
| 118 | + } |
| 119 | + |
| 120 | + $funcCall = (new NodeFinder())->findFirst( |
| 121 | + $statements, |
| 122 | + fn ($node) => $node instanceof FuncCall && $node->name->toString() === 'fgetcsv' |
| 123 | + ); |
| 124 | + |
| 125 | + if ($funcCall->args[0]->name !== null) { |
| 126 | + return Failure::fromNameAndReason( |
| 127 | + $this->getName(), |
| 128 | + 'The stream argument must be specified using a positional parameter' |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + if (count($funcCall->args) > 2) { |
| 133 | + return Failure::fromNameAndReason( |
| 134 | + $this->getName(), |
| 135 | + 'You should only specify the stream and separator arguments, no others' |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + if (!isset($funcCall->args[1])) { |
| 140 | + return Failure::fromNameAndReason($this->getName(), 'The separator argument has not been specified'); |
| 141 | + } |
| 142 | + |
| 143 | + if (!$funcCall->args[1]->name) { |
| 144 | + return Failure::fromNameAndReason( |
| 145 | + $this->getName(), |
| 146 | + 'The second positional argument should not be specified' |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + if ($funcCall->args[1]->name->name !== 'separator') { |
| 151 | + return Failure::fromNameAndReason( |
| 152 | + $this->getName(), |
| 153 | + 'A named argument has been used, but not for the separator argument' |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + return new Success($this->getName()); |
| 158 | + } |
| 159 | +} |
0 commit comments