0

I have some helper functions that I use everywhere in a CakePHP 2 application. They are underlined with red and reported as 'undefined functions' in the console. I know I can just ignore the errors, but I would like to have it working properly going forward, especially if I can manage to get alt-click working with it for models.

In my VS Code settings.json, I have the following:

"intelephense.stubs": [
        "apache", "bcmath", "bz2", "calendar", "com_dotnet", "Core", "ctype", "curl", "date", "dba", "dom", "enchant", "exif", "FFI", "fileinfo", "filter", "fpm", "ftp", "gd", "gettext", "gmp", "hash", "iconv", "imap", "intl", "json", "ldap", "libxml", "mbstring", "meta", "mysqli", "oci8", "odbc", "openssl", "pcntl", "pcre", "PDO", "pdo_ibm", "pdo_mysql", "pdo_pgsql", "pdo_sqlite", "pgsql", "Phar", "posix", "pspell", "readline", "Reflection", "session", "shmop", "SimpleXML", "snmp", "soap", "sockets", "sodium", "SPL", "sqlite3", "standard", "superglobals", "sysvmsg", "sysvsem", "sysvshm", "tidy", "tokenizer", "xml", "xmlreader", "xmlrpc", "xmlwriter", "xsl", "Zend OPcache", "zip", "zlib", "wordpress",
        "./Config/core.php"
    ],

It's my understanding that it is allowed to import custom .php files as stubs so that Intelephense recognizes them. However, no path that I enter will work. I have tried everything I can think of:

  1. C:/wamp64/www/MY_APP/app/
  2. ../App/Config
  3. simply not adding .php, simply not adding the file at all
  4. So many more!

How can I get Intelephense to recognize core.php, which essentially just holds a bunch of settings and some helper functions defined as, for example:

// just an alias for json decode
function jdec($json) {
    return json_decode($json, 1, 512, JSON_INVALID_UTF8_IGNORE);
}

Note: I am sure my difficulty with Intelephense is not strictly related to the CakePHP 2 framework.

Vael Victus
  • 3,966
  • 7
  • 34
  • 55

1 Answers1

1

I think you need to add CakePHP 2 location path to intelephense.environment.includePaths setting.

Here's an example of my workspace config for similar situation. I have the libraries I include outside of my workspace, so I specify paths to those like this:

"intelephense.environment.includePaths": [
  "/path/to/lib1",
  "/path/to/lib2"
]

After this, all the functions from included libraries are properly indexed by Intelephense and no longer marked as undefined.

Desert Fox
  • 364
  • 1
  • 7
  • Hmm, I think this is more for lib connectivity. My `core.php` is full of helper functions that I simply need to import, you could imagine it as a simple include for a "`global_functions.php`" solution in the vanilla PHP days of olde. – Vael Victus Nov 02 '20 at 15:13
  • So have you tried to add your `core.php` location to `intelephense.environment.includePaths` like I suggested? You might need to restart VS Code in order for changes to take effect. – Desert Fox Nov 03 '20 at 21:27
  • Yep :( and I went pretty crazy with it just to be sure. `"intelephense.environment.includePaths": [ "../lib/", "../app/Config/", "../app/Config/core.php", "Config/", "Config/core.php", "./Config/", "./Config/core.php", ]` And I am aware it's a relative path. – Vael Victus Nov 04 '20 at 00:56
  • I'm sure yours is the actual answer: turns out I had a function in `core.php` that made Intelephense ignore the entire file. That function simply needed to re-open PHP with a ` – Vael Victus Jan 16 '21 at 23:27