GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Home
  • Try
  • Documentation
  • Blog
  • GitHub
  • ← Previous: Abstract Syntax Tree
  • Next: Type System →

This view

  • Original page
  • New address

GeezerVison is showing phpstan.org — Scope

All Documentation
Developing Extensions Developing Extensions
Foundations
  • Extension Types
  • Core Concepts
  • Abstract Syntax Tree
  • Scope
  • Type System
  • Trinary Logic
  • Reflection
  • Dependency Injection & Configuration
Custom Rules
  • Custom Rules
  • Collectors
  • Restricted Usage Extensions
  • Forbidden Class Name Extensions
Type Inference
  • Dynamic Return Type Extensions
  • Dynamic Throw Type Extensions
  • Type-Specifying Extensions
  • Closure Extensions
  • Parameter Out Type Extensions
  • Expression Type Resolver Extensions
  • Operator Type Specifying Extensions
  • Custom PHPDoc Types
Metadata
  • Class Reflection Extensions
  • Custom Deprecations
  • Allowed Subtypes
  • Additional Constructors
  • Exception Type Resolver
  • Stub Files Extensions
Dead Code Detection
  • Always-Read and Written Properties
  • Always-Used Class Constants
  • Always-Used Methods
Output & Infrastructure
  • Error Formatters
  • Ignore Error Extensions
  • Diagnose Extensions
  • Result Cache Meta Extensions
  • Testing
  • Backward Compatibility Promise
  • API Reference
Other Sections
  • User Guide
  • Config Reference
  • Writing PHP Code
Sponsor PHPStan!

Scope

The PHPStan\Analyser\Scope object can be used to get more information about the code, like types of variables, or current file and namespace.

It’s passed as a method parameter to these types of extensions:

  • Custom rules (2nd parameter of processNode method)
  • Dynamic return type extensions (last parameter of getTypeFrom*Call methods)
  • Dynamic throw type extensions (last parameter of getTypeFrom*Call methods)
  • Type-specifying extensions (3rd parameter of specifyTypes method)

The Scope represents the state of the analyser in the position in the AST where the extension is being executed.

public function doFoo(?array $list): void
{
	// Scope knows we're in doFoo() method here
	// And it knows that $list is either an array or null
	if ($list !== null) {
		// Scope knows that $list is an array
		$foo = true;
		// Scope knows that $foo is true
	} else {
		// Scope knows that $list is null
		$foo = false;
		// Scope knows that $foo is false
	}

	// Scope knows that $list is either an array or null
	// Scope knows that $foo is bool
}

The most interesting method is Scope::getType(PhpParser\Node\Expr $expr): PHPStan\Type\Type. It allows us to ask for types of expressions like this:

// $methodCall is PhpParser\Node\Expr\MethodCall
// On which type was the method called?
$calledOnType = $scope->getType($methodCall->var);

The getType() method returns an implementation of PHPStan\Type\Type. You can learn more about types in the article about the type system.

Where are we? #

You can use different Scope methods to ask about the current whereabouts:

  • getFile() - returns the current file path
  • getNamespace() - returns the current namespace
  • isInClass() - tells the caller whether we are in a class
  • getClassReflection() - returns the reflection of the current class. Cannot return null if isInClass() is true.
  • isInTrait() - tells the caller whether we are in a trait
  • getTraitReflection() - returns the reflection of the current trait. Cannot return null if isInTrait() is true.
  • getFunction() - returns FunctionReflection, MethodReflection, or null.
  • isInAnonymousFunction() - tells the caller whether we are in an anonymous function
  • getAnonymousFunctionReflection() - returns anonymous function reflection, or null.

Resolving special names #

In the case of relative names, PhpParser\Node\Name AST node instances are resolved according to the current namespace and use statements, however certain special names like self remain unresolved.

For example we can have PhpParser\Node\Expr\StaticCall that represents static method call like self::doFoo(). If we were to use the value in its $class property as a definitive class name, we’d work with self, but there’s no class self defined in our code. We first need to resolve self to the correct name:

// $staticCall is PhpParser\Node\Expr\StaticCall
$className = $scope->resolveName($staticCall->class);
Theme
X Mastodon YouTube RSS
© 2026 PHPStan s.r.o.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.