-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Zend: suggest similar names for undefined function/class/method error #22589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jorgsowa
wants to merge
6
commits into
php:master
Choose a base branch
from
jorgsowa:feat/levenshtein-suggestions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
11e41df
Zend: suggest similar names for undefined function/method/class errors
jorgsowa a1cbd3c
Zend/JIT: fix undefined function suggestion under opcache JIT
jorgsowa 3c111b3
apply review comments
jorgsowa 96bd85d
Zend: suggest similar function names for undefined-function errors in…
jorgsowa 8a477d3
Zend: drop redundant () from function/method name suggestions
jorgsowa 160adb0
Zend/tests: organize name suggestion test files in consistent directory
jorgsowa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
15 changes: 15 additions & 0 deletions
15
Zend/tests/first_class_callable/constexpr/error_unknown_function_suggestion.phpt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --TEST-- | ||
| FCC in initializer suggests a similar function name for a missing function. | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| const Closure = strlenn(...); | ||
|
|
||
| var_dump(Closure); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Fatal error: Uncaught Error: Call to undefined function strlenn() (did you mean strlen?) in %s:%d | ||
| Stack trace: | ||
| #0 {main} | ||
| thrown in %s on line %d |
18 changes: 18 additions & 0 deletions
18
Zend/tests/first_class_callable/constexpr/error_unknown_function_suggestion_default_arg.phpt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --TEST-- | ||
| FCC in default argument suggests a similar function name for a missing function. | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function test(Closure $name = array_pussh(...)) { | ||
| var_dump($name); | ||
| } | ||
|
|
||
| try { | ||
| test(); | ||
| } catch (Error $e) { | ||
| echo $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Call to undefined function array_pussh() (did you mean array_push?) | ||
|
jorgsowa marked this conversation as resolved.
|
||
35 changes: 35 additions & 0 deletions
35
Zend/tests/name_suggestion/suggestion_undefined_class.phpt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| --TEST-- | ||
| Levenshtein suggestion for undefined class, interface and trait lookups | ||
| --FILE-- | ||
| <?php | ||
| // One edit away — should suggest | ||
| try { new ArrayObjekt(); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
| try { new StdClas(); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
|
|
||
| // One edit away for a longer name — should suggest | ||
| try { new ArrayIteratr(); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
|
|
||
| // Two edits away for a name >= 8 chars — adaptive threshold should suggest | ||
| try { new ArryObjct(); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
|
|
||
| // Completely wrong name — no suggestion | ||
| try { new Unicorn(); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
|
|
||
| // Interface | ||
| try { | ||
| $c = new class() implements Countble {}; | ||
| } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
|
|
||
| // Trait | ||
| try { | ||
| eval('class T { use NonExistntTrait; }'); | ||
| } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
| ?> | ||
| --EXPECTF-- | ||
| Error: Class "ArrayObjekt" not found (did you mean ArrayObject?) | ||
| Error: Class "StdClas" not found (did you mean stdClass?) | ||
| Error: Class "ArrayIteratr" not found (did you mean ArrayIterator?) | ||
| Error: Class "ArryObjct" not found (did you mean ArrayObject?) | ||
| Error: Class "Unicorn" not found | ||
| Error: Interface "Countble" not found (did you mean Countable?) | ||
| Error: Trait "NonExistntTrait" not found |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| --TEST-- | ||
| Levenshtein suggestion for undefined function in FCC context | ||
| --FILE-- | ||
| <?php | ||
| // Test 1: In default argument position | ||
|
|
||
| function test(Closure $name = array_pussh(...)) { | ||
| var_dump($name); | ||
| } | ||
|
|
||
| try { | ||
| test(); | ||
| } catch (Error $e) { | ||
| echo $e::class, ': ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| // Test 2: In constant initializer (via eval so the fatal can be caught for combined output) | ||
|
|
||
| try { | ||
| eval("const Closure2 = strlenn(...);"); | ||
| } catch (Error $e) { | ||
| echo $e::class, ': ', $e->getMessage(), "\n"; | ||
| } | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| Error: Call to undefined function array_pussh() (did you mean array_push?) | ||
| Error: Call to undefined function strlenn() (did you mean strlen?) |
26 changes: 26 additions & 0 deletions
26
Zend/tests/name_suggestion/suggestion_undefined_function.phpt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --TEST-- | ||
| Levenshtein suggestion for undefined function calls | ||
| --FILE-- | ||
| <?php | ||
| // One edit away — should suggest | ||
| try { strlenn("x"); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
|
|
||
| // One edit away for a longer name — should suggest | ||
| try { array_pussh([], 1); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
|
|
||
| // Two edits away for a name >= 8 chars — adaptive threshold should suggest | ||
| try { arry_pussh([], 1); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
|
|
||
| // Completely wrong name — no suggestion | ||
| try { nonexistentfunc(); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
|
|
||
| // Dynamic call — same suggestion logic applies | ||
| $f = "strlenx"; | ||
| try { $f("x"); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
| ?> | ||
| --EXPECT-- | ||
| Error: Call to undefined function strlenn() (did you mean strlen?) | ||
| Error: Call to undefined function array_pussh() (did you mean array_push?) | ||
| Error: Call to undefined function arry_pussh() (did you mean array_push?) | ||
| Error: Call to undefined function nonexistentfunc() | ||
| Error: Call to undefined function strlenx() (did you mean strlen?) |
29 changes: 29 additions & 0 deletions
29
Zend/tests/name_suggestion/suggestion_undefined_method.phpt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --TEST-- | ||
| Levenshtein suggestion for undefined method calls | ||
| --FILE-- | ||
| <?php | ||
| class Calculator { | ||
| public function add(int $a, int $b): int { return $a + $b; } | ||
| public function subtract(int $a, int $b): int { return $a - $b; } | ||
| public function multiply(int $a, int $b): int { return $a * $b; } | ||
| } | ||
|
|
||
| $calc = new Calculator(); | ||
|
|
||
| // One edit away — should suggest | ||
| try { $calc->addd(1, 2); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
|
|
||
| // Two edits away for a name >= 8 chars — adaptive threshold should suggest | ||
| try { $calc->subtarct(5, 3); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
|
|
||
| // Completely wrong name — no suggestion | ||
| try { $calc->nonexistent(); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
|
|
||
| // Static call — same logic applies | ||
| try { Calculator::addd(1, 2); } catch (Error $e) { echo $e::class, ': ', $e->getMessage(), "\n"; } | ||
| ?> | ||
| --EXPECT-- | ||
| Error: Call to undefined method Calculator::addd() (did you mean add?) | ||
| Error: Call to undefined method Calculator::subtarct() (did you mean subtract?) | ||
| Error: Call to undefined method Calculator::nonexistent() | ||
| Error: Call to undefined method Calculator::addd() (did you mean add?) |
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.