当前位置: 首页>>代码示例>>PHP>>正文


PHP Parser::parse方法代码示例

本文整理汇总了PHP中PhpParser\Parser::parse方法的典型用法代码示例。如果您正苦于以下问题:PHP Parser::parse方法的具体用法?PHP Parser::parse怎么用?PHP Parser::parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PhpParser\Parser的用法示例。


在下文中一共展示了Parser::parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: check

 /**
  * @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);
 }
开发者ID:jacmoe,项目名称:php-workshop,代码行数:37,代码来源:FunctionRequirementsCheck.php

示例2: applyPatch

 /**
  * @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);
 }
开发者ID:jacmoe,项目名称:php-workshop,代码行数:31,代码来源:CodePatcher.php

示例3: build

 /**
  * @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;
 }
开发者ID:thomasruiz,项目名称:file-modifier,代码行数:12,代码来源:FileFactory.php

示例4: provideGetStatus

 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])]);
     }
 }
开发者ID:joshdifabio,项目名称:semantic-diff,代码行数:7,代码来源:FactoryTest.php

示例5: validate

 public function validate($code)
 {
     $traverser = new NodeTraverser();
     $traverser->addVisitor($this->visitor);
     $code = "<?php\n" . $code;
     $ast = $this->parser->parse($code);
     $traverser->traverse($ast);
 }
开发者ID:paslandau,项目名称:php-sandbox,代码行数:8,代码来源:Sandbox.php

示例6: parse

 /**
  * @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;
 }
开发者ID:angrybender,项目名称:phpPM,代码行数:14,代码来源:ParserLine.php

示例7: __invoke

 /**
  * @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));
 }
开发者ID:AydinHassan,项目名称:BetterReflection,代码行数:14,代码来源:SourceStubber.php

示例8: analyze

 /**
  * 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()));
 }
开发者ID:AltThree,项目名称:TestBench,代码行数:16,代码来源:ReferenceAnalyzer.php

示例9: parseFile

 /**
  * @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();
 }
开发者ID:christianblos,项目名称:codedocs,代码行数:15,代码来源:CodeParser.php

示例10: analyseFile

 /**
  * @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;
 }
开发者ID:Kevin-ZK,项目名称:vaneDisk,代码行数:14,代码来源:codechecker.php

示例11: analyze

 /**
  *
  * @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;
 }
开发者ID:kabirbaidhya,项目名称:Inspector,代码行数:16,代码来源:Analyzer.php

示例12: assertTokenizerOutput

 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));
 }
开发者ID:e1himself,项目名称:php-x-components,代码行数:10,代码来源:PhpInlineHtmlTokenizerTest.php

示例13: findReflectionsOfType

 /**
  * 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);
     }
 }
开发者ID:roave,项目名称:better-reflection,代码行数:19,代码来源:Locator.php

示例14: printArray

 /**
  * 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);
 }
开发者ID:tebru,项目名称:retrofit-php,代码行数:17,代码来源:ArrayPrinter.php

示例15: findRecursionInFile

 /**
  * @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());
 }
开发者ID:jeremeamia,项目名称:recursed,代码行数:15,代码来源:RecursiveCallFinder.php


注:本文中的PhpParser\Parser::parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。