|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hal\Application\Config; |
|
4
|
|
|
|
|
5
|
|
|
use Hal\Metric\Group\Group; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @package Hal\Application\Config |
|
9
|
|
|
*/ |
|
10
|
|
|
class Validator |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param Config $config |
|
14
|
|
|
* @throws ConfigException |
|
15
|
|
|
*/ |
|
16
|
|
|
public function validate(Config $config) |
|
17
|
|
|
{ |
|
18
|
|
|
// required |
|
19
|
|
|
if (!$config->has('files')) { |
|
20
|
|
|
throw new ConfigException('Directory to parse is missing or incorrect'); |
|
21
|
|
|
} |
|
22
|
|
|
foreach ($config->get('files') as $dir) { |
|
|
|
|
|
|
23
|
|
|
if (!file_exists($dir)) { |
|
24
|
|
|
throw new ConfigException(sprintf('Directory %s does not exist', $dir)); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
// extensions |
|
29
|
|
|
if (!$config->has('extensions')) { |
|
30
|
|
|
$config->set('extensions', 'php,inc'); |
|
31
|
|
|
} |
|
32
|
|
|
$config->set('extensions', explode(',', $config->get('extensions'))); |
|
33
|
|
|
|
|
34
|
|
|
// excluded directories |
|
35
|
|
|
if (!$config->has('exclude')) { |
|
36
|
|
|
$config->set('exclude', 'vendor,test,Test,tests,Tests,testing,Testing,bower_components,node_modules,cache,spec'); |
|
37
|
|
|
} |
|
38
|
|
|
$config->set('exclude', array_filter(explode(',', $config->get('exclude')))); |
|
39
|
|
|
|
|
40
|
|
|
// groups by regex |
|
41
|
|
|
if (!$config->has('groups')) { |
|
42
|
|
|
$config->set('groups', [['name' => 'all', 'match' => '!.*!']]); |
|
43
|
|
|
} |
|
44
|
|
|
$groupsRaw = $config->get('groups'); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
$groups = []; |
|
47
|
|
|
$config->set('groups', []); |
|
48
|
|
|
foreach ($groupsRaw as $groupRaw) { |
|
|
|
|
|
|
49
|
|
|
$groups[] = new Group($groupRaw['name'], $groupRaw['match']); |
|
50
|
|
|
} |
|
51
|
|
|
$config->set('groups', $groups); |
|
52
|
|
|
|
|
53
|
|
|
// parameters with values |
|
54
|
|
|
$keys = ['report-html', 'report-csv', 'report-violation', 'report-json', 'extensions', 'config']; |
|
55
|
|
|
foreach ($keys as $key) { |
|
56
|
|
|
$value = $config->get($key); |
|
|
|
|
|
|
57
|
|
|
if ($config->has($key) && empty($value) || true === $value) { |
|
58
|
|
|
throw new ConfigException(sprintf('%s option requires a value', $key)); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function help() |
|
67
|
|
|
{ |
|
68
|
|
|
return <<<EOT |
|
69
|
|
|
Usage: |
|
70
|
|
|
|
|
71
|
|
|
phpmetrics [...options...] <directories> |
|
72
|
|
|
|
|
73
|
|
|
Required: |
|
74
|
|
|
|
|
75
|
|
|
<directories> List of directories to parse, separated by a comma (,) |
|
76
|
|
|
|
|
77
|
|
|
Optional: |
|
78
|
|
|
|
|
79
|
|
|
--config=<file> Use a file for configuration |
|
80
|
|
|
--exclude=<directory> List of directories to exclude, separated by a comma (,) |
|
81
|
|
|
--extensions=<php,inc> List of extensions to parse, separated by a comma (,) |
|
82
|
|
|
--report-html=<directory> Folder where report HTML will be generated |
|
83
|
|
|
--report-csv=<file> File where report CSV will be generated |
|
84
|
|
|
--report-json=<file> File where report Json will be generated |
|
85
|
|
|
--report-violations=<file> File where XML violations report will be generated |
|
86
|
|
|
--git[=</path/to/git_binary>] Perform analyses based on Git History (default binary path: "git") |
|
87
|
|
|
--junit[=</path/to/junit.xml>] Evaluates metrics according to JUnit logs |
|
88
|
|
|
--quiet Quiet mode |
|
89
|
|
|
--version Display current version |
|
90
|
|
|
|
|
91
|
|
|
Examples: |
|
92
|
|
|
|
|
93
|
|
|
phpmetrics --report-html="./report" ./src |
|
94
|
|
|
|
|
95
|
|
|
Analyze the "./src" directory and generate a HTML report on the "./report" folder |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
phpmetrics --report-violations="./build/violations.xml" ./src,./lib |
|
99
|
|
|
|
|
100
|
|
|
Analyze the "./src" and "./lib" directories, and generate the "./build/violations.xml" file. This file could |
|
101
|
|
|
be read by any Continuous Integration Platform, and follows the "PMD Violation" standards. |
|
102
|
|
|
|
|
103
|
|
|
EOT; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|