Completed
Push — master ( ded049...28d1f7 )
by Дмитрий
02:21
created

AbstractCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMemoryUsage() 0 4 1
A loadConfiguration() 0 15 1
B createParser() 0 31 5
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
    protected function getMemoryUsage($type)
26
    {
27
        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
    protected function loadConfiguration($configFile, $configurationDirectory)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $configurationDirectory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
37
    {
38
        $loader = new ConfigurationLoader(new FileLocator([
39
            getcwd(),
40
            $configurationDirectory
41
        ]));
42
43
        $loadedConfig = $loader->load($configFile);
44
45
        return new Configuration(
46
            $loadedConfig[0], // config
47
            Analyzer\Factory::getPassesConfigurations(),
48
            $loadedConfig[1] // path to config file
49
        );
50
    }
51
52
    /**
53
     * @param string $application
54
     *
55
     * @return PhpParser\Parser
56
     */
57
    protected function createParser($application)
58
    {
59
        $parserStr = $application->configuration->getValue('parser', 'prefer-7');
60
        switch ($parserStr) {
61
            case 'prefer-7':
62
                $languageLevel = ParserFactory::PREFER_PHP7;
63
                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
        return (new ParserFactory())->create($languageLevel, new \PhpParser\Lexer\Emulative([
79
            'usedAttributes' => [
80
                'comments',
81
                'startLine',
82
                'endLine',
83
                'startTokenPos',
84
                'endTokenPos'
85
            ]
86
        ]));
87
    }
88
}
89