Completed
Pull Request — master (#345)
by Enrico
05:30
created

AbstractCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 64.85%

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 24
cts 37
cp 0.6485
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 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)
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 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