|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPSA\Command; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\ParserFactory; |
|
6
|
|
|
use PHPSA\Analyzer; |
|
7
|
|
|
use PHPSA\Application; |
|
8
|
|
|
use PHPSA\Configuration; |
|
9
|
|
|
use PHPSA\ConfigurationLoader; |
|
10
|
|
|
use Symfony\Component\Console\Command\Command; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
use Symfony\Component\Config\FileLocator; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Base Command providing config loading, memory usage |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractCommand extends Command |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param boolean $type |
|
23
|
|
|
* @return float |
|
24
|
|
|
*/ |
|
25
|
2 |
|
protected function getMemoryUsage($type) |
|
26
|
|
|
{ |
|
27
|
2 |
|
return round(memory_get_usage($type) / 1024 / 1024, 2); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $configFile |
|
32
|
|
|
* @param string $configurationDirectory |
|
33
|
|
|
* |
|
34
|
|
|
* @return Configuration |
|
35
|
|
|
*/ |
|
36
|
2 |
|
protected function loadConfiguration($configFile, $configurationDirectory) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
2 |
|
$loader = new ConfigurationLoader(new FileLocator([ |
|
39
|
2 |
|
getcwd(), |
|
40
|
|
|
$configurationDirectory |
|
41
|
2 |
|
])); |
|
42
|
|
|
|
|
43
|
2 |
|
$loadedConfig = $loader->load($configFile); |
|
44
|
|
|
|
|
45
|
2 |
|
return new Configuration( |
|
46
|
2 |
|
$loadedConfig[0], // config |
|
47
|
2 |
|
Analyzer\Factory::getPassesConfigurations(), |
|
48
|
2 |
|
$loadedConfig[1] // path to config file |
|
49
|
2 |
|
); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $application |
|
54
|
|
|
* |
|
55
|
|
|
* @return PhpParser\Parser |
|
56
|
|
|
*/ |
|
57
|
2 |
|
protected function createParser($application) |
|
58
|
|
|
{ |
|
59
|
2 |
|
$parserStr = $application->configuration->getValue('parser', 'prefer-7'); |
|
60
|
|
|
switch ($parserStr) { |
|
61
|
2 |
|
case 'prefer-7': |
|
62
|
2 |
|
$languageLevel = ParserFactory::PREFER_PHP7; |
|
63
|
2 |
|
break; |
|
64
|
|
|
case 'prefer-5': |
|
65
|
|
|
$languageLevel = ParserFactory::PREFER_PHP5; |
|
66
|
|
|
break; |
|
67
|
|
|
case 'only-7': |
|
68
|
|
|
$languageLevel = ParserFactory::ONLY_PHP7; |
|
69
|
|
|
break; |
|
70
|
|
|
case 'only-5': |
|
71
|
|
|
$languageLevel = ParserFactory::ONLY_PHP5; |
|
72
|
|
|
break; |
|
73
|
|
|
default: |
|
74
|
|
|
$languageLevel = ParserFactory::PREFER_PHP7; |
|
75
|
|
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
return (new ParserFactory())->create($languageLevel, new \PhpParser\Lexer\Emulative([ |
|
79
|
|
|
'usedAttributes' => [ |
|
80
|
2 |
|
'comments', |
|
81
|
2 |
|
'startLine', |
|
82
|
2 |
|
'endLine', |
|
83
|
2 |
|
'startTokenPos', |
|
84
|
|
|
'endTokenPos' |
|
85
|
2 |
|
] |
|
86
|
2 |
|
])); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.