Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ issue](https://github.com/brigade/overcommit/issues/238) for more details.
* [NginxTest](lib/overcommit/hook/pre_commit/nginx_test.rb)
* [PhpCs](lib/overcommit/hook/pre_commit/php_cs.rb)
* [PhpLint](lib/overcommit/hook/pre_commit/php_lint.rb)
* [PhpStan](lib/overcommit/hook/pre_commit/php_stan.rb)
* [Pronto](lib/overcommit/hook/pre_commit/pronto.rb)
* [PuppetLint](lib/overcommit/hook/pre_commit/puppet_lint.rb)
* [PuppetMetadataJsonLint](lib/overcommit/hook/pre_commit/puppet_metadata_json_lint.rb)
Expand Down
8 changes: 8 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,14 @@ PreCommit:
flags: ['--standard=PSR2', '--report=csv']
include: '**/*.php'

PhpStan:
description: 'Analyze with phpstan'
enabled: false
command: 'phpstan'
flags: ['analyze', '--errorFormat=raw']
include:
- '**/*.php'

Pronto:
enabled: false
description: 'Analyzing with pronto'
Expand Down
28 changes: 28 additions & 0 deletions lib/overcommit/hook/pre_commit/php_stan.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Overcommit::Hook::PreCommit
# Runs `phpstan` against any modified PHP files.
# For running `phpstan` with Laravel, it requires setup with `ide_helper`.
#
# References:
# https://github.com/phpstan/phpstan/issues/239
# https://gist.github.com/edmondscommerce/89695c9cd2584fefdf540fb1c528d2c2
class PhpStan < Base
MESSAGE_REGEX = /^(?<file>.+)\:(?<line>\d+)\:(?<message>.+)/

def run
messages = []

result = execute(command, args: applicable_files)

unless result.success?
messages += result.stdout.lstrip.split("\n")
end

return :pass if messages.empty?

extract_messages(
messages,
MESSAGE_REGEX
)
end
end
end
47 changes: 47 additions & 0 deletions spec/overcommit/hook/pre_commit/php_stan_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'spec_helper'

describe Overcommit::Hook::PreCommit::PhpStan do
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
let(:context) { double('context') }
subject { described_class.new(config, context) }

before do
subject.stub(:applicable_files).and_return(%w[sample.php])
end

context 'when phpstan exits successfully' do
before do
sample_output = ''

result = double('result')
result.stub(:success?).and_return(true)
result.stub(:stdout).and_return(sample_output)
result.stub(:status).and_return(0)
subject.stub(:execute).and_return(result)
end

it { should pass }
end

context 'when phpstan exits unsuccessfully' do
let(:result) { double('result') }

before do
result.stub(:success?).and_return(false)
result.stub(:status).and_return(2)
subject.stub(:execute).and_return(result)
end

context 'and it reports a warning' do
before do
sample_output = [
'/sample1.php:14:Call to an undefined static method Sample1::where()',
'/sample2.php:17:Anonymous function has an unused use $myVariable.'
].join("\n")
result.stub(:stdout).and_return(sample_output)
end

it { should fail_hook }
end
end
end