I can't seem to get code completion working for my project. I have installed the plugin and linked to my php 7 bin folder.
Static functions seem to work okay but as soon as there is code similar to this:
File 1:
Class Test {
function test()
{
echo "hi";
}
}
File 2:
$tt = new Test();
$tt->test();
There is no code completion and no way for me to go to the definition. Is there any knows problems related to this or have it setup it in the wrong way?
Could you clarify on what symbols exactly go to definition and completion does not work? If you look into the fixtures folder and tests, you can see that simple cases like this are all tested
Mainly on my own classes, would warnings on the function documentation hinder this in any way?
`
class UserPermissions {
const FULL_ACCESS = 1;
/**
* Checks if the user has permission the the resource
* @param Int $required, The level that is required for access
* @param Int $userLevel, user id of user that requests access
*/
function CheckUserPermissionAndRedirect($required, $userLevel)
{
// Code
}
}
?>`
Within the file I can peek at the definition, but from another file this line of code does not allow for it
$UserPermissions->CheckUserPermissionAndRedirect(2, $_SESSION['user_level']);
All i get is "No definition is found for CheckUserPermissionAndRedirect". The same error is found for
$UserPermissions.
The variable $UserPermissions is initialized in an included file to:
$UserPermissions = new UserPermissions();
This is just one example
What do you mean with "in an included file"? Could you provide an SSCCE?
Sure thing, sorry!
setup.php
<?php
$UserPermission = new UserPermission();
?>
delivery.php
<?php
require('setup.php');
$UserPermissions->CheckUserPermissionAndRedirect(2, $_SESSION['user_level']);
// other code
?>
logic.php
<?php
class UserPermissions {
const FULL_ACCESS = 1;
/**
* Checks if the user has permission the the resource, legacy function
* @param Int $required, The level that is required for access
* @param Int $userLevel, user id of user that requests access
*/
function CheckUserPermissionAndRedirect($required, $userLevel)
{
return 1;
}
}
All files are in the same folder, the folder is then opened in VSC that is using PHP Intellisense plugin.
From setup.php I cannot access the class definition by clicking + CMD or by the context menu, it also wont suggest anything but "$UserPermission" when I start typing U.
Same story in delivery.php
Wont suggest or find any definitions.
Although when I am in the logic.php file I can peek at the definition for the method, but I'm unable to find its references (just says "loading").
This is my settings file if it's of any use. I turned on basic suggestions after I noticed that the plugin failed.
// Place your settings in this file to overwrite the default settings
{
"php.suggest.basic": true,
// Points to the PHP executable.
"php.validate.executablePath": "/usr/local/bin/php",
// The path to a PHP 7+ executable.
"php.executablePath": "/usr/local/bin/php",
"workbench.iconTheme": "material-icon-theme",
"search.exclude": {
"**/script/app.js": true,
"**/css/style.css": true
}
}
Thanks!
That is currently as designed. Variables are not indexed globally but resolved in the current function or file scope on demand (global variables are generally considered an anti pattern). Require and include statements are not considered for this.
Okay, why is that? I can imagine this being a rather normal situation, it certainly is for my project. I really appreciate the plugin but if I recall it worked fine in sublime, what's the main difference in the language server from there?
Nevermind, Sublime just parses the open files and throws matches from there to the suggestions. Although not as advanced as I imagine this being that actually helped a lot.
Still interested in understanding the problem. Even if i define the variable in delivery.php it wont give my any suggestion, is this also by design?
No reason, following include/require is simply not implemented. Indexing variables does not make sense because they are usually scoped, don't have a fully qualified structural element name and don't introduce any global symbol. No idea what Sublime does.
Please clarify what you mean with "Even if i define the variable in delivery.php it wont give my any suggestion"
Even if my files look like this (added static function which does seem to work as it should):
delivery.php
<?php
require('logic.php');
$UserPermission = new UserPermission();
$UserPermissions->CheckUserPermissionAndRedirect(2, $_SESSION['user_level']);
UserPermissions::StaticCheckUserPermissionAndRedirect(2, $_SESSION['user_level']); // <--- WORKS
// other code
?>
logic.php
<?php
class UserPermissions {
const FULL_ACCESS = 1;
/**
* Checks if the user has permission the the resource, legacy function
* @param Int $required, The level that is required for access
* @param Int $userLevel, user id of user that requests access
*/
function CheckUserPermissionAndRedirect($required, $userLevel)
{
return 1;
}
/**
* Checks if the user has permission the the resource, legacy function
* @param Int $required, The level that is required for access
* @param Int $userLevel, user id of user that requests access
*/
static function StaticCheckUserPermissionAndRedirect($required, $userLevel)
{
return 1;
}
}
what exactly does not work?
As i said previously. I wont give my any suggestion for the function or even be able to find the definition for either the class or the method on line 4 and 5 in delivery.php
Both work perfectly in the static example though!
The class in your example is named UserPermissions, but you instantiate UserPermission. Your variable is also named $UserPermission, but you call the method on $UserPermissions. If I correct the names, it works as expected.
God damn... you are correct.
So it is variables that are initiated in files that are then included that are not parsed by the extension, correct?
That would be a great feature to implement, considering that including a file in php is the same as just appending the included code into the file it is included.
Most helpful comment
The class in your example is named
UserPermissions, but you instantiateUserPermission. Your variable is also named$UserPermission, but you call the method on$UserPermissions. If I correct the names, it works as expected.