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).
 /**
  * @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
 /**
  * @param Patch $patch
  * @param string $code
  * @return string
  */
 private function applyPatch(Patch $patch, $code)
 {
     $statements = $this->parser->parse($code);
     foreach ($patch->getInsertions() as $insertion) {
         try {
             $codeToInsert = $insertion->getCode();
             $codeToInsert = sprintf('<?php %s', preg_replace('/^\\s*<\\?php/', '', $codeToInsert));
             $additionalStatements = $this->parser->parse($codeToInsert);
         } catch (Error $e) {
             //we should probably log this and have a dev mode or something
             continue;
         }
         switch ($insertion->getType()) {
             case CodeInsertion::TYPE_BEFORE:
                 array_unshift($statements, ...$additionalStatements);
                 break;
             case CodeInsertion::TYPE_AFTER:
                 array_push($statements, ...$additionalStatements);
                 break;
         }
     }
     foreach ($patch->getTransformers() as $transformer) {
         $statements = $transformer($statements);
     }
     return $this->printer->prettyPrintFile($statements);
 }
Example #3
0
 /**
  * @param string $contents
  *
  * @return File
  */
 public function build($contents)
 {
     $file = new File($this->codeFactory->buildNamespace());
     $nodes = $this->PHPParser->parse($contents);
     $this->parser->parse($nodes, $file);
     return $file;
 }
 public function provideGetStatus()
 {
     $parser = new Parser(new Lexer());
     foreach ($this->getTestCases() as $testId => $testCase) {
         (yield $testId => [$testCase[0], $parser->parse($testCase[1]), $parser->parse($testCase[2])]);
     }
 }
Example #5
0
 public function validate($code)
 {
     $traverser = new NodeTraverser();
     $traverser->addVisitor($this->visitor);
     $code = "<?php\n" . $code;
     $ast = $this->parser->parse($code);
     $traverser->traverse($ast);
 }
Example #6
0
 /**
  * @param string $code
  * @param int $lineNumber
  * @return Node
  */
 public function parse($code, $lineNumber)
 {
     $stmts = $this->parser->parse($code);
     $this->nodeVisitor->setLine($lineNumber);
     $this->nodeTraverser->addVisitor($this->nodeVisitor);
     $this->nodeTraverser->traverse($stmts);
     $node = $this->nodeVisitor->getNode();
     return $node;
 }
 /**
  * @param ClassReflection $reflection
  *
  * @return string
  */
 public function __invoke(ClassReflection $reflection)
 {
     $stubCode = ClassGenerator::fromReflection($reflection)->generate();
     $isInterface = $reflection->isInterface();
     if (!($isInterface || $reflection->isTrait())) {
         return $stubCode;
     }
     return $this->prettyPrinter->prettyPrint($this->replaceNodesRecursively($this->parser->parse('<?php ' . $stubCode), $isInterface));
 }
Example #8
0
 /**
  * Get the fullyqualified imports and typehints.
  *
  * @param string $path
  *
  * @return string[]
  */
 public function analyze($path)
 {
     $traverser = new NodeTraverser();
     $traverser->addVisitor(new NameResolver());
     $traverser->addVisitor($imports = new ImportVisitor());
     $traverser->addVisitor($names = new NameVisitor());
     $traverser->traverse($this->parser->parse(file_get_contents($path)));
     return array_unique(array_merge($imports->getImports(), $names->getNames()));
 }
Example #9
0
 /**
  * @param string $file
  *
  * @return RefClass[]
  * @throws RuntimeException
  */
 protected function parseFile($file)
 {
     $stmts = $this->phpParser->parse(file_get_contents($file));
     $visitor = new RefVisitor($file);
     $this->traverser->addVisitor($visitor);
     $this->traverser->traverse($stmts);
     $this->traverser->removeVisitor($visitor);
     return $visitor->getClasses();
 }
Example #10
0
 /**
  * @param string $file
  * @return array
  */
 public function analyseFile($file)
 {
     $code = file_get_contents($file);
     $statements = $this->parser->parse($code);
     $visitor = new CodeCheckVisitor($this->blackListedClassNames);
     $traverser = new NodeTraverser();
     $traverser->addVisitor($visitor);
     $traverser->traverse($statements);
     return $visitor->errors;
 }
Example #11
0
 /**
  *
  * @param SourceIterator $source
  * @param array $options
  * @return array
  */
 public function analyze(SourceIterator $source, array $options)
 {
     $result = [];
     foreach ($source as $filename => $code) {
         $ast = $this->parser->parse($code);
         $issues = $this->getIssues($ast, ['filename' => $filename]);
         $result[$filename] = new AnalyzedFile($filename, $issues);
     }
     return $result;
 }
 private function assertTokenizerOutput($source_file, $tokens_file)
 {
     $source = $this->loadFixture($source_file);
     $expected_tokens = json_decode($this->loadFixture($tokens_file), true);
     $nodes = $this->phpParser->parse($source);
     $tokenizer = new TreeEmittingTokenizer(new PhpNodesInputStream($nodes), $tree = new DecodingTreeBuilder());
     $tokenizer->parse();
     $actual_tokens = $tree->save();
     $this->assertEquals($expected_tokens, $this->normalize($actual_tokens));
 }
Example #13
0
 /**
  * Get an array of reflections found in some code.
  *
  * @param Reflector $reflector
  * @param LocatedSource $locatedSource
  * @param IdentifierType $identifierType
  * @return \BetterReflection\Reflection\Reflection[]
  * @throws Exception\ParseToAstFailure
  */
 public function findReflectionsOfType(Reflector $reflector, LocatedSource $locatedSource, IdentifierType $identifierType)
 {
     try {
         return $this->findReflectionsInTree->__invoke($reflector, $this->parser->parse($locatedSource->getSource()), $identifierType, $locatedSource);
     } catch (\Exception $exception) {
         throw Exception\ParseToAstFailure::fromLocatedSource($locatedSource, $exception);
     } catch (\Throwable $exception) {
         throw Exception\ParseToAstFailure::fromLocatedSource($locatedSource, $exception);
     }
 }
Example #14
0
 /**
  * Take a PHP array and convert it to a string
  *
  * @param array $array
  * @return string
  * @throws RetrofitException
  */
 public function printArray(array $array)
 {
     $string = var_export($array, true);
     $string = preg_replace('/\'\\$(.+)\'/', '$' . '\\1', $string);
     $statements = $this->parser->parse($string);
     if (null === $statements) {
         throw new RetrofitException('There was an error parsing the array');
     }
     return $this->printer->prettyPrintFile($statements);
 }
 /**
  * @param SplFileInfo|string $file
  *
  * @return RecursiveCallIterator
  */
 public function findRecursionInFile($file)
 {
     $file = $file instanceof SplFileInfo ? $file : new SplFileInfo($file);
     $ast = $this->parser->parse(file_get_contents($file->getRealPath()));
     $traverser = new NodeTraverser();
     $visitor = new RecursiveCallFinderNodeVisitor($file);
     $traverser->addVisitor($visitor);
     $traverser->traverse($ast);
     return new RecursiveCallIterator($visitor->getRecursiveCalls());
 }
Example #16
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);
 }
 /**
  * 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);
 }
 private function assertCompilerOutput($source, $expected)
 {
     $nodes = $this->parser->parse($source);
     $compiler = new SourceCompiler(new SimpleComponentCompiler(), $this->componentsProvider);
     $compiled = $compiler->compileTemplate($nodes);
     echo $compiled;
     $compiledNodes = $this->phpParser->parse($compiled);
     $expectedNodes = $this->phpParser->parse($expected);
     $this->assertEquals($this->serializer->serialize($expectedNodes), $this->serializer->serialize($compiledNodes));
 }
 /**
  * Traverse the parser nodes so we can extract the information
  *
  * @param $content
  * @param array $nodeVisitors
  * @internal param bool $addInterface
  */
 protected function traverse($content, array $nodeVisitors = [])
 {
     $nodes = $this->phpParser->parse($content);
     $traverser = new NodeTraverser();
     $traverser->addVisitor(new NameResolver());
     foreach ($nodeVisitors as $nodeVisitor) {
         $traverser->addVisitor($nodeVisitor);
     }
     $traverser->traverse($nodes);
 }
 /**
  * @param $toBeAdded
  * @param $configPath
  * @return string
  */
 public function parse($toBeAdded, $initalCode)
 {
     $nodes = $this->parser->parse($initalCode);
     $traverser = new NodeTraverser();
     $addServiceProviderVisitor = new ServiceProviderVisitor();
     $addServiceProviderVisitor->setServiceProvider($toBeAdded);
     $traverser->addVisitor($addServiceProviderVisitor);
     $parsedNodes = $traverser->traverse($nodes);
     $prettyPrinter = new LaravelPrettyPrinter(['shortArraySyntax' => true]);
     return $prettyPrinter->prettyPrint($parsedNodes);
 }
Example #21
0
 /**
  * @param ContextInterface $context
  *
  * @return FileContext
  */
 public function checkContext(ContextInterface $context)
 {
     try {
         $parsedStatements = $this->parser->parse($context->getCheckedCode());
         $this->traverser->traverse($parsedStatements, $context, $this->lexer->getTokens());
     } catch (\Exception $e) {
         $context->addError(new CheckError($e->getMessage()));
     } catch (\ParseException $e) {
         $context->addError(new CheckError($e->getMessage(), $e->getLine()));
     }
 }
Example #22
0
 /**
  * @param string $file
  */
 public function scan($file)
 {
     // Set the current file used by the registry so that we can tell where the change was scanned.
     $this->registry->setCurrentFile($file);
     $code = file_get_contents($file);
     try {
         $statements = $this->parser->parse($code);
         $this->traverser->traverse($statements);
     } catch (Error $e) {
         throw new RuntimeException('Parse Error: ' . $e->getMessage() . ' in ' . $file);
     }
 }
Example #23
0
 /**
  * @param string $code
  * @return string
  */
 public function highlight($code)
 {
     if (!is_string($code)) {
         throw new \InvalidArgumentException('Argument 1 should be a string of valid PHP code');
     }
     try {
         $statements = $this->parser->parse($code);
     } catch (Error $e) {
         throw new \InvalidArgumentException('PHP could not be parsed');
     }
     return $this->printer->prettyPrintFile($statements);
 }
 /**
  * @param string $path
  */
 public function addFile($path)
 {
     $contents = file_get_contents($path);
     // Remove the hash-bang line if there, since
     // PhpParser doesn't support it
     if (substr($contents, 0, 2) === '#!') {
         $contents = substr($contents, strpos($contents, "\n") + 1);
     }
     $nodes = $this->parser->parse($contents);
     foreach ($nodes as $node) {
         $this->processNode($path, $node);
     }
 }
Example #25
0
 /**
  * @param $code
  * @param $sourcePath
  * @return Model
  */
 public function parse($code, $sourcePath)
 {
     try {
         $stmts = $this->parser->parse($code);
         $stmts = $this->traverser->traverse($stmts);
         if ($this->namespaceParser->match($stmts)) {
             $this->namespaceParser->parse($stmts, $sourcePath);
         }
     } catch (PhpParserError $e) {
         throw new ParseException('Parse Error: ' . $e->getMessage());
     }
     return $this->model;
 }
 /**
  * @param string $filePath
  *
  * @throws TombstoneExtractionException
  */
 public function extractTombstones($filePath)
 {
     $this->visitor->setCurrentFile($filePath);
     if (!is_readable($filePath)) {
         throw new TombstoneExtractionException('File "' . $filePath . '" is not readable.');
     }
     try {
         $code = file_get_contents($filePath);
         $stmts = $this->parser->parse($code);
         $this->traverser->traverse($stmts);
     } catch (Error $e) {
         throw new TombstoneExtractionException('PHP code in "' . $filePath . '" could not be parsed.', null, $e);
     }
 }
Example #27
0
 /**
  * Возвращает коллекцию методов
  *
  * @param SplFileInfo[] $files Список файлов с исходными кодами
  *
  * @return Method[]
  */
 public function extract($files)
 {
     $methods = [];
     foreach ($files as $file) {
         $nodes = $this->parser->parse($file->getContents());
         if ($nodes === null) {
             error_log($file . ' does not contains any node');
             continue;
         }
         $this->parse($nodes, $methods);
         $this->progress();
     }
     return $methods;
 }
 private function assertTreeBuilderOutput($source_file, $tokens_file)
 {
     $source = $this->loadFixture($source_file);
     $phpNodes = $this->phpParser->parse($source);
     $tokenizer = new TreeEmittingTokenizer(new PhpNodesInputStream($phpNodes), $tree = new TreeBuilder($this->componentsProvider));
     $nodes = $tokenizer->parse();
     $actual = new \DOMDocument();
     $actual->preserveWhiteSpace = false;
     $actual->loadXML($this->serializer->serialize($nodes));
     $expected = new \DOMDocument();
     $expected->preserveWhiteSpace = false;
     $expected->loadXML($this->loadFixture($tokens_file));
     $this->assertEquals($expected, $actual);
 }
Example #29
0
 /**
  * @param CodeCollectorInterface $codeCollector
  * @param Logger                 $logger
  * @param string                 $filterFileName
  *
  * @return array
  */
 public function convert(CodeCollectorInterface $codeCollector, Logger $logger, $filterFileName = null)
 {
     $zephirCode = array();
     $classes = array();
     $files = $codeCollector->getCode();
     $count = count($files);
     $codes = array();
     $logger->log('Collect class names');
     $progress = $logger->progress($count);
     foreach ($files as $fileName => $fileContent) {
         try {
             $codes[$fileName] = $this->parser->parse($fileContent);
             $classes[$fileName] = $this->classCollector->collect($codes[$fileName], $fileName);
         } catch (\Exception $e) {
             $logger->log(sprintf('<error>Could not convert file' . "\n" . '"%s"' . "\n" . 'cause : %s %s %s</error>' . "\n", $fileName, $e->getMessage(), $e->getFile(), $e->getLine()));
         }
         $progress->advance();
     }
     $progress->finish();
     $logger->log("\nConvert php to zephir");
     $progress = $logger->progress(count($classes));
     foreach ($classes as $phpFile => $class) {
         if ($filterFileName !== null) {
             if (basename($phpFile, '.php') !== $filterFileName) {
                 continue;
             }
         }
         $phpCode = $codes[$phpFile];
         $zephirFile = pathinfo($phpFile, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR . pathinfo($phpFile, PATHINFO_FILENAME) . '.zep';
         $fileName = basename($phpFile, '.php');
         try {
             $converted = $this->convertCode($phpCode, $this->classCollector, $logger, $phpFile, $classes);
             $converted['class'] = $class;
         } catch (\Exception $e) {
             $logger->log(sprintf('Could not convert file "%s" cause : %s %s %s' . "\n", $phpFile, $e->getMessage(), $e->getFile(), $e->getLine()));
             $progress->advance();
             continue;
         }
         $zephirCode[$phpFile] = array_merge($converted, array('phpPath' => substr($phpFile, 0, strrpos($phpFile, '/')), 'fileName' => $fileName, 'fileDestination' => $zephirFile));
         $zephirCode[$phpFile]['fileDestination'] = str_replace('\\', '/', $zephirCode[$phpFile]['fileDestination']);
         foreach ($converted['additionalClass'] as $aditionalClass) {
             $zephirCode[$phpFile . $aditionalClass['name']] = array_merge(array('fileName' => $aditionalClass['name'], 'zephir' => $aditionalClass['code'], 'fileDestination' => str_replace('\\', '/', $converted['namespace']) . '/' . $aditionalClass['name'] . '.zep', 'destination' => str_replace('\\', '/', $converted['namespace']) . '/'));
         }
         $progress->advance();
     }
     $progress->finish();
     $logger->log("\n");
     return $zephirCode;
 }
Example #30
0
 /**
  * @param $content
  * @param $prefix
  *
  * @return string
  */
 public function addNamespacePrefix($content, $prefix)
 {
     $traverser = new NodeTraverser();
     $traverser->addVisitor(new NamespaceScoperNodeVisitor($prefix));
     $traverser->addVisitor(new UseNamespaceScoperNodeVisitor($prefix));
     $traverser->addVisitor(new FullyQualifiedNamespaceUseScoperNodeVisitor($prefix));
     try {
         $statements = $this->parser->parse($content);
     } catch (Error $error) {
         throw new ParsingException($error->getMessage());
     }
     $statements = $traverser->traverse($statements);
     $prettyPrinter = new Standard();
     return $prettyPrinter->prettyPrintFile($statements) . "\n";
 }