/**
  * @param ExerciseInterface $exercise
  * @param string $fileName
  * @return ResultInterface
  */
 public function check(ExerciseInterface $exercise, $fileName)
 {
     if (!$exercise instanceof FunctionRequirementsExerciseCheck) {
         throw new \InvalidArgumentException();
     }
     $requiredFunctions = $exercise->getRequiredFunctions();
     $bannedFunctions = $exercise->getBannedFunctions();
     $code = file_get_contents($fileName);
     try {
         $ast = $this->parser->parse($code);
     } catch (Error $e) {
         return Failure::fromCheckAndCodeParseFailure($this, $e, $fileName);
     }
     $visitor = new FunctionVisitor($requiredFunctions, $bannedFunctions);
     $traverser = new NodeTraverser();
     $traverser->addVisitor($visitor);
     $traverser->traverse($ast);
     $bannedFunctions = [];
     if ($visitor->hasUsedBannedFunctions()) {
         $bannedFunctions = array_map(function (FuncCall $node) {
             return ['function' => $node->name->__toString(), 'line' => $node->getLine()];
         }, $visitor->getBannedUsages());
     }
     $missingFunctions = [];
     if (!$visitor->hasMetFunctionRequirements()) {
         $missingFunctions = $visitor->getMissingRequirements();
     }
     if (!empty($bannedFunctions) || !empty($missingFunctions)) {
         return new FunctionRequirementsFailure($this, $bannedFunctions, $missingFunctions);
     }
     return Success::fromCheck($this);
 }
Example #2
0
 public function testFailureFromCodeParseException()
 {
     $e = new Error('Something went wrong yo');
     $failure = Failure::fromCheckAndCodeParseFailure($this->check, $e, 'exercise.php');
     $this->assertInstanceOf(ResultInterface::class, $failure);
     $this->assertEquals('File: "exercise.php" could not be parsed. Error: "Something went wrong yo on unknown line"', $failure->getReason());
     $this->assertEquals('Some Check', $failure->getCheckName());
 }
 /**
  * This check grabs the contents of the student's solution and
  * attempts to parse it with `nikic/php-parser`. If any exceptions are thrown
  * by the parser, it is treated as a failure.
  *
  * @param ExerciseInterface $exercise The exercise to check against.
  * @param Input $input The command line arguments passed to the command.
  * @return ResultInterface The result of the check.
  */
 public function check(ExerciseInterface $exercise, Input $input)
 {
     $code = file_get_contents($input->getArgument('program'));
     try {
         $this->parser->parse($code);
     } catch (Error $e) {
         return Failure::fromCheckAndCodeParseFailure($this, $e, $input->getArgument('program'));
     }
     return Success::fromCheck($this);
 }
Example #4
0
 /**
  * @param ExerciseInterface $exercise
  * @param string $fileName
  * @return ResultInterface
  */
 public function check(ExerciseInterface $exercise, $fileName)
 {
     $code = file_get_contents($fileName);
     try {
         $this->parser->parse($code);
     } catch (Error $e) {
         return Failure::fromCheckAndCodeParseFailure($this, $e, $fileName);
     }
     return Success::fromCheck($this);
 }