PhpParser\Parser::parse PHP Method

parse() public method

Parses PHP code into a node tree.
public parse ( string $code, phpparser\ErrorHandler $errorHandler = null ) : phpparser\Node[] | null
$code string The source code to parse
$errorHandler phpparser\ErrorHandler Error handler to use for lexer/parser errors, defaults to ErrorHandler\Throwing.
return phpparser\Node[] | null Array of statements (or null if the 'throwOnError' option is disabled and the parser was unable to recover from an error).
    public function parse($code, ErrorHandler $errorHandler = null);

Usage Example

 /**
  * @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);
 }
All Usage Examples Of PhpParser\Parser::parse
Parser