PHPSA\Context::syntaxError PHP Méthode

syntaxError() public méthode

Creates a syntax error message.
public syntaxError ( Error $exception, string $filepath ) : boolean
$exception PhpParser\Error
$filepath string
Résultat boolean
    public function syntaxError(\PhpParser\Error $exception, $filepath)
    {
        $code = file($filepath);
        $this->output->writeln('<error>Syntax error:  ' . $exception->getMessage() . " in {$filepath} </error>");
        $this->output->writeln('');
        $issueCollector = $this->application->getIssuesCollector();
        $issueCollector->addIssue(new Issue('syntax-error', 'syntax-error', new IssueLocation($filepath, $exception->getStartLine() - 2)));
        $code = trim($code[$exception->getStartLine() - 2]);
        $this->output->writeln("<comment>\t {$code} </comment>");
        return true;
    }

Usage Example

Exemple #1
0
 /**
  * @param string $filepath
  * @param Context $context
  * @throws RuntimeException when filepath is not readable
  */
 public function parserFile($filepath, Context $context)
 {
     $context->setFilepath($filepath);
     try {
         if (!is_readable($filepath)) {
             throw new RuntimeException('File ' . $filepath . ' is not readable');
         }
         $context->debug('<comment>Precompile: ' . $filepath . '.</comment>');
         $code = file_get_contents($filepath);
         $astTree = $this->parser->parse($code);
         $this->nodeTraverser->traverse($astTree);
         $context->aliasManager = new AliasManager();
         $namespace = null;
         /**
          * Step 1 Precompile
          */
         foreach ($astTree as $topStatement) {
             if ($topStatement instanceof Node\Stmt\Namespace_) {
                 /**
                  * Namespace block can be created without NS name
                  */
                 if ($topStatement->name) {
                     $namespace = $topStatement->name->toString();
                     $context->aliasManager->setNamespace($namespace);
                 }
                 if ($topStatement->stmts) {
                     $this->parseTopDefinitions($topStatement->stmts, $context->aliasManager, $filepath);
                 }
             } else {
                 if (is_array($topStatement)) {
                     $this->parseTopDefinitions($topStatement, $context->aliasManager, $filepath);
                 } else {
                     $this->parseTopDefinitions($astTree, $context->aliasManager, $filepath);
                 }
             }
         }
         $context->clear();
     } catch (\PhpParser\Error $e) {
         $context->syntaxError($e, $filepath);
     } catch (Exception $e) {
         $context->output->writeln("<error>{$e->getMessage()}</error>");
     }
 }