1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hgraca\Phorensic\Miner; |
4
|
|
|
|
5
|
|
|
use Hgraca\Phorensic\Miner\Code\CodeMinerInterface; |
6
|
|
|
use Hgraca\Phorensic\Miner\Code\PDepend\PDependAdapter; |
7
|
|
|
use Hgraca\Phorensic\Miner\Vcs\Git\Console\ShellAdapter; |
8
|
|
|
use Hgraca\Phorensic\Miner\Vcs\Git\GitAdapter; |
9
|
|
|
use Hgraca\Phorensic\Miner\Vcs\VcsMinerInterface; |
10
|
|
|
use Hgraca\Phorensic\SharedKernel\Command\StorageAwareCommandAbstract; |
11
|
|
|
use Hgraca\Phorensic\SharedKernel\Repository\FilesRepository; |
12
|
|
|
use Hgraca\Phorensic\SharedKernel\Repository\FilesRepositoryInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
|
17
|
|
|
final class ExtractCommand extends StorageAwareCommandAbstract |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritDoc} |
21
|
|
|
*/ |
22
|
|
|
protected function configure() |
23
|
|
|
{ |
24
|
|
|
$this |
25
|
|
|
->setName('phorensic:extract') |
26
|
|
|
->setDescription('Extract, from a repository, all information possible and store the results in a DB.') |
27
|
|
|
->addArgument('repositoryPath', InputArgument::OPTIONAL, 'What repository do you want to analyse?', getcwd()) |
28
|
|
|
->addArgument( |
29
|
|
|
'since', |
30
|
|
|
InputArgument::OPTIONAL, |
31
|
|
|
'Since when do you want to analyse? (ie: "2010-11-23", defaults to last 3 months)' |
32
|
|
|
) |
33
|
|
|
->addArgument( |
34
|
|
|
'dbPath', |
35
|
|
|
InputArgument::OPTIONAL, |
36
|
|
|
'To where do you want to save the extracted data? (sqlite DB)' |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
44
|
|
|
{ |
45
|
|
|
$dbPath = $this->setUpDatabase($input); |
46
|
|
|
$storage = $this->getStorageService($dbPath); |
47
|
|
|
|
48
|
|
|
$this->extractFileChangeRates($input, $output, $storage); |
49
|
|
|
$this->extractPhpFilesMetrics($input, $output, $storage); |
50
|
|
|
|
51
|
|
|
$output->writeln('Extracted data has been stored in: ' . $dbPath); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function extractFileChangeRates(InputInterface $input, OutputInterface $output, FilesRepositoryInterface $storage) |
55
|
|
|
{ |
56
|
|
|
$output->writeln('Extracting file change rates...'); |
57
|
|
|
$storage->storeFilesChangeRate( |
58
|
|
|
$this->getVcsMiner()->findMostChangedFiles($input->getArgument('repositoryPath'), 'since') |
|
|
|
|
59
|
|
|
); |
60
|
|
|
$output->writeln('Finished extracting file change rates.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function extractPhpFilesMetrics(InputInterface $input, OutputInterface $output, FilesRepositoryInterface $storage) |
64
|
|
|
{ |
65
|
|
|
$output->writeln('Extracting PHP files metrics...'); |
66
|
|
|
$phpFilesList = $storage->findPhpFiles(); |
67
|
|
|
$analysis = $this->getCodeMiner()->mine($phpFilesList, $input->getArgument('repositoryPath')); |
|
|
|
|
68
|
|
|
$storage->storePhpFilesMetrics($analysis); |
69
|
|
|
$output->writeln('Finished extracting PHP files metrics.'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function setUpDatabase(InputInterface $input): string |
73
|
|
|
{ |
74
|
|
|
$dbPath = $this->getDatabasePath($input); |
75
|
|
|
copy(ROOT_DIR . "/storage/template.sqlite", $dbPath); |
76
|
|
|
|
77
|
|
|
return $dbPath; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function getVcsMiner(): VcsMinerInterface |
81
|
|
|
{ |
82
|
|
|
return new GitAdapter(new ShellAdapter()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function getCodeMiner(): CodeMinerInterface |
86
|
|
|
{ |
87
|
|
|
return new PDependAdapter(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function getStorageService(string $dbPath): FilesRepositoryInterface |
91
|
|
|
{ |
92
|
|
|
return new FilesRepository($this->getDatabaseClient($dbPath)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.