1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\App; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\App\RuleLoader; |
14
|
|
|
use Lechimp\Dicto\Rules\Ruleset; |
15
|
|
|
use Symfony\Component\Yaml\Yaml; |
16
|
|
|
use Pimple\Container; |
17
|
|
|
use PhpParser\ParserFactory; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The App to be run from a script. |
21
|
|
|
*/ |
22
|
|
|
class App { |
23
|
|
|
/** |
24
|
|
|
* @var RuleLoader |
25
|
|
|
*/ |
26
|
|
|
protected $rule_loader; |
27
|
|
|
|
28
|
4 |
|
public function __construct() { |
29
|
4 |
|
ini_set('xdebug.max_nesting_level', 200); |
30
|
4 |
|
$this->rule_loader = new RuleLoader(); |
31
|
4 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Run the app. |
35
|
|
|
* |
36
|
|
|
* @param array $params from commandline |
37
|
|
|
* @return null |
38
|
|
|
*/ |
39
|
|
|
public function run(array $params) { |
40
|
|
|
if (count($params) < 2) { |
41
|
|
|
throw new \RuntimeException( |
42
|
|
|
"Expected path to rule-file as first parameter."); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$configs = array(); |
46
|
|
|
list($ruleset, $configs[]) = $this->load_rules_file($params[1]); |
47
|
|
|
|
48
|
|
|
// drop programm name and rule file path |
49
|
|
|
array_shift($params); |
50
|
|
|
array_shift($params); |
51
|
|
|
|
52
|
|
|
$this->load_extra_configs($params, $configs); |
53
|
|
|
|
54
|
|
|
$dic = $this->create_dic($ruleset, $configs); |
55
|
|
|
|
56
|
|
|
$dic["engine"]->run(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Load rules and initial config from a *.php-file. |
61
|
|
|
* |
62
|
|
|
* @param string $path |
63
|
|
|
* @return array ($ruleset, $config) |
64
|
|
|
*/ |
65
|
4 |
|
protected function load_rules_file($path) { |
66
|
4 |
|
if (!file_exists($path)) { |
67
|
|
|
throw new \RuntimeException("Unknown rule-file '$path'"); |
68
|
|
|
} |
69
|
4 |
|
list($ruleset, $config) = $this->rule_loader->load_rules_from($path); |
70
|
4 |
|
assert('is_array($config)'); |
71
|
4 |
|
assert('$ruleset instanceof \\Lechimp\\Dicto\\Rules\\RuleSet'); |
72
|
4 |
|
return array($ruleset, $config); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Load extra configs from yaml files. |
77
|
|
|
* |
78
|
|
|
* @param array $config_file_paths |
79
|
|
|
* @param array &$configs_array |
80
|
|
|
* @return null |
81
|
|
|
*/ |
82
|
|
|
protected function load_extra_configs(array $config_file_paths, array &$configs_array) { |
83
|
|
|
foreach ($config_file_paths as $config_file) { |
84
|
|
|
if (!file_exists($config_file)) { |
85
|
|
|
throw new \RuntimeException("Unknown config-file '$config_file'"); |
86
|
|
|
} |
87
|
|
|
$configs_array[] = Yaml::parse(file_get_contents($config_file)); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Create and initialize the DI-container. |
93
|
|
|
* |
94
|
|
|
* @param RuleSet $ruleset |
95
|
|
|
* @param array &$configs |
96
|
|
|
* @return Container |
97
|
|
|
*/ |
98
|
3 |
|
protected function create_dic(RuleSet $ruleset, array &$configs) { |
99
|
3 |
|
$container = new Container(); |
100
|
|
|
|
101
|
|
|
$container["config"] = function ($c) use (&$configs) { |
|
|
|
|
102
|
2 |
|
return new Config($configs); |
103
|
|
|
}; |
104
|
|
|
|
105
|
3 |
|
$container["ruleset"] = $ruleset; |
106
|
|
|
|
107
|
|
|
$container["engine"] = function($c) { |
108
|
|
|
return new Engine |
109
|
1 |
|
( $c["log"] |
110
|
1 |
|
, $c["config"] |
111
|
1 |
|
, $c["database_factory"] |
112
|
1 |
|
, $c["indexer_factory"] |
113
|
1 |
|
, $c["analyzer_factory"] |
114
|
1 |
|
, $c["source_status"] |
115
|
1 |
|
); |
116
|
|
|
}; |
117
|
|
|
|
118
|
|
|
$container["log"] = function ($c) { |
|
|
|
|
119
|
2 |
|
return new CLILogger(); |
120
|
|
|
}; |
121
|
|
|
|
122
|
|
|
$container["database_factory"] = function($c) { |
|
|
|
|
123
|
1 |
|
return new DBFactory(); |
124
|
|
|
}; |
125
|
|
|
|
126
|
|
|
$container["indexer_factory"] = function($c) { |
127
|
|
|
return new \Lechimp\Dicto\Indexer\IndexerFactory |
128
|
2 |
|
( $c["log"] |
129
|
2 |
|
, $c["php_parser"] |
130
|
2 |
|
, array |
131
|
2 |
|
( new \Lechimp\Dicto\Rules\ContainText() |
132
|
2 |
|
, new \Lechimp\Dicto\Rules\DependOn() |
133
|
2 |
|
, new \Lechimp\Dicto\Rules\Invoke() |
134
|
2 |
|
) |
135
|
2 |
|
); |
136
|
|
|
}; |
137
|
|
|
|
138
|
|
|
$container["analyzer_factory"] = function($c) { |
139
|
|
|
return new \Lechimp\Dicto\Analysis\AnalyzerFactory |
140
|
1 |
|
( $c["log"] |
141
|
1 |
|
, $c["ruleset"] |
142
|
1 |
|
); |
143
|
|
|
}; |
144
|
|
|
|
145
|
|
|
$container["php_parser"] = function($c) { |
|
|
|
|
146
|
2 |
|
return (new ParserFactory)->create(ParserFactory::PREFER_PHP7); |
147
|
|
|
}; |
148
|
|
|
|
149
|
|
|
$container["report_generator"] = function($c) { |
|
|
|
|
150
|
|
|
return new CLIReportGenerator(); |
151
|
|
|
}; |
152
|
|
|
|
153
|
2 |
|
$container["source_status"] = function($c) { |
154
|
2 |
|
return new SourceStatusGit($c["config"]->project_root()); |
155
|
|
|
}; |
156
|
|
|
|
157
|
3 |
|
return $container; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.