phpcop 0.1.0 → 0.2.0

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: 743c6a613d5bf326b3021c4945eced461400280c
4
- data.tar.gz: fc484f73e15bf03bc577e2f8ca05370b1be2e832
3
+ metadata.gz: de28dc7778c7b42e3b3c0c0607435ace13e54125
4
+ data.tar.gz: 8e330895bc41a6ece84839874dafa19335f25b61
5
5
  SHA512:
6
- metadata.gz: 08044f4b9cd57894a303c7e79695f401365b0ae80836bdcfc2ffe34e422242026aa869ece3776da04edb2f5f0e5c2e49e7929b3010e0e20ebfff65606ecdee0c
7
- data.tar.gz: 1cc847c3f46e232c3bd439d88ed2d3af62f2e21637e5cd4c81141feff6cd1f26d0a4bce73db80d203fb7e6fbbade9dbdce64bd1329b9792c3de00405680f0309
6
+ metadata.gz: c789c1f471f5c10fde1dfcd03ca18b92c3d8364fd004dc704d941dceadf2c038941190c3dc6d854c1bd1218aa59282c3780032394560a223fb812fae2107a8e9
7
+ data.tar.gz: 3c9283b591b42e5f976f698e60fd20caeca3a64d88559e170ca373155be20e153727dc4502c0da40598a7385363fe9a4dc459d1abd7f3e60d0ddf0f5c09fc5f3
data/README.md CHANGED
@@ -21,3 +21,6 @@ Or install it yourself as:
21
21
  ## Usage
22
22
 
23
23
  Go to project and execute command `phpcop`.
24
+
25
+ Use default config or create a .phpcop.yml in root to project. Copy default file
26
+ 'config/phpcop.yml'.
data/config/default.yml CHANGED
@@ -11,7 +11,14 @@ php:
11
11
  AllCops:
12
12
  # Exclude:
13
13
  # - '**/'
14
- phpTags:
14
+ files/phpTags:
15
15
  enabled: true
16
- description: 'PHP Tags'
16
+ description: >
17
+ PHP code MUST use the long <?php ?> tags or the short-echo <?= ?> tags; it
18
+ MUST NOT use the other tag variations.
17
19
  see: 'http://www.php-fig.org/psr/psr-1/#2-1-php-tags'
20
+ files/phpEncoding:
21
+ enabled: true
22
+ description: >
23
+ PHP code MUST use only UTF-8 without BOM.
24
+ see: 'http://www.php-fig.org/psr/psr-1/#2-2-character-encoding'
data/lib/phpcop/cli.rb CHANGED
@@ -2,50 +2,18 @@ module PhpCop
2
2
  # The CLI is a class responsible of handling all the command line interface
3
3
  # logic
4
4
  class CLI
5
- EXT = %w(.php .phtml .php.dist).freeze
6
- EXCLUDE_FOLDER = %w(. .. .git .gitignore vendor).freeze
5
+ attr_reader :config_store
7
6
 
8
- attr_reader :options, :config_store, :count_files
7
+ MSG_END = '%s fichier traité. %s erreurs.'.freeze
9
8
 
10
9
  def initialize
11
- @options = {}
12
10
  @config_store = ConfigStore.new
13
- @count_files = 0
14
- @count_errors = 0
15
11
  end
16
12
 
17
13
  # Run all files
18
14
  def run(_args = ARGV)
19
- Dir.foreach(Dir.pwd) do |file|
20
- run_folder(file, Dir.pwd)
21
- end
22
-
23
- puts format('%s fichier traité. %s erreurs.', @count_files, @count_errors)
24
- end
25
-
26
- private
27
-
28
- def foreach_folder(path)
29
- Dir.foreach(path) do |file|
30
- run_folder(file, path)
31
- end
32
- end
33
-
34
- def run_folder(file, path)
35
- unless EXCLUDE_FOLDER.include?(file)
36
- f = path + '/' + file
37
- if File.directory?(f)
38
- foreach_folder(f)
39
- elsif EXT.include?(File.extname(file))
40
- execute_tests_in_file(file, path)
41
- end
42
- end
43
- end
44
-
45
- def execute_tests_in_file(file, path)
46
- @count_files += 1
47
- test = PhpCop::Cop::Files::PhpTags.new(format('%s/%s', path, file))
48
- @count_errors += test.errors
15
+ runner = PhpCop::Runner.new(@config_store)
16
+ puts format(MSG_END, runner.count_files, runner.count_errors)
49
17
  end
50
18
  end
51
19
  end
@@ -1,23 +1,27 @@
1
1
  require 'yaml'
2
2
 
3
- module Phpcop
3
+ module PhpCop
4
4
  # This class load a files configuration to gem. So config file default and
5
5
  # customize config file in project scan
6
6
  class ConfigLoader
7
+ attr_reader :options
8
+
9
+ # Path to gem
10
+ DEFAULT_PATH = File.realpath(File.join(File.dirname(__FILE__), '..', '..'))
7
11
  # Config file in gem
8
- DEFAULT_CONF = 'config/default.yml'.freeze
12
+ DEFAULT_CONF = File.join(DEFAULT_PATH, 'config', 'default.yml').freeze
9
13
  # Config file in project PHP
10
14
  CUSTOMIZE_CONF = '.phpcop.yml'.freeze
11
15
 
12
16
  def initialize
13
- load_configuration(DEFAULT_CONF)
14
- load_configuration(CUSTOMIZE_CONF)
17
+ @options = load_configuration(DEFAULT_CONF)
18
+ @options ||= load_configuration(CUSTOMIZE_CONF)
15
19
  end
16
20
 
17
21
  private
18
22
 
19
23
  def load_configuration(file)
20
- File.exist?(file) ? YAML.load(file) : false
24
+ File.exist?(file) ? YAML.load(File.read(file)) : false
21
25
  end
22
26
  end
23
27
  end
@@ -1,7 +1,21 @@
1
1
  module PhpCop
2
- # Store configuration of gems
2
+ # Store configuration of gems.
3
+ # Parse config file and test if option is enabled
3
4
  class ConfigStore
5
+ attr_reader :options, :rules
6
+
4
7
  def initialize
8
+ opt = ConfigLoader.new
9
+ @options = opt.options
10
+ @rules = []
11
+
12
+ @options.fetch('AllCops').each do |val|
13
+ @rules.push PhpCop::Rule.new(val[0], val[1])
14
+ end
15
+ end
16
+
17
+ def rules_by_type(type)
18
+ @rules.select { |rule| rule.type.eql? type }
5
19
  end
6
20
  end
7
21
  end
@@ -0,0 +1,10 @@
1
+ module PhpCop
2
+ module Cop
3
+ module Files
4
+ # 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
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ module PhpCop
2
+ # Object represent a rule in yaml configuration file
3
+ class Rule
4
+ attr_reader :name, :type, :enabled, :description, :see
5
+
6
+ def initialize(name, option = {})
7
+ nt = name.split('/')
8
+ @name = nt[1]
9
+ @type = nt[0]
10
+ @enabled = option['enabled']
11
+ @description = option['description']
12
+ @see = option['see']
13
+ end
14
+ end
15
+ end
data/lib/phpcop/runner.rb CHANGED
@@ -1,5 +1,78 @@
1
1
  module PhpCop
2
2
  # This class runner
3
3
  class Runner
4
+ EXT = %w(.php .phtml .php.dist).freeze
5
+ EXCLUDE_FOLDER = %w(. .. .git .gitignore vendor).freeze
6
+
7
+ attr_reader :count_files, :count_errors
8
+
9
+ def initialize(config_store)
10
+ @count_files = 0
11
+ @count_errors = 0
12
+ @conf = config_store
13
+ @rules = @conf.rules
14
+ @types = { files: false }
15
+
16
+ Dir.foreach(Dir.pwd) do |file|
17
+ run_folder(file, Dir.pwd)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def foreach_folder(path)
24
+ Dir.foreach(path) do |file|
25
+ run_folder(file, path)
26
+ end
27
+ end
28
+
29
+ def run_folder(file, path)
30
+ unless EXCLUDE_FOLDER.include?(file)
31
+ f = path + '/' + file
32
+ if File.directory?(f)
33
+ foreach_folder(f)
34
+ elsif EXT.include?(File.extname(file))
35
+ execute_tests_in_file(file, path)
36
+ end
37
+ end
38
+ end
39
+
40
+ # Test each rules
41
+ def execute_tests_in_file(file, path)
42
+ @count_files += 1
43
+ file_with_path = format('%s/%s', path, file)
44
+ types_default
45
+ @rules.each do |value|
46
+ case value.type
47
+ when 'files'
48
+ test_file(file_with_path, value.type) unless @types['files']
49
+ end
50
+ end
51
+ end
52
+
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
61
+ end
62
+ @types['files'] = true
63
+ end
64
+
65
+ def test_file_php_tags(file)
66
+ test = PhpCop::Cop::Files::PhpTags.new(file)
67
+ @count_errors += test.errors
68
+ end
69
+
70
+ def test_file_php_encoding
71
+ true
72
+ end
73
+
74
+ def types_default
75
+ @types.map { |k, _| @types[k] = false }
76
+ end
4
77
  end
5
78
  end
@@ -1,4 +1,4 @@
1
1
  # Module PHP Cop
2
2
  module Phpcop
3
- VERSION = '0.1.0'.freeze
3
+ VERSION = '0.2.0'.freeze
4
4
  end
data/lib/phpcop.rb CHANGED
@@ -4,5 +4,7 @@ require 'phpcop/configloader'
4
4
  require 'phpcop/configstore'
5
5
  require 'phpcop/cli'
6
6
  require 'phpcop/runner'
7
+ require 'phpcop/rule'
7
8
 
8
9
  require 'phpcop/cop/files/phptags.rb'
10
+ require 'phpcop/cop/files/phpencoding.rb'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phpcop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
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-19 00:00:00.000000000 Z
11
+ date: 2016-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,7 +85,9 @@ files:
85
85
  - lib/phpcop/cli.rb
86
86
  - lib/phpcop/configloader.rb
87
87
  - lib/phpcop/configstore.rb
88
+ - lib/phpcop/cop/files/phpencoding.rb
88
89
  - lib/phpcop/cop/files/phptags.rb
90
+ - lib/phpcop/rule.rb
89
91
  - lib/phpcop/runner.rb
90
92
  - lib/phpcop/version.rb
91
93
  - phpcop.gemspec