phpcop 0.2.0 → 0.3.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de28dc7778c7b42e3b3c0c0607435ace13e54125
4
- data.tar.gz: 8e330895bc41a6ece84839874dafa19335f25b61
3
+ metadata.gz: 7ef9b83034da7e194fb6d2ed9f9d98ebd1c4b2ba
4
+ data.tar.gz: 2707957b3b3b80b58e03babe2acf2eec19808805
5
5
  SHA512:
6
- metadata.gz: c789c1f471f5c10fde1dfcd03ca18b92c3d8364fd004dc704d941dceadf2c038941190c3dc6d854c1bd1218aa59282c3780032394560a223fb812fae2107a8e9
7
- data.tar.gz: 3c9283b591b42e5f976f698e60fd20caeca3a64d88559e170ca373155be20e153727dc4502c0da40598a7385363fe9a4dc459d1abd7f3e60d0ddf0f5c09fc5f3
6
+ metadata.gz: 84d475a58cecdcf05d00ea15c36241af52ca3a4704ec8dc3e52947ce15a7e07d86af26b5d43865456bd0f86340609ebd7d842f3005aeb59d294c8c23ee22d06d
7
+ data.tar.gz: 0833da19a707ac122d95aedd14fcfd86642db1a5a66a5a3e23569e64c7593619d5b21aa6bb9c1849877e2b111dc226e49a1eff77772cf8d1eca5b369e3bbc860
data/bin/phpcop CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ # frozen_string_literal: true
2
4
 
3
5
  require 'phpcop'
4
6
  require 'benchmark'
data/config/default.yml CHANGED
@@ -22,3 +22,8 @@ AllCops:
22
22
  description: >
23
23
  PHP code MUST use only UTF-8 without BOM.
24
24
  see: 'http://www.php-fig.org/psr/psr-1/#2-2-character-encoding'
25
+ ccpm/methods:
26
+ enabled: true
27
+ description: >
28
+ http://www.php-fig.org/psr/psr-1/#4-3-methods
29
+ see: 'http://www.php-fig.org/psr/psr-1/#4-3-methods'
data/lib/phpcop/cli.rb CHANGED
@@ -1,10 +1,13 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
1
4
  module PhpCop
2
5
  # The CLI is a class responsible of handling all the command line interface
3
6
  # logic
4
7
  class CLI
5
8
  attr_reader :config_store
6
9
 
7
- MSG_END = '%s fichier traité. %s erreurs.'.freeze
10
+ MSG_END = '%s fichier traité. %s erreurs.'
8
11
 
9
12
  def initialize
10
13
  @config_store = ConfigStore.new
@@ -1,3 +1,6 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
1
4
  require 'yaml'
2
5
 
3
6
  module PhpCop
@@ -9,9 +12,9 @@ module PhpCop
9
12
  # Path to gem
10
13
  DEFAULT_PATH = File.realpath(File.join(File.dirname(__FILE__), '..', '..'))
11
14
  # Config file in gem
12
- DEFAULT_CONF = File.join(DEFAULT_PATH, 'config', 'default.yml').freeze
15
+ DEFAULT_CONF = File.join(DEFAULT_PATH, 'config', 'default.yml')
13
16
  # Config file in project PHP
14
- CUSTOMIZE_CONF = '.phpcop.yml'.freeze
17
+ CUSTOMIZE_CONF = '.phpcop.yml'
15
18
 
16
19
  def initialize
17
20
  @options = load_configuration(DEFAULT_CONF)
@@ -1,3 +1,6 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
1
4
  module PhpCop
2
5
  # Store configuration of gems.
3
6
  # Parse config file and test if option is enabled
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ require 'active_support/core_ext/string'
5
+
6
+ module PhpCop
7
+ module Cop
8
+ # Acronym to Class Constants, Properties and Methods
9
+ module CCPM
10
+ # Test method in php class
11
+ class Methods < Cop
12
+ MSG_ALERT_DESCRIB = 'Name method is not in camelCase().'
13
+
14
+ def initialize(file, line, line_number)
15
+ super(file, line.to_s, line_number)
16
+ test_line
17
+ end
18
+
19
+ private
20
+
21
+ def test_line
22
+ test_camel_case_method if @line.include?(' function ')
23
+ end
24
+
25
+ def test_camel_case_method
26
+ name = @line.slice!(/function.*()/).gsub('function ', '')
27
+ name = name.gsub(/\(.*/, '')
28
+ unless name.include?('__')
29
+ name_camel = name.camelize(:lower)
30
+ return_an_error(@file, @line_number, 0) unless name_camel.eql? name
31
+ end
32
+ end
33
+
34
+ def return_an_error(file, line, column)
35
+ @errors += 1
36
+ line += 1
37
+ puts format(MSG_ALERT_FILE, file, line, column)
38
+ puts MSG_ALERT_DESCRIB
39
+ puts ''
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module PhpCop
5
+ module Cop
6
+ # Generic class for all rules
7
+ class Cop
8
+ attr_reader :errors
9
+ # Display an error 'fichier.ext:line:column'
10
+ MSG_ALERT_FILE = '%s:%s:%s'
11
+
12
+ def initialize(file, line, line_number)
13
+ @count_open = 0
14
+ @count_close = 0
15
+ @errors = 0
16
+ @file = file
17
+ @line = line
18
+ @line_number = line_number
19
+ end
20
+
21
+ private
22
+
23
+ def return_an_error(file, line, column)
24
+ @errors += 1
25
+ line += 1
26
+ puts format(MSG_ALERT_FILE, file, line, column)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,9 +1,41 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
1
4
  module PhpCop
2
5
  module Cop
3
6
  module Files
4
7
  # This class test file php use correctly encoding type
5
- class PhpEncoding
6
- MSG_ALERT = '%s, is not encoding in UTF-8 without BOM.'.freeze
8
+ class PhpEncoding < Cop
9
+ MSG_ALERT_DESCRIB = 'Is not encoding in UTF-8 without BOM.'
10
+
11
+ def initialize(file, line, line_number)
12
+ super(file, line.to_s, line_number)
13
+ test_line
14
+ end
15
+
16
+ private
17
+
18
+ # Parse line and test if line is correctly enconding
19
+ def test_line
20
+ line_encoding = @line
21
+ unless line_encoding.ascii_only?
22
+ return_an_error(@file, @line_number, 0)
23
+ end
24
+ end
25
+
26
+ # TODO : Return position column to char non ASCII
27
+ def column_number
28
+ array = @line.split(/./)
29
+ array.each_with_index { |c, index| index unless c.ascii_only? }
30
+ end
31
+
32
+ def return_an_error(file, line, column)
33
+ @errors += 1
34
+ line += 1
35
+ puts format(MSG_ALERT_FILE, file, line, column)
36
+ puts MSG_ALERT_DESCRIB
37
+ puts ''
38
+ end
7
39
  end
8
40
  end
9
41
  end
@@ -1,59 +1,65 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
1
4
  module PhpCop
2
5
  module Cop
3
6
  module Files
4
7
  # This class test file php use correctly tags PHP '<?php ?>' or '<?= ?>'
5
- class PhpTags
6
- MSG_ALERT = '%s, dont use correctly PHP Tags.'.freeze
8
+ class PhpTags < Cop
9
+ attr_reader :count_open, :count_close
10
+
11
+ MSG_ALERT_DESCRIB = 'Dont use correctly PHP Tags.'
7
12
  TAG_OPEN = ['\<\?php', '\<\?='].freeze
8
13
  TAG_CLOSE = ['\?\>'].freeze
9
14
 
10
- attr_reader :errors
11
-
12
- def initialize(file)
13
- @count_open = 0
14
- @count_close = 0
15
- @errors = 0
15
+ def initialize(file, line = nil, line_number = nil)
16
+ super(file, line, line_number)
17
+ end
16
18
 
17
- # Open file and parse
18
- f = File.open(file, 'r')
19
- while (line = f.gets)
20
- parse_line(line)
19
+ def test_counters
20
+ # Test if tags open is equal to tags close
21
+ unless @count_open == @count_close
22
+ return_an_error(@file, @line_number, 0)
21
23
  end
22
-
23
- # Test counters if there are equal
24
- test_counters(file)
25
24
  end
26
25
 
27
- private
28
-
29
- def parse_line(line)
26
+ # Parse line and test if line use correctly tags PHP
27
+ def test_line(line, line_number)
28
+ @line = line
29
+ @line_number = line_number
30
30
  # Parse file and search tags exists
31
- parse_and_search_open_tag(line)
31
+ parse_and_search_open_tag
32
32
  # If file contains tag <?php or <?= search close tags
33
- parse_and_search_close_tag(line)
33
+ parse_and_search_close_tag
34
34
  end
35
35
 
36
+ private
37
+
36
38
  # Parse file and search tags
37
- def parse_and_search_open_tag(line)
39
+ def parse_and_search_open_tag
38
40
  TAG_OPEN.each do |rule|
39
- @count_open += 1 if Regexp.new(rule).match line
41
+ @count_open += 1 if match_line(rule)
40
42
  end
41
43
  end
42
44
 
43
- def parse_and_search_close_tag(line)
45
+ def parse_and_search_close_tag
44
46
  TAG_CLOSE.each do |rule|
45
- @count_close += 1 if Regexp.new(rule).match line
47
+ @count_close += 1 if match_line(rule)
46
48
  end
47
49
  end
48
50
 
49
- def test_counters(file)
50
- return_an_error(file) unless @count_open == @count_close
51
+ def match_line(rule)
52
+ Regexp.new(rule).match @line.encode('UTF-16be',
53
+ invalid: :replace,
54
+ replace: '?').encode('UTF-8')
51
55
  end
52
56
 
53
- # Return a text error with file name
54
- def return_an_error(file)
55
- puts format(MSG_ALERT, file)
57
+ def return_an_error(file, line, column)
56
58
  @errors += 1
59
+ line += 1
60
+ puts format(MSG_ALERT_FILE, file, line, column)
61
+ puts MSG_ALERT_DESCRIB
62
+ puts ''
57
63
  end
58
64
  end
59
65
  end
data/lib/phpcop/rule.rb CHANGED
@@ -1,3 +1,6 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
1
4
  module PhpCop
2
5
  # Object represent a rule in yaml configuration file
3
6
  class Rule
data/lib/phpcop/runner.rb CHANGED
@@ -1,3 +1,6 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
1
4
  module PhpCop
2
5
  # This class runner
3
6
  class Runner
@@ -40,39 +43,47 @@ module PhpCop
40
43
  # Test each rules
41
44
  def execute_tests_in_file(file, path)
42
45
  @count_files += 1
43
- file_with_path = format('%s/%s', path, file)
44
- types_default
46
+ line_number = 0
47
+ # Execute all test in file
48
+ f = File.open(format('%s/%s', path, file), 'r')
49
+ @php_tag = PhpCop::Cop::Files::PhpTags.new(file)
50
+ while (line = f.gets)
51
+ parse_file(file, line, line_number)
52
+ line_number += 1
53
+ end
54
+ @php_tag.test_counters
55
+ @count_errors += @php_tag.errors
56
+ end
57
+
58
+ def parse_file(file, line, line_number)
45
59
  @rules.each do |value|
46
- case value.type
47
- when 'files'
48
- test_file(file_with_path, value.type) unless @types['files']
49
- end
60
+ parse_rule(value, file, line, line_number) if value.enabled
50
61
  end
51
62
  end
52
63
 
53
- def test_file(file, type)
54
- @conf.rules_by_type(type).each do |value|
55
- case value.name
56
- when 'phpTags'
57
- test_file_php_tags(file) if value.enabled
58
- when 'phpEncoding'
59
- test_file_php_encoding if value.enabled
60
- end
64
+ def parse_rule(rule, file, line, line_number)
65
+ case rule.name
66
+ when 'phpTags'
67
+ test_file_php_tags(line, line_number)
68
+ when 'phpEncoding'
69
+ test_file_php_encoding(file, line, line_number)
70
+ when 'methods'
71
+ test_ccpm_methods(file, line, line_number)
61
72
  end
62
- @types['files'] = true
63
73
  end
64
74
 
65
- def test_file_php_tags(file)
66
- test = PhpCop::Cop::Files::PhpTags.new(file)
67
- @count_errors += test.errors
75
+ def test_file_php_tags(line, line_number)
76
+ @php_tag.test_line(line, line_number)
68
77
  end
69
78
 
70
- def test_file_php_encoding
71
- true
79
+ def test_file_php_encoding(file, line, line_number)
80
+ test = PhpCop::Cop::Files::PhpEncoding.new(file, line, line_number)
81
+ @count_errors += test.errors
72
82
  end
73
83
 
74
- def types_default
75
- @types.map { |k, _| @types[k] = false }
84
+ def test_ccpm_methods(file, line, line_number)
85
+ test = PhpCop::Cop::CCPM::Methods.new(file, line, line_number)
86
+ @count_errors += test.errors
76
87
  end
77
88
  end
78
89
  end
@@ -1,4 +1,6 @@
1
- # Module PHP Cop
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
2
4
  module Phpcop
3
- VERSION = '0.2.0'.freeze
5
+ VERSION = '0.3.1'
4
6
  end
data/lib/phpcop.rb CHANGED
@@ -1,3 +1,6 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
1
4
  require 'phpcop/version'
2
5
 
3
6
  require 'phpcop/configloader'
@@ -6,5 +9,7 @@ require 'phpcop/cli'
6
9
  require 'phpcop/runner'
7
10
  require 'phpcop/rule'
8
11
 
12
+ require 'phpcop/cop/cop.rb'
9
13
  require 'phpcop/cop/files/phptags.rb'
10
14
  require 'phpcop/cop/files/phpencoding.rb'
15
+ require 'phpcop/cop/ccpm/methods.rb'
metadata CHANGED
@@ -1,15 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phpcop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy VAILLANT
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-20 00:00:00.000000000 Z
11
+ date: 2016-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 4.2.6
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 4.2.6
13
33
  - !ruby/object:Gem::Dependency
14
34
  name: bundler
15
35
  requirement: !ruby/object:Gem::Requirement
@@ -74,23 +94,21 @@ executables:
74
94
  extensions: []
75
95
  extra_rdoc_files: []
76
96
  files:
77
- - ".gitignore"
78
- - Gemfile
79
97
  - LICENSE
80
98
  - README.md
81
- - Rakefile
82
99
  - bin/phpcop
83
100
  - config/default.yml
84
101
  - lib/phpcop.rb
85
102
  - lib/phpcop/cli.rb
86
103
  - lib/phpcop/configloader.rb
87
104
  - lib/phpcop/configstore.rb
105
+ - lib/phpcop/cop/ccpm/methods.rb
106
+ - lib/phpcop/cop/cop.rb
88
107
  - lib/phpcop/cop/files/phpencoding.rb
89
108
  - lib/phpcop/cop/files/phptags.rb
90
109
  - lib/phpcop/rule.rb
91
110
  - lib/phpcop/runner.rb
92
111
  - lib/phpcop/version.rb
93
- - phpcop.gemspec
94
112
  homepage: https://github.com/Dev-Crea/phpcop
95
113
  licenses: []
96
114
  metadata: {}
data/.gitignore DELETED
@@ -1,9 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in phpcop.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,2 +0,0 @@
1
- require 'bundler/gem_tasks'
2
- task default: :spec
data/phpcop.gemspec DELETED
@@ -1,26 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'phpcop/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = 'phpcop'
8
- spec.version = Phpcop::VERSION
9
- spec.authors = ['Jeremy VAILLANT']
10
- spec.email = ['vaillant.jeremy@dev-crea.com']
11
-
12
- spec.summary = 'Parse PHP code and analyser.'
13
- spec.description = 'Parser php files and gives non-respected guidelines.'
14
- spec.homepage = 'https://github.com/Dev-Crea/phpcop'
15
-
16
- r = %r{^(test|spec|features)/}
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(r) }
18
- spec.bindir = 'bin'
19
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
- spec.require_paths = ['lib']
21
-
22
- spec.add_development_dependency 'bundler', '~> 1.11'
23
- spec.add_development_dependency 'rake', '~> 10.0'
24
- spec.add_development_dependency 'overcommit', '~> 0.32.0'
25
- spec.add_development_dependency 'rubocop', '~> 0.38.0'
26
- end