|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hal\Application; |
|
3
|
|
|
|
|
4
|
|
|
use Hal\Application\Config\ConfigException; |
|
5
|
|
|
use Hal\Application\Config\Parser; |
|
6
|
|
|
use Hal\Application\Config\Validator; |
|
7
|
|
|
use Hal\Component\File\Finder; |
|
8
|
|
|
use Hal\Component\Issue\Issuer; |
|
9
|
|
|
use Hal\Report; |
|
10
|
|
|
use Hal\Violation\ViolationParser; |
|
11
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatter; |
|
12
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class Application |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param $argv |
|
20
|
|
|
*/ |
|
21
|
|
|
public function run($argv) |
|
22
|
|
|
{ |
|
23
|
|
|
// formatter |
|
24
|
|
|
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, new OutputFormatter()); |
|
25
|
|
|
|
|
26
|
|
|
// issues and debug |
|
27
|
|
|
$issuer = (new Issuer($output));//->enable(); |
|
28
|
|
|
|
|
29
|
|
|
// config |
|
30
|
|
|
$config = (new Parser())->parse($argv); |
|
31
|
|
|
try { |
|
32
|
|
|
(new Validator())->validate($config); |
|
33
|
|
|
} catch (ConfigException $e) { |
|
34
|
|
|
|
|
35
|
|
|
if ($config->has('help')) { |
|
36
|
|
|
$output->writeln((new Validator())->help()); |
|
37
|
|
|
exit(0); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($config->has('version')) { |
|
41
|
|
|
$output->writeln(sprintf("PhpMetrics %s <http://phpmetrics.org>\nby Jean-François Lépine <https://twitter.com/Halleck45>", getVersion())); |
|
42
|
|
|
exit(0); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$output->writeln(sprintf("\n<error>%s</error>\n", $e->getMessage())); |
|
46
|
|
|
$output->writeln((new Validator())->help()); |
|
47
|
|
|
exit(1); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($config->has('quiet')) { |
|
51
|
|
|
$output->setVerbosity(ConsoleOutput::VERBOSITY_QUIET); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// find files |
|
55
|
|
|
$finder = new Finder($config->get('extensions'), $config->get('exclude')); |
|
|
|
|
|
|
56
|
|
|
$files = $finder->fetch($config->get('files')); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
// analyze |
|
59
|
|
|
try { |
|
60
|
|
|
$metrics = (new Analyze($config, $output, $issuer))->run($files); |
|
61
|
|
|
}catch(ConfigException $e) { |
|
62
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
|
63
|
|
|
exit(1); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// violations |
|
67
|
|
|
(new ViolationParser($config, $output))->apply($metrics); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
// report |
|
70
|
|
|
(new Report\Cli\Reporter($config, $output))->generate($metrics); |
|
71
|
|
|
(new Report\Html\Reporter($config, $output))->generate($metrics); |
|
72
|
|
|
(new Report\Violations\Xml\Reporter($config, $output))->generate($metrics); |
|
73
|
|
|
|
|
74
|
|
|
// end |
|
75
|
|
|
$output->writeln(''); |
|
76
|
|
|
$output->writeln('<info>Done</info>'); |
|
77
|
|
|
} |
|
78
|
|
|
} |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exitexpression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.