1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FileCommand.php |
4
|
|
|
* |
5
|
|
|
* MIT LICENSE |
6
|
|
|
* |
7
|
|
|
* LICENSE: This source file is subject to the MIT license. |
8
|
|
|
* A copy of the licenses text was distributed alongside this |
9
|
|
|
* file (usually the repository or package root). The text can also |
10
|
|
|
* be obtained through one of the following sources: |
11
|
|
|
* * http://opensource.org/licenses/MIT |
12
|
|
|
* * https://github.com/suralc/pvra/blob/master/LICENSE |
13
|
|
|
* |
14
|
|
|
* @author suralc <[email protected]> |
15
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
16
|
|
|
*/ |
17
|
|
|
namespace Pvra\Console\Commands; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
use Pvra\AnalysisResult; |
21
|
|
|
use Pvra\Result\Collection as ResultCollection; |
22
|
|
|
use Pvra\Result\MessageFormatter; |
23
|
|
|
use Symfony\Component\Console\Helper\Table; |
24
|
|
|
use Pvra\Result\Reasoning; |
25
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
26
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class FileCommand |
30
|
|
|
* |
31
|
|
|
* @package Pvra\Console\Commands |
32
|
|
|
*/ |
33
|
|
|
class FileCommand extends PvraBaseCommand |
34
|
|
|
{ |
35
|
64 |
|
protected function configure() |
36
|
|
|
{ |
37
|
32 |
|
$this |
38
|
64 |
|
->setName('analyse:file') |
39
|
64 |
|
->setDescription('Analyse the requirements of a given file.'); |
40
|
|
|
|
41
|
64 |
|
parent::configure(); |
42
|
64 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
34 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
48
|
|
|
{ |
49
|
34 |
|
$file = $input->getArgument('target'); |
50
|
34 |
View Code Duplication |
if (!is_file($file) || !is_readable($file)) { |
51
|
2 |
|
throw new \InvalidArgumentException(sprintf('The target argument with value "%s" is not a valid file.', |
52
|
1 |
|
$file)); |
53
|
|
|
} |
54
|
|
|
|
55
|
32 |
|
$output->writeln(sprintf('<info>Running analysis for "%s"</info>', $this->formatOutputPath(realpath($file)))); |
56
|
|
|
|
57
|
|
|
|
58
|
32 |
|
if ($input->getOption('preventNameExpansion') && $this->hasNameDependentAnalyser()) { |
59
|
2 |
|
$output->writeln('<warn>Warning: Detection of newly introduced functions and classes may not work or produce' |
60
|
2 |
|
. ' false positives in namespaced contexts if you prevent name expansions</warn>'); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
32 |
|
$req = $this->createFileAnalyserInstance($input); |
64
|
|
|
|
65
|
32 |
|
$req->attachRequirementVisitors($this->createNodeWalkerInstances($input->getOption('libraryDataSource'))); |
66
|
|
|
|
67
|
32 |
|
$result = (new AnalysisResult()) |
68
|
32 |
|
->setMsgFormatter(new MessageFormatter( |
69
|
32 |
|
$this->createMessageLocatorInstance($input), false, true |
70
|
13 |
|
)); |
71
|
|
|
|
72
|
26 |
|
$req->setResultInstance($result); |
73
|
|
|
|
74
|
26 |
|
$req->run(); |
75
|
|
|
|
76
|
26 |
|
$output->writeln(sprintf('<info>Required version: %s</info>', $result->getRequiredVersion())); |
77
|
|
|
|
78
|
26 |
|
$tableData = []; |
79
|
26 |
|
foreach (array_reverse($result->getRequirements()) as $reasons) { |
80
|
26 |
|
foreach ($reasons as $reason) { |
81
|
26 |
|
$tableData[] = [$reason['version'], $reason['msg'], $reason['line']]; |
82
|
13 |
|
} |
83
|
13 |
|
} |
84
|
|
|
|
85
|
26 |
|
(new Table($output)) |
86
|
26 |
|
->setHeaders(['Version', 'Message', 'Line']) |
87
|
26 |
|
->setRows($tableData) |
88
|
26 |
|
->render(); |
89
|
|
|
|
90
|
26 |
|
if ($file = $input->getOption('saveAsFile')) { |
91
|
6 |
|
$this->writeToFile($file, $input->getOption('saveFormat'), (new ResultCollection())->add($result), $output); |
92
|
6 |
|
if (file_exists($file)) { |
93
|
4 |
|
$output->writeln(sprintf('<error>%s already exists. Cannot override an already existing file!</error>', |
94
|
2 |
|
$file)); |
95
|
2 |
|
} else { |
96
|
2 |
|
$output->writeln(sprintf('<info>Generating output file at %s</info>', $file)); |
97
|
2 |
|
$outData = iterator_to_array($result); |
98
|
2 |
|
switch ($input->getOption('saveFormat')) { |
99
|
2 |
|
case 'json': |
100
|
|
|
file_put_contents($file, json_encode($outData)); |
101
|
|
|
break; |
102
|
1 |
|
default: |
103
|
2 |
|
$output->writeln('<error>Invalid save format</error>'); |
104
|
1 |
|
} |
105
|
|
|
} |
106
|
3 |
|
} |
107
|
26 |
|
} |
108
|
|
|
} |
109
|
|
|
|