Class: Danger::DangerPhpCodesniffer

Inherits:
Plugin
  • Object
show all
Defined in:
lib/php_codesniffer/plugin.rb

Overview

Checks PHP files code standard using [PHP_CodeSniffer](github.com/squizlabs/PHP_CodeSniffer).

Examples:

Execute PHP_CodeSniffer with changed files only and ignored directory


php_codesniffer.ignore = "./vendor"
php_codesniffer.filtering = true
php_codesniffer.standard = "PSR2"
php_codesniffer.exec

See Also:

  • golface/danger-php_codesniffer

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fail_on_errorBoolean

An attribute for failing on errors In case phpcs finds errors, Danger will also fail This allows failing pipelines using the ‘–fail-on-errors` flag in Danger

Returns:

  • (Boolean)


37
38
39
# File 'lib/php_codesniffer/plugin.rb', line 37

def fail_on_error
  @fail_on_error
end

#filteringBoolean

An attribute for enabling filtering Only show messages within changed files.

Returns:

  • (Boolean)


31
32
33
# File 'lib/php_codesniffer/plugin.rb', line 31

def filtering
  @filtering
end

#ignoreString

An attribute for ignoring file or directory

Returns:

  • (String)


26
27
28
# File 'lib/php_codesniffer/plugin.rb', line 26

def ignore
  @ignore
end

#standardString

An attribute for setting code standard

Returns:

  • (String)


21
22
23
# File 'lib/php_codesniffer/plugin.rb', line 21

def standard
  @standard
end

Instance Method Details

#execvoid

This method returns an undefined value.

Execute and process phpcs CLL’s result.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/php_codesniffer/plugin.rb', line 42

def exec
  bin = phpcs_path
  raise "phpcs is not installed" unless bin

  summary = { "errors"=> 0, "warnings"=> 0, "fixable"=> 0 }
  report = []

  unless filtering
    result = run_phpcs(bin, ".")
    summary = result.fetch("totals")
    report = generate_report result
  else
    ((git.modified_files - git.deleted_files) + git.added_files)
      .select {|file| ((file.end_with? ".php") || (file.end_with? ".inc"))}
      .map { |file| file.gsub("#{Dir.pwd}/", '') }
      .each do |file|
        result = run_phpcs(bin, file)
        totals = result.fetch("totals")

        summary["errors"] += totals["errors"]
        summary["warnings"] += totals.fetch("warnings")
        summary["fixable"] += totals.fetch("fixable")

        if (totals["errors"] + totals.fetch("warnings") + totals.fetch("fixable")) > 0
          report.push(generate_report result)
        end
      end
  end

  if (summary["errors"] + summary["warnings"] + summary["fixable"]) > 0
    markdown "# PHP_CodeSniffer report"
    markdown generate_summary_markdown summary
    markdown report
  end
  
  if fail_on_error && summary["errors"] > 0
    fail "There are #{summary["errors"]} errors that need to be resolved."
  end
end