Skip to content
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

phpcs doesn't seem to follow rulesets with exclude-pattern #436

Open
kourylape opened this issue Oct 24, 2023 · 1 comment
Open

phpcs doesn't seem to follow rulesets with exclude-pattern #436

kourylape opened this issue Oct 24, 2023 · 1 comment
Labels
needs: contribution Contributions are welcome

Comments

@kourylape
Copy link

I'm new to nvim-lint and php is not my primary language, but I noticed an inconsistency between running phpcs locally versus inside neovim.

.phpcs.xml

<?xml version="1.0"?>
<ruleset name="MyApp">
  <file>blog/wp-content/themes/mytheme-child/</file>
  <file>app/</file>

  <rule ref="WordPress">
    <exclude-pattern>app/</exclude-pattern>
  </rule>

  <rule ref="PSR12">
    <exclude-pattern>blog/</exclude-pattern>
  </rule>

  <arg name="colors"/>
  <arg value="sp"/>
  <arg name="parallel" value="8"/>
  <arg name="report" value="full"/>

  <ini name="memory_limit" value="128M"/>
</ruleset>

plugins.lua

  {
    'mfussenegger/nvim-lint',
    config = function()
      require('plugin.linter')
    end
  },

plugins/linter.lua

local lint = require("lint")

-- PHP
local phpcs = lint.linters.phpcs
phpcs.cmd = "vendor/bin/phpcs"

-- Setup Linters
lint.linters_by_ft = {
	php = { "phpcs" },
}

Scenario 1: Inside neovim

  • When running :lua require('lint').try_lint() on app/test.php, I see WordPress related errors.
  • When running :lua require('lint').try_lint() on blog/wp-config.php, I see PSR12 related errors.

Scenario 2: Running phpcs

  • When running vendor/bin/phpcs app/test.php, I see only PSR12 related errors.
  • When running vendor/bin/phpcs blog/wp-config.php, I only see WordPress related errors.
@kourylape
Copy link
Author

I believe the issue is with stdin and lua/lint/linters/phpcs.lua. I can create a PR, but not sure if I'm heading down the right path.

I was able to match the functionality between running phpcs locally and using the linter in nvim.

I made the following changes to phpcs.lua.

  parser = function(output, bufnr)
    -- other unmodified code

    local filename = vim.fn.fnamemodify(vim.fn.bufname(bufnr), ":p")
    local files = decoded["files"]
    local messages = {}

    if files["STDIN"] == nil then
      if files[filename] == nil then
        return {}
      end
      messages = files[filename]["messages"]
    else
      messages = files["STDIN"]["messages"]
    end

   -- other unmodified code
  end,

And customized the linter in my config to be:

local lint = require("lint")

-- PHP
local phpcs = lint.linters.phpcs
phpcs.cmd = "vendor/bin/phpcs"
phpcs.stdin = false
phpcs.args = {
  "--report=json",
  "-q",
}

This does work, but is this something worth submitting?

@mfussenegger mfussenegger added the needs: contribution Contributions are welcome label Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: contribution Contributions are welcome
Projects
None yet
Development

No branches or pull requests

2 participants