Skip to content

Instantly share code, notes, and snippets.

@mehlah
Created November 22, 2014 18:42
Show Gist options
  • Save mehlah/33792b932e4475a339cb to your computer and use it in GitHub Desktop.
Save mehlah/33792b932e4475a339cb to your computer and use it in GitHub Desktop.
diff --git a/.buildpacks b/.buildpacks
new file mode 100644
index 0000000..5eef61a
--- /dev/null
+++ b/.buildpacks
@@ -0,0 +1,2 @@
+https://github.com/heroku/heroku-buildpack-ruby.git
+https://github.com/heroku/heroku-buildpack-php.git
diff --git a/.travis.yml b/.travis.yml
index e74af49..deb7417 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -3,6 +3,10 @@ cache: bundler
rvm: 2.1.5
before_install:
+ - sudo apt-get update -qq
+ - sudo apt-get install php5 -y
+ - sudo apt-get install php-pear -y
+ - sudo pear install pear/PHP_CodeSniffer
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
diff --git a/Gemfile b/Gemfile
index 80e81e2..61c352b 100644
--- a/Gemfile
+++ b/Gemfile
@@ -20,6 +20,7 @@ gem "octokit"
gem "omniauth-github"
gem "paranoia", "~> 2.0"
gem "pg"
+gem "phpcs"
gem "rails", "4.1.5"
gem "resque", "~> 1.22.0"
gem "resque-retry"
diff --git a/Gemfile.lock b/Gemfile.lock
index 7a9ee62..aafa351 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -157,6 +157,8 @@ GEM
ast (>= 1.1, < 3.0)
slop (~> 3.4, >= 3.4.5)
pg (0.17.1)
+ phpcs (0.0.2)
+ json (~> 1.8)
poltergeist (1.5.1)
capybara (~> 2.1)
cliver (~> 0.3.1)
@@ -326,6 +328,7 @@ DEPENDENCIES
omniauth-github
paranoia (~> 2.0)
pg
+ phpcs
poltergeist
rails (= 4.1.5)
rails_12factor
diff --git a/app/models/repo_config.rb b/app/models/repo_config.rb
index 2faebd1..e655107 100644
--- a/app/models/repo_config.rb
+++ b/app/models/repo_config.rb
@@ -4,9 +4,10 @@ class RepoConfig
"ruby" => "yaml",
"java_script" => "json",
"coffee_script" => "json",
+ "php" => "json"
}
HOUND_CONFIG_FILE = ".hound.yml"
- STYLE_GUIDES = %w(ruby coffee_script java_script)
+ STYLE_GUIDES = %w(ruby coffee_script java_script php)
pattr_initialize :commit
diff --git a/app/models/style_checker.rb b/app/models/style_checker.rb
index fb74571..d1f9e43 100644
--- a/app/models/style_checker.rb
+++ b/app/models/style_checker.rb
@@ -40,6 +40,8 @@ def style_guide_class(filename)
StyleGuide::CoffeeScript
when /.+\.js\z/
StyleGuide::JavaScript
+ when /.+\.php\z/
+ StyleGuide::Php
else
StyleGuide::Unsupported
end
diff --git a/app/models/style_guide/php.rb b/app/models/style_guide/php.rb
new file mode 100644
index 0000000..1cb8098
--- /dev/null
+++ b/app/models/style_guide/php.rb
@@ -0,0 +1,26 @@
+# Determine Ruby style guide violations per-line.
+module StyleGuide
+ class Php < Base
+ DEFAULT_CONFIG_FILE = File.join(CONFIG_DIR, "php.json")
+
+ def violations_in_file(file)
+ phpcs.lint(file.content).map do |violation|
+ Violation.new(file, violation.line, violation.comment)
+ end
+ end
+
+ private
+
+ def phpcs
+ @phpcs ||= Phpcs::Phpcs.new(config['standard'])
+ end
+
+ def config
+ default_config.merge(repo_config.for(name))
+ end
+
+ def default_config
+ JSON.parse(File.read(DEFAULT_CONFIG_FILE))
+ end
+ end
+end
diff --git a/app/views/application/configuration.haml b/app/views/application/configuration.haml
index 9a2e9fa..707e475 100644
--- a/app/views/application/configuration.haml
+++ b/app/views/application/configuration.haml
@@ -87,3 +87,26 @@
java_script:
enabled: true
config_file: config/.jshint.json
+
+ %h3 Php
+
+ %p
+ To enable Php style checking, add the following to
+ %em.code .hound.yml
+ in the root of your project
+
+ %code.code-block
+ :preserve
+ php:
+ enabled: true
+
+ %p
+ Hound uses PSR2 standard by default, but you can override and chose one among PEAR, PHPCS, PSR1, PSR2, Squiz or Zend by adding a config file to your project and adding the following to
+ %em.code .hound.yml
+ in the root of your project.
+
+ %code.code-block
+ :preserve
+ php:
+ enabled: true
+ config_file: config/.php.json
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..657f313
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,5 @@
+{
+ "require": {
+ "php": "5.5.14"
+ }
+}
diff --git a/config/style_guides/php.json b/config/style_guides/php.json
new file mode 100644
index 0000000..f8583da
--- /dev/null
+++ b/config/style_guides/php.json
@@ -0,0 +1,3 @@
+{
+ "standard": "PSR2"
+}
diff --git a/spec/models/repo_config_spec.rb b/spec/models/repo_config_spec.rb
index 7d82b4c..14ccfd4 100644
--- a/spec/models/repo_config_spec.rb
+++ b/spec/models/repo_config_spec.rb
@@ -41,6 +41,8 @@
hello: world
java_script:
hello: world
+ php:
+ enabled: false
EOS
repo_config = RepoConfig.new(commit)
@@ -86,6 +88,18 @@
end
end
+ context "when Php is enabled" do
+ it "returns true for php" do
+ commit = double("Commit", file_content: <<-EOS.strip_heredoc)
+ php:
+ enabled: true
+ EOS
+ repo_config = RepoConfig.new(commit)
+
+ expect(repo_config).to be_enabled_for("php")
+ end
+ end
+
context "with legacy config file" do
context "when no style guide is enabled" do
it "only returns true for ruby" do
@@ -100,6 +114,7 @@
expect(repo_config).to be_enabled_for("ruby")
expect(repo_config).not_to be_enabled_for("coffee_script")
expect(repo_config).not_to be_enabled_for("java_script")
+ expect(repo_config).not_to be_enabled_for("php")
end
end
@@ -118,6 +133,7 @@
expect(repo_config).to be_enabled_for("ruby")
expect(repo_config).to be_enabled_for("coffee_script")
expect(repo_config).not_to be_enabled_for("java_script")
+ expect(repo_config).not_to be_enabled_for("php")
end
end
end
@@ -201,6 +217,20 @@
end
end
+ context "when php standard is specified" do
+ it "returns parsed config" do
+ config = config_for_file("php.json", <<-EOS.strip_heredoc)
+ {
+ "standard": "Zend"
+ }
+ EOS
+
+ result = config.for("php")
+
+ expect(result).to eq("standard" => "Zend")
+ end
+ end
+
context "when there is no Hound config file" do
it "returns empty config for all style guides" do
commit = double("Commit", file_content: nil)
@@ -242,6 +272,10 @@ def config_for_file(file_path, content)
java_script:
enabled: true
config_file: #{file_path}
+
+ php:
+ enabled: true
+ config_file: php.json
EOS
commit = double("Commit")
config = RepoConfig.new(commit)
diff --git a/spec/models/style_guide/php_spec.rb b/spec/models/style_guide/php_spec.rb
new file mode 100644
index 0000000..d800e14
--- /dev/null
+++ b/spec/models/style_guide/php_spec.rb
@@ -0,0 +1,53 @@
+require "fast_spec_helper"
+require "app/models/style_guide/base"
+require "app/models/style_guide/php"
+require "app/models/violation"
+require "phpcs"
+
+describe StyleGuide::Php, "#violations_in_file" do
+ context "with default configuration" do
+ describe "closing tag" do
+ it "returns violation" do
+ code = <<-CODE.strip_heredoc
+ <?php ?>
+ CODE
+ violations = violations_in(code)
+
+ expect(violations.first.messages).to include("A closing tag is not permitted at the end of a PHP file")
+ expect(violations.first.line_number).to eq(1)
+ end
+ end
+
+ describe "class name" do
+ it "returns violations" do
+ code = <<-CODE.strip_heredoc
+ <?php
+
+ namespace app/bar;
+
+ class foo {
+
+ }
+ CODE
+ violations = violations_in(code)
+
+ expect(violations.first.messages).to include("Class name \"foo\" is not in camel caps format")
+ expect(violations.first.line_number).to eq(5)
+ expect(violations.second.messages).to include("Opening brace of a class must be on the line after the definition")
+ expect(violations.second.line_number).to eq(5)
+ end
+ end
+ end
+
+ private
+
+ def violations_in(content, config = nil)
+ repo_config = double("RepoConfig", for: {"standard" => "PSR2"})
+ style_guide = StyleGuide::Php.new(repo_config)
+ style_guide.violations_in_file(build_file(content))
+ end
+
+ def build_file(content)
+ file = double(:file, content: content).as_null_object
+ end
+end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment