public function check(ExerciseInterface $exercise, Input $input)
    {
        if (!$exercise instanceof FunctionRequirementsExerciseCheck) {
            throw new \InvalidArgumentException;
        }

        $requiredFunctions  = $exercise->getRequiredFunctions();
        $bannedFunctions    = $exercise->getBannedFunctions();

        $code = file_get_contents($input->getArgument('program'));

        try {
            $ast = $this->parser->parse($code);
        } catch (Error $e) {
            return Failure::fromCheckAndCodeParseFailure($this, $e, $input->getArgument('program'));
        }

        $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);
    }
public function check(ExerciseInterface $exercise, Input $input)
    {
        if (!$exercise instanceof FunctionRequirementsExerciseCheck) {
            throw new \InvalidArgumentException;
        }

        $requiredFunctions  = $exercise->getRequiredFunctions();
        $bannedFunctions    = $exercise->getBannedFunctions();

        $code = file_get_contents($input->getArgument('program'));

        try {
            $ast = $this->parser->parse($code);
        } catch (Error $e) {
            return Failure::fromCheckAndCodeParseFailure($this, $e, $input->getArgument('program'));
        }

        $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);
    }
public function check ( ExerciseInterface $ exercise , Input $ input ) { if ( ! $ exercise instanceof FunctionRequirementsExerciseCheck ) { throw new \ InvalidArgumentException ; } $ requiredFunctions = $ exercise -> getRequiredFunctions ( ) ; $ bannedFunctions = $ exercise -> getBannedFunctions ( ) ; $ code = file_get_contents ( $ input -> getArgument ( 'program' ) ) ; try { $ ast = $ this -> parser -> parse ( $ code ) ; } catch ( Error $ e ) { return Failure :: fromCheckAndCodeParseFailure ( $ this , $ e , $ input -> getArgument ( 'program' ) ) ; } $ 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 ) ; }
Parse the students solution and check that there are usages of
required functions and that banned functions are not used. The requirements
are pulled from the exercise.

@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.
Parse the students solution and check that there are usages of required functions and that banned functions are not used . The requirements are pulled from the exercise .