Skip to content

fix: a plain PHP function called through global narrows as in PHP - #130

Merged
heyJordanParker merged 4 commits into
masterfrom
fix/global-narrowing
Oct 15, 2026
Merged

heyJordanParker merged 4 commits into
masterfrom
fix/global-narrowing

Conversation

@heyJordanParker

@heyJordanParker heyJordanParker commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

What users can do now

A PHP# file can test a value with any plain PHP function through global. and use the narrowed value right away, as PHP code does with the bare call:

if (global.array_key_exists(key, prices)) {
    return key;                              // key is a string here, not Any?
}

if (global.in_array(value, sizes, true)) {
    return value;                            // value is an int here
}

if (global.count(sizes) > 0) {
    return sizes;                            // sizes is known to be non-empty
}

This works in every condition that narrows: if, &&, ||, !, ternaries, match guards and early returns. It covers array_key_exists, key_exists, in_array, count, sizeof, strlen, function_exists, is_callable, is_a and is_subclass_of. It also covers a plain PHP function called through an imported namespace, as in Pdf.render(…).

A call through global. also gets every other check the bare PHP call gets:

if (global.function_exists("helper")) {
    global.helper(text);                     // no missing-function error: the check proved it exists
}

global.strlen(text);                         // reported: a pure call whose result is unused
global.fresh(text);                          // reported: fresh() is #[\NoDiscard]
global.record(global.func_get_args());       // text counts as used, as func_get_args() reads it
global.Record(text);                         // reported: the function is declared as record()
global.preview(text);                        // reported: preview() is @experimental
global.intdiv(5, 0);                         // reported: a zero divisor
global.setcookie("a", "b", ["path": "/"], "/");      // reported: too many arguments for the array form
global.session_set_save_handler(() => true);        // reported: too few arguments for the callable form

Plain PHP files gain three fixes from the same change:

  • \function_exists('foo') now proves foo exists, as function_exists('foo') already did.
  • An unqualified fresh() in a namespace now gets the checks of the root fresh it runs.
  • \intdiv(5, 0) now gets the zero-divisor report intdiv(5, 0) gets, and the cookie and session checks run on fully qualified calls too.

Plugin authors: FunctionCallHook now takes the NamedCall view, which holds the function the call runs, its arguments and its span, instead of &FunctionCall. Hooks now run for every call of a function by its name, whatever form the call takes. A call of a function value, such as $f(), has no name and reaches no function call hook.

Why

Since #124, PHP# calls plain PHP functions as global.name(…). Every check that found a call by its function's name knew only PHP's bare name(…), so these checks stopped working. Only functions whose stubs declare @assert-if-true, such as is_string, is_int and is_null, kept narrowing.

How

Every check that finds a call by its function's name reads one view, NamedCall, and so do the function call hooks. A bare call and a PHP# namespace call both produce it, and its name is the function the call runs. A bare call follows PHP's lookup: an imported or fully qualified name as resolved, else the file's namespace, else the root namespace. The namespace lookup the call analyzer already used now lives in utils/names.rs, so every check and call analysis resolve global.name the same way.

Tests

  • a_plain_php_function_called_through_global_narrows_as_in_php has one row per narrowing function and per condition form. Each row asserts the type of the narrowed read, which is the type PHP's bare call gives.
  • a_plain_php_function_called_through_global_is_checked_as_in_php has one row per other check. Each row asserts the same issue codes for the PHP# call and its PHP twin.
  • a_function_call_hook_checks_every_call_of_its_function has one row per stdlib hook through global., plus PHP's \intdiv(5, 0).
  • function_call_hooks_see_a_global_call_as_php_call checks that a hook reports on, skips, and types global.f() exactly as it does \f().

Problem: since #124 a PHP# file calls a plain PHP function as
`global.name(…)`, but the analyzer narrowed only on PHP's bare `name(…)`.
So `if (global.array_key_exists(key, prices))` left `key` as `Any?`, and
the same held for `key_exists`, `in_array`, `count`, `strlen`,
`function_exists` and `is_a`, in every condition that narrows: `if`, `&&`,
`||`, `!`, ternaries, `match` guards and early returns. Only functions
whose stubs declare `@assert-if-true`, such as `is_string`, still narrowed.

Solution: narrowing reads every call of a function by name through one
view, `NamedCall`: the function's full name, its unqualified name when PHP
falls back to the root namespace, and the arguments. A bare call and a
PHP# namespace call (`global.name(…)`, `Pdf.render(…)`) both give one, so
every narrowing function narrows through `global.` as it does bare. The
namespace lookup the call analyzer used moves to `utils/names.rs` and takes
the resolved names and the codebase, so narrowing and analysis share it.

sharp/mago/crates/analyzer/
├── src/
│   ├── assertion.rs                     NamedCall and get_named_call; the special-function,
│   │                                    count, strlen, function_exists, is_callable and is_a
│   │                                    checks read it; is_function_call_to is gone
│   ├── utils/names.rs                   namespace_function_name moves here; it,
│   │                                    member_namespace and is_imported_namespace take the
│   │                                    resolved names and the codebase
│   ├── expression/call/function_call.rs its copy of namespace_function_name is gone
│   ├── expression/call/mod.rs           calls the shared namespace_function_name
│   ├── expression/access/mod.rs         passes the resolved names and the codebase
│   └── effects/check.rs                 passes the resolved names and the codebase
└── tests/sharp.rs                       a_plain_php_function_called_through_global_narrows_as_in_php:
                                         one row per narrowing function and condition form
…bare call gets

Problem: narrowing was not the only check that knew only PHP's bare
`name(…)`. Through `global.name(…)` a PHP# file lost five more:
`global.function_exists("foo")` proved nothing, so `global.foo()` after it
was still a missing function. `global.strlen(x);` as a statement got no
unused-statement report. A `#[\NoDiscard]` function got no
unused-function-call report, because `global.f()` was read as a static
method of a class named `global`. `global.func_get_args()` did not count as
reading the parameters. And `global.Record()` and an `@experimental`
function got no casing or experimental report. PHP lost two of these too:
`\function_exists('foo')` proved nothing, and an unqualified `fresh()` in a
namespace was looked up as `Demo\fresh` instead of the root `fresh` it runs.

Solution: every check that finds a call by its function's name reads one
view, `NamedCall`, in `utils/expression`. Its name is the function the call
runs: PHP's name by PHP's lookup (imported or fully qualified as resolved,
else the file's namespace, else the root namespace), and a PHP# namespace
call's function. Narrowing, function_exists, the unused-statement and
must-use checks, and the func_get_args check all read it. The namespace
call path skips the missing-function report for a proven function and runs
the casing and experimental checks the bare path runs, through one
`check_function_use`. `Context::resolve_function_name` is gone: its one
caller reads the view, and its leading-backslash branch never ran, because
the binder resolves a fully qualified name as an explicit one.

sharp/mago/crates/analyzer/
├── src/
│   ├── utils/expression/mod.rs          NamedCall, get_named_call and function_name; the
│   │                                    function id of a call reads the view
│   ├── assertion.rs                     reads the shared view; its private copy is gone
│   ├── utils/symbol_existence.rs        function_exists and defined read the view
│   ├── expression/call/function_call.rs a namespace call honours a proven function and gets
│   │                                    check_function_use, which the bare call shares
│   ├── statement/mod.rs                 the pure-call check reads the view; the must-use
│   │                                    lookup passes the codebase
│   ├── statement/function_like/
│   │   └── unused_parameter.rs          func_get_args is found in any named call
│   ├── context/mod.rs                   resolve_function_name is gone
│   ├── expression/binary/logical.rs     pass the resolved names and the codebase
│   ├── expression/conditional.rs        pass the resolved names and the codebase
│   ├── expression/match.rs              pass the resolved names and the codebase
│   ├── statement/if.rs                  pass the resolved names and the codebase
│   └── statement/switch.rs              pass the resolved names and the codebase
└── tests/sharp.rs                       a_plain_php_function_called_through_global_is_checked_as_in_php:
                                         one row per check, each with its PHP twin
…ntdiv() included

Problem: the function call hooks ran only inside the analysis of PHP's bare
`name(…)`, and took that node. So `global.intdiv(5, 0)` got no zero-divisor
report, and `global.setcookie`, `global.session_set_cookie_params` and
`global.session_set_save_handler` got no argument checks. The four stdlib
hooks also matched the name as written, so PHP's `\intdiv(5, 0)` missed the
check too.

Solution: `FunctionCallHook` takes the `NamedCall` view, the function the
call runs and its arguments and span, instead of `&FunctionCall`. The view
moves to `plugin/hook/call.rs` beside `StaticCall`, the view the static call
hook takes, and is built by `NamedCall::from_call`. `Call::analyze` runs the
hooks around both PHP's bare call and PHP#'s namespace call through one
`analyze_with_function_call_hooks`, so a hook that skips the call or gives it
a type does so for both. The four stdlib hooks match on the view's name, and
point their "this call" annotation at the whole call.

sharp/mago/crates/analyzer/
├── src/
│   ├── plugin/hook/call.rs              NamedCall moves here and is public; FunctionCallHook takes it
│   ├── plugin/hook/mod.rs               says FunctionCallHook takes the NamedCall view
│   ├── plugin/registry.rs               before_function_call and after_function_call take the view
│   ├── plugin/libraries/stdlib/
│   │   ├── math/intdiv.rs               matches the view's name
│   │   ├── cookie/setcookie.rs          matches the view's name
│   │   └── session/
│   │       ├── session_set_cookie_params.rs  matches the view's name
│   │       └── session_set_save_handler.rs   matches the view's name
│   ├── expression/call/mod.rs           runs the hooks around both call forms
│   ├── expression/call/function_call.rs analyze_with_function_call_hooks; FunctionCall::analyze
│   │                                    no longer runs hooks itself
│   ├── utils/expression/mod.rs          its copy of NamedCall is gone
│   ├── assertion.rs                     reads NamedCall::from_call
│   ├── utils/symbol_existence.rs        reads NamedCall::from_call
│   ├── statement/mod.rs                 reads NamedCall::from_call
│   └── statement/function_like/
│       └── unused_parameter.rs          reads NamedCall::from_call
└── tests/sharp.rs                       a_function_call_hook_checks_every_call_of_its_function: one row per
                                         stdlib hook; function_call_hooks_see_a_global_call_as_php_call: a hook
                                         reports, skips and types global.f() as \f()
…owing

Problem: #129 landed on master, and both sides changed the imports of the call
analyzer's dispatch.

Solution: keep both imports. Master's `seed_extension_type_arguments` and
this branch's `NamedCall` sit side by side, and git merged the dispatch body
itself: master's dropped class-value arms and the `false` argument of
`analyze_static_method_call` sit beside this branch's function call hooks.

sharp/mago/crates/analyzer/src/expression/call/
└── mod.rs    imports seed_extension_type_arguments and NamedCall
@heyJordanParker
heyJordanParker merged commit 224c379 into master Oct 15, 2026
19 checks passed
@heyJordanParker
heyJordanParker deleted the fix/global-narrowing branch November 3, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant