Class: PreCommit::PhpCheck
- Inherits:
-
Object
- Object
- PreCommit::PhpCheck
- Defined in:
- lib/pre-commit/checks/php_check.rb
Instance Method Summary collapse
- #call ⇒ Object
- #check_name ⇒ Object
-
#display_error(error) ⇒ Object
Format an error line.
- #files_to_check ⇒ Object
- #reject_non_php(staged_files) ⇒ Object
- #run(php_files) ⇒ Object
- #run_check(file) ⇒ Object
- #should_run?(php_files) ⇒ Boolean
Instance Method Details
#call ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/pre-commit/checks/php_check.rb', line 11 def call php_files = reject_non_php(files_to_check) if should_run?(php_files) run(php_files) else # pretend the check passed and move on true end end |
#check_name ⇒ Object
21 22 23 |
# File 'lib/pre-commit/checks/php_check.rb', line 21 def check_name "PHP Lint" end |
#display_error(error) ⇒ Object
Format an error line.
68 69 70 |
# File 'lib/pre-commit/checks/php_check.rb', line 68 def display_error(error) "pre-commit: #{check_name.upcase} #{error}" end |
#files_to_check ⇒ Object
7 8 9 |
# File 'lib/pre-commit/checks/php_check.rb', line 7 def files_to_check Utils.staged_files('.').split(" ") end |
#reject_non_php(staged_files) ⇒ Object
50 51 52 |
# File 'lib/pre-commit/checks/php_check.rb', line 50 def reject_non_php(staged_files) staged_files.select { |f| f =~ /\.(php|engine|theme|install|inc|module|test)$/ } end |
#run(php_files) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pre-commit/checks/php_check.rb', line 25 def run(php_files) errors = [] php_files.each do |file| error = run_check(file) unless error.nil? errors << display_error(error) end end if errors.empty? true else $stderr.puts errors.join("\n") $stderr.puts $stderr.puts 'pre-commit: You can bypass this check using `git commit -n`' $stderr.puts false end end |
#run_check(file) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pre-commit/checks/php_check.rb', line 54 def run_check(file) # We force PHP to display errors otherwise they will likely end up in the # error_log and not in stdout. cmd = "php -d display_errors=1 -l #{file}" result = %x[ #{cmd} ] # Filter out the obvious note from PHP. result = result.split($/).find_all {|line| line !~ /Errors/}.join($/) # If PHP exited non-zero then there was a parse error. if ($? != 0) result end end |
#should_run?(php_files) ⇒ Boolean
46 47 48 |
# File 'lib/pre-commit/checks/php_check.rb', line 46 def should_run?(php_files) php_files.any? end |