Skip to content

Fix the crash on a global namespace block after a named one - #70

Merged
alies-dev merged 1 commit into
masterfrom
fix/global-namespace-block
Nov 11, 2026
Merged

alies-dev merged 1 commit into
masterfrom
fix/global-namespace-block

Conversation

@alies-dev

Copy link
Copy Markdown
Contributor

Context

A file with a braced global namespace block after a named one crashes the plugin:

<?php
namespace Shop {
    use Shop\Catalog\Product as Item;
}

namespace {
    add_action( 'init', '__return_false' );
}
Uncaught Error: Call to a member function toString() on null in Plugin.php:1362

namespace { ... } has no name, and HookNodeVisitor::enterNode() calls $node->name->toString() on every Namespace_ node. Fixes #58.

Solution

A nameless block now sets the current namespace to the global one ('') and clears the use statements, like any other namespace block.

#59 avoids the crash by skipping nameless blocks, but then the global block keeps the previous block's namespace and imports. Hook docblocks are resolved with them, so in the example above @param Item $item becomes Shop\Catalog\Product, and apply_filters( 'hook', new Item() ) reports InvalidArgument. The new test case covers both: it crashes without the fix and reports that InvalidArgument with #59's check. This PR supersedes #59; thanks @schlessera for the report and the first fix (co-author of the commit).

"namespace { ... }" has no name, so reading it crashed with "Call to a
member function toString() on null". It now resets the namespace and
the use statements like any other namespace, so hook docblocks in it
don't resolve names through the previous block's imports.

Fixes #58

Co-authored-by: Alain Schlesser <83631+schlessera@users.noreply.github.com>
@alies-dev alies-dev added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 11, 2026
@alies-dev alies-dev self-assigned this Nov 11, 2026
@alies-dev
alies-dev merged commit 2d04428 into master Nov 11, 2026
6 checks passed
@alies-dev
alies-dev deleted the fix/global-namespace-block branch November 11, 2026 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix The PR will be included in 'Fixes' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fatal error: calling toString() on null

1 participant