class

Noir::PhpBlockParser

Inherits Reference < Object

The block structure of PHP source, read off a PhpLexer's masked text (strings, comments, heredocs and inline HTML blanked), so no delimiter or keyword inside a literal or a comment can open, close or fake a block.

One linear pass builds a tree of the constructs analyzers ask about:

  • statements: if / elseif / else chains, try / catch / finally, switch and its cases, loops, bare { } blocks, with each construct's parenthesised head (condition, subject) as a span;
  • code units: named functions, methods, closures (function () use () { }) and arrow functions (fn () => expr), with their parameter lists, and match expressions — also when they sit inside an argument list or another construct's condition;
  • declarations: class / trait / interface / enum (and new class) with their methods and properties as members;
  • PHP 8 attributes #[A(...), B], attached to the declaration they precede.

Every delimiter pair is matched once up front, so the tree, close_of and array_entries all answer without rescanning. Ranges are character offsets into the lexed source (same as PhpLexer#masked); a block's body excludes its braces.

Constants

ALT_STOPS = {Kind::If => {"elseif", "else", "endif"}, Kind::Elseif => {"elseif", "else", "endif"}, Kind::Else => {"endif"}, Kind::Foreach => {"endforeach"}, Kind::For => {"endfor"}, Kind::While => {"endwhile"}, Kind::Switch => {"endswitch"}}

The words that end an alternative-syntax body (if (x): ... endif;).

CLASS_KIND = Set {"class", "trait", "interface", "enum"}
MODIFIERS = Set {"public", "protected", "private", "static", "final", "abstract", "readonly", "var"}

Constructors

new(lexer : PhpLexer)
Source
new(source : String, *, php_mode : Bool = false)
Source

Instance methods

array_entries(open : Int32) : Array(ArrayEntry) | Nil

Elements of the array literal whose [ (or array('s () is at open, or nil when it does not close.

Source
attributes

Every attribute in source order.

Source
close_of(open : Int32) : Int32 | Nil

The delimiter closing the ( / [ / { at open, or nil.

Source
declarations

class / trait / interface / enum declarations outside any function: at the top level, in a namespace { } block, or declared under a condition (if (! class_exists(X::class)) { class X ... }).

Source
each_node

Depth-first, in source order.

Source
lexer
Source
path(pos : Int32) : Array(Node)

The nodes enclosing pos, outermost first (the root excluded).

Source
root
Source

Nested types