當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。