Repository navigation
fix: a plain PHP function called through global narrows as in PHP - #130
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:This works in every condition that narrows:
if,&&,||,!, ternaries,matchguards and early returns. It coversarray_key_exists,key_exists,in_array,count,sizeof,strlen,function_exists,is_callable,is_aandis_subclass_of. It also covers a plain PHP function called through an imported namespace, as inPdf.render(…).A call through
global.also gets every other check the bare PHP call gets:Plain PHP files gain three fixes from the same change:
\function_exists('foo')now provesfooexists, asfunction_exists('foo')already did.fresh()in a namespace now gets the checks of the rootfreshit runs.\intdiv(5, 0)now gets the zero-divisor reportintdiv(5, 0)gets, and the cookie and session checks run on fully qualified calls too.Plugin authors:
FunctionCallHooknow takes theNamedCallview, 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 barename(…), so these checks stopped working. Only functions whose stubs declare@assert-if-true, such asis_string,is_intandis_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 inutils/names.rs, so every check and call analysis resolveglobal.namethe same way.Tests
a_plain_php_function_called_through_global_narrows_as_in_phphas 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_phphas 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_functionhas one row per stdlib hook throughglobal., plus PHP's\intdiv(5, 0).function_call_hooks_see_a_global_call_as_php_callchecks that a hook reports on, skips, and typesglobal.f()exactly as it does\f().