本文整理汇总了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);
}
示例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);
}
示例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;
}
示例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])]);
}
}
示例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);
}
示例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;
}
示例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));
}
示例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()));
}
示例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();
}
示例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;
}
示例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;
}
示例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));
}
示例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);
}
}
示例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);
}
示例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());
}