Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ParserAbstract.php

function ParserAbstract::getNamespacingStyle

Determine namespacing style (semicolon or brace)

Parameters

Node[] $stmts Top-level statements.:

Return value

null|string One of "semicolon", "brace" or null (no namespaces)

1 call to ParserAbstract::getNamespacingStyle()
ParserAbstract::handleNamespaces in vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php
Moves statements of semicolon-style namespaces into $ns->stmts and checks various error conditions.

File

vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php, line 658

Class

ParserAbstract

Namespace

PhpParser

Code

private function getNamespacingStyle(array $stmts) : ?string {
    $style = null;
    $hasNotAllowedStmts = false;
    foreach ($stmts as $i => $stmt) {
        if ($stmt instanceof Node\Stmt\Namespace_) {
            $currentStyle = null === $stmt->stmts ? 'semicolon' : 'brace';
            if (null === $style) {
                $style = $currentStyle;
                if ($hasNotAllowedStmts) {
                    $this->emitError(new Error('Namespace declaration statement has to be the very first statement in the script', $this->getNamespaceErrorAttributes($stmt)));
                }
            }
            elseif ($style !== $currentStyle) {
                $this->emitError(new Error('Cannot mix bracketed namespace declarations with unbracketed namespace declarations', $this->getNamespaceErrorAttributes($stmt)));
                // Treat like semicolon style for namespace normalization
                return 'semicolon';
            }
            continue;
        }
        
        /* declare(), __halt_compiler() and nops can be used before a namespace declaration */
        if ($stmt instanceof Node\Stmt\Declare_ || $stmt instanceof Node\Stmt\HaltCompiler || $stmt instanceof Node\Stmt\Nop) {
            continue;
        }
        
        /* There may be a hashbang line at the very start of the file */
        if ($i === 0 && $stmt instanceof Node\Stmt\InlineHTML && preg_match('/\\A#!.*\\r?\\n\\z/', $stmt->value)) {
            continue;
        }
        
        /* Everything else if forbidden before namespace declarations */
        $hasNotAllowedStmts = true;
    }
    return $style;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal