bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.64k stars 94 forks source link

Does intelephense.environment.phpVersion uses composer.json config.platform ? #1710

Open Kwaadpepper opened 3 years ago

Kwaadpepper commented 3 years ago

It would be nice if it could parse this parameter and use this to validate php code.

Of if it actually does this right now, it does not show any information about it.

EG:

    "config": {
          "platform": {
              "php": "7.3.26"
          }
      },
bmewburn commented 3 years ago

It does not look in composer.json for this.

yendor commented 8 months ago

I've managed to get this working in a basic sort of way in neovim by adding the following to my nvim-lspconfig configuration.

Not sure if there's an easier way to do this but at appears to be working for me

           require("lspconfig").intelephense.setup({
                on_init = function(client)
                    local project_path = client.workspace_folders[1].name

                    local function read_file(path)
                        local open = io.open
                        local file = open(path, "rb") -- r read mode and b binary mode
                        if not file then
                            return nil
                        end
                        local content = file:read("*a") -- *a or *all reads the whole file
                        file:close()
                        return content
                    end

                    local composer = vim.json.decode(read_file(project_path .. "/composer.json"))
                    print("Configuring intelephense for php " .. composer.config.platform.php)
                    client.config.settings["intelephense"].environment.phpVersion = composer.config.platform.php

                    client.notify("workspace/didChangeConfiguration", { settings = client.config.settings })
                    return true
                end,
                capabilities = capabilities,
                settings = {
                    ["intelephense"] = {
                        environment = {
                            phpVersion = "",
                        },
                        stubs = {
                            "mysqli",
                            "mbstring",
                            "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",
                            "random",
                            "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",
                            "memcached",
                            "memcached",
                        },
                    },
                },
            })
kedodrill commented 6 months ago

I've managed to get this working in a basic sort of way in neovim by adding the following to my nvim-lspconfig configuration.

Your solution works well for me!

kedodrill commented 5 months ago

@yendor with neovim 0.10.0 I had to change your code to the following:

on_init = function(client)
  local project_path = client.workspace_folders[1].name
  local function read_file(path)
    local open = io.open
    local file = open(path, "rb") -- r read mode and b binary mode
    if not file then
      return nil
    end
    local content = file:read("*a") -- *a or *all reads the whole file
    file:close()
    return content
  end

  local composer = vim.json.decode(read_file(project_path .. "/composer.json"))
  print("Configuring intelephense for php " .. composer.config.platform.php)
  client.config.settings.intelephense = vim.tbl_deep_extend('force', client.config.settings.intelephense, {
    environment = {
      phpVersion = composer.config.platform.php,
    },
  })
  client.notify("workspace/didChangeConfiguration", { settings = client.config.settings })
end,
settings = {
  intelephense = {}
},

You also need the intelephense key in the settings table else it won't work.

kedodrill commented 4 months ago

It does not look in composer.json for this.

Hi there, is this something that you are looking at implementing? It would be nice if intelephense did this automatically if the composer.json file exists. Appreciate the project, definitely helps out at work!