phpcsrb 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "PHP_CodeSniffer"]
2
+ path = PHP_CodeSniffer
3
+ url = git@github.com:squizlabs/PHP_CodeSniffer.git
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ language: php
2
+
3
+ php: 5.4
4
+
5
+ before_install:
6
+ - pyrus install pear/PHP_CodeSniffer
7
+ - phpenv rehash
8
+
9
+ script:
10
+ - gem update bundler
11
+ - bundle install
12
+ - bundle exec rake
13
+
14
+ notifications:
15
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Craftsmen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ [![Build Status](https://magnum.travis-ci.com/craftsmen/phpcs-rb.svg?token=CdVCrysG3xdFiGU8XWq1&branch=0.0.1)](https://magnum.travis-ci.com/craftsmen/phpcs-rb)
2
+
3
+ # Phpcs
4
+
5
+ Phpcs is a rubby wrapper for Php_CodeSniffer.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'phpcs'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install phpcs
22
+
23
+ ## Usage
24
+
25
+ You should look at `test/phpcs_test.rb` to understand how to use it.
26
+
27
+ Phpcs gem is using PSR2 rule. It is not possible for the moment to use another or create custom rules.
28
+
29
+ ## TODO
30
+
31
+ Make it possible to use other rules and create custom ruleset.
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/foo/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ t.pattern = 'test/**/*_test.rb'
6
+ end
7
+
8
+ task default: :test
@@ -0,0 +1,3 @@
1
+ module Phpcs
2
+ VERSION = "0.0.3"
3
+ end
data/lib/phpcs.rb ADDED
@@ -0,0 +1,52 @@
1
+ require 'tempfile'
2
+ require 'base64'
3
+ require 'open3'
4
+ require 'json'
5
+
6
+ module Phpcs
7
+ class Match < Struct.new(:line, :comment)
8
+ end
9
+
10
+ class Phpcs
11
+ STANDARDS = [
12
+ 'PEAR',
13
+ 'PHPCS',
14
+ 'PSR1',
15
+ 'PSR2',
16
+ 'Squiz',
17
+ 'Zend'
18
+ ]
19
+
20
+ def initialize(standard = nil)
21
+ @standard = (STANDARDS.include? standard) ? standard : 'PSR2'
22
+ end
23
+
24
+ def lint(content)
25
+ code = Base64.encode64(content).strip[0..16]
26
+ file = Tempfile.new(code)
27
+ file.write(content)
28
+ file.close
29
+
30
+ stdin, stdout, stderr, wait_thr = Open3.popen3('PHP_CodeSniffer/scripts/phpcs', file.path, "--standard=#{@standard}", '--report=json')
31
+
32
+ out = JSON.load(stdout.read)
33
+
34
+ if out['totals']['errors'] > 0
35
+ parse_matches(out)
36
+ else
37
+ []
38
+ end
39
+ end
40
+
41
+ def parse_matches(body)
42
+ path = body['files'].keys.first
43
+ errors = body['files'][path]
44
+ matches = []
45
+
46
+ errors['messages'].each do |error|
47
+ matches << Match.new(error['line'], error['message'])
48
+ end
49
+ matches
50
+ end
51
+ end
52
+ end
data/phpcs.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'phpcs/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'phpcsrb'
8
+ spec.version = Phpcs::VERSION
9
+ spec.authors = ['Kevin Chavanne', 'Blaine Schmeisser']
10
+ spec.email = ['kevin@craftsmen.io', 'blainesch@gmail.com']
11
+ spec.summary = %q{Ruby wrapper for phpcs}
12
+ spec.description = %q{Ruby wrapper for phpcs}
13
+ spec.homepage = 'https://github.com/blainesch/phpcs-rb'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'minitest', '~> 5.4'
24
+ spec.add_dependency 'json', '~> 1.8'
25
+ end
@@ -0,0 +1,11 @@
1
+ <?php
2
+
3
+ class Foo {
4
+
5
+ public function bar() {
6
+ return 'hello';
7
+ }
8
+
9
+ }
10
+
11
+ ?>
@@ -0,0 +1,84 @@
1
+ require 'minitest/autorun'
2
+ require 'phpcs'
3
+ require 'minitest/pride'
4
+
5
+ class PhpcsTest < Minitest::Test
6
+ def test_defaut_standard
7
+ file = File.open('test/fixtures/foo.php')
8
+ phpcs = Phpcs::Phpcs.new
9
+ results = phpcs.lint(file.read)
10
+
11
+ assert_equal results[0].line, 3
12
+ assert_equal results[0].comment, 'Each class must be in a namespace of at least one level (a top-level vendor name)'
13
+ assert_equal results[1].line, 3
14
+ assert_equal results[1].comment, 'Opening brace of a class must be on the line after the definition'
15
+ assert_equal results[2].line, 5
16
+ assert_equal results[2].comment, 'Spaces must be used to indent lines; tabs are not allowed'
17
+ assert_equal results[3].line, 5
18
+ assert_equal results[3].comment, 'Line indented incorrectly; expected at least 4 spaces, found 1'
19
+ assert_equal results[4].line, 5
20
+ assert_equal results[4].comment, 'Line indented incorrectly; expected 4 spaces, found 1'
21
+ assert_equal results[5].line, 5
22
+ assert_equal results[5].comment, 'Opening brace should be on a new line'
23
+ assert_equal results[6].line, 6
24
+ assert_equal results[6].comment, 'Spaces must be used to indent lines; tabs are not allowed'
25
+ assert_equal results[7].line, 6
26
+ assert_equal results[7].comment, 'Line indented incorrectly; expected at least 8 spaces, found 3'
27
+ assert_equal results[8].line, 7
28
+ assert_equal results[8].comment, 'Spaces must be used to indent lines; tabs are not allowed'
29
+ assert_equal results[9].line, 9
30
+ assert_equal results[9].comment, 'The closing brace for the class must go on the next line after the body'
31
+ assert_equal results[10].line, 11
32
+ assert_equal results[10].comment, 'A closing tag is not permitted at the end of a PHP file'
33
+ end
34
+
35
+ def test_pear_standard
36
+ file = File.open('test/fixtures/foo.php')
37
+ phpcs = Phpcs::Phpcs.new('PEAR')
38
+ results = phpcs.lint(file.read)
39
+
40
+ assert_equal results[0].line, 2
41
+ assert_equal results[0].comment, 'Missing file doc comment'
42
+ assert_equal results[1].line, 3
43
+ assert_equal results[1].comment, 'Missing class doc comment'
44
+ assert_equal results[2].line, 3
45
+ assert_equal results[2].comment, 'Opening brace of a class must be on the line after the definition'
46
+ assert_equal results[3].line, 5
47
+ assert_equal results[3].comment, 'Spaces must be used to indent lines; tabs are not allowed'
48
+ assert_equal results[4].line, 5
49
+ assert_equal results[4].comment, 'Line indented incorrectly; expected at least 4 spaces, found 1'
50
+ assert_equal results[5].line, 5
51
+ assert_equal results[5].comment, 'Missing function doc comment'
52
+ assert_equal results[6].line, 5
53
+ assert_equal results[6].comment, 'Line indented incorrectly; expected 4 spaces, found 1'
54
+ assert_equal results[7].line, 5
55
+ assert_equal results[7].comment, 'Opening brace should be on a new line'
56
+ assert_equal results[8].line, 6
57
+ assert_equal results[8].comment, 'Spaces must be used to indent lines; tabs are not allowed'
58
+ assert_equal results[9].line, 6
59
+ assert_equal results[9].comment, 'Line indented incorrectly; expected at least 8 spaces, found 3'
60
+ assert_equal results[10].line, 7
61
+ assert_equal results[10].comment, 'Spaces must be used to indent lines; tabs are not allowed'
62
+ end
63
+
64
+ def test_zend_standard
65
+ file = File.open('test/fixtures/foo.php')
66
+ phpcs = Phpcs::Phpcs.new('Zend')
67
+ results = phpcs.lint(file.read)
68
+
69
+ assert_equal results[0].line, 3
70
+ assert_equal results[0].comment, 'Opening brace of a class must be on the line after the definition'
71
+ assert_equal results[1].line, 5
72
+ assert_equal results[1].comment, 'Spaces must be used to indent lines; tabs are not allowed'
73
+ assert_equal results[2].line, 5
74
+ assert_equal results[2].comment, 'Opening brace should be on a new line'
75
+ assert_equal results[3].line, 6
76
+ assert_equal results[3].comment, 'Spaces must be used to indent lines; tabs are not allowed'
77
+ assert_equal results[4].line, 7
78
+ assert_equal results[4].comment, 'Spaces must be used to indent lines; tabs are not allowed'
79
+ assert_equal results[5].line, 11
80
+ assert_equal results[5].comment, 'A closing tag is not permitted at the end of a PHP file'
81
+ end
82
+
83
+
84
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phpcsrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kevin Chavanne
9
+ - Blaine Schmeisser
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-11-22 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.7'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '1.7'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '10.0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '10.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '5.4'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '5.4'
63
+ - !ruby/object:Gem::Dependency
64
+ name: json
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: '1.8'
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: '1.8'
79
+ description: Ruby wrapper for phpcs
80
+ email:
81
+ - kevin@craftsmen.io
82
+ - blainesch@gmail.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - .gitmodules
89
+ - .travis.yml
90
+ - Gemfile
91
+ - LICENSE
92
+ - README.md
93
+ - Rakefile
94
+ - lib/phpcs.rb
95
+ - lib/phpcs/version.rb
96
+ - phpcs.gemspec
97
+ - test/fixtures/foo.php
98
+ - test/phpcs_test.rb
99
+ homepage: https://github.com/blainesch/phpcs-rb
100
+ licenses:
101
+ - MIT
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.23.2
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Ruby wrapper for phpcs
124
+ test_files:
125
+ - test/fixtures/foo.php
126
+ - test/phpcs_test.rb