Configuration   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 58
c 1
b 0
f 0
dl 0
loc 136
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 13 5
A __construct() 0 2 1
A init() 0 10 2
A oneLevelIndentationString() 0 20 4
A destroyInstance() 0 3 1
A getLinterConfiguration() 0 7 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Pluswerk\TypoScriptAutoFixer\Adapter\Configuration;
5
6
use Helmich\TypoScriptLint\Linter\LinterConfiguration;
7
use Helmich\TypoScriptLint\Linter\Sniff\IndentationSniff;
8
use Pluswerk\TypoScriptAutoFixer\Adapter\Configuration\Reader\AbstractConfigurationReader;
9
use Pluswerk\TypoScriptAutoFixer\Adapter\Configuration\Reader\DefaultConfigurationReader;
10
use Pluswerk\TypoScriptAutoFixer\Exception\ConfigurationInstantiationException;
11
use Symfony\Component\Config\Definition\Processor;
12
13
final class Configuration
14
{
15
    private static $instance;
16
17
    /**
18
     * @var LinterConfiguration
19
     */
20
    private $linterConfiguration;
21
22
    /**
23
     * @var bool
24
     */
25
    private $initNeeded = false;
26
27
    /**
28
     * @var bool
29
     */
30
    private $initialised = false;
31
32
    /**
33
     * @var array
34
     */
35
    private $config = [
36
        'sniffs' => [
37
            0 => [
38
                'class'      => 'Indentation',
39
                'parameters' => [
40
                    'useSpaces'        => true,
41
                    'indentPerLevel'   => 2,
42
                    'indentConditions' => true,
43
                ],
44
            ],
45
            1 => [
46
                'class' => 'DeadCode',
47
            ],
48
            2 => [
49
                'class' => 'OperatorWhitespace',
50
            ],
51
            4 => [
52
                'class' => 'DuplicateAssignment',
53
            ],
54
            5 => [
55
                'class' => 'EmptySection',
56
            ],
57
            6 => [
58
                'class'      => 'NestingConsistency',
59
                'parameters' => [
60
                    'commonPathPrefixThreshold' => 1,
61
                ],
62
            ],
63
        ],
64
        'paths'        => [],
65
        'filePatterns' => [],
66
    ];
67
68
    /**
69
     * @return Configuration
70
     */
71
    public static function getInstance(): Configuration
72
    {
73
        if (!(self::$instance instanceof Configuration)) {
74
            self::$instance = new Configuration();
75
        } elseif (self::$instance->initNeeded) {
76
            throw new ConfigurationInstantiationException('Configuration need initialisation after instantiation!');
77
        }
78
79
        if (!self::$instance->initNeeded && !self::$instance->initialised) {
80
            self::$instance->initNeeded = true;
81
        }
82
83
        return self::$instance;
84
    }
85
86
    /**
87
     * @return void
88
     */
89
    public static function destroyInstance(): void
90
    {
91
        self::$instance = null;
92
    }
93
94
    private function __construct()
95
    {
96
    }
97
98
    /**
99
     * @return void
100
     */
101
    public function init(AbstractConfigurationReader $configurationReader = null): void
102
    {
103
        $this->initNeeded = false;
104
        $this->initialised = true;
105
106
        if (!($configurationReader instanceof AbstractConfigurationReader)) {
107
            $configurationReader = new DefaultConfigurationReader();
108
        }
109
110
        $this->config = $configurationReader->getArrayCopy();
111
    }
112
113
    /**
114
     * @return LinterConfiguration
115
     */
116
    public function getLinterConfiguration(): LinterConfiguration
117
    {
118
        $configurationProcessor = new Processor();
119
        $this->linterConfiguration = new LinterConfiguration();
120
        $linterConfig  = $configurationProcessor->processConfiguration($this->linterConfiguration, [$this->config]);
121
        $this->linterConfiguration->setConfiguration($linterConfig);
122
        return $this->linterConfiguration;
123
    }
124
125
126
    /**
127
     * @return string
128
     */
129
    public function oneLevelIndentationString(): string
130
    {
131
        $this->getLinterConfiguration();
132
        $sniffs = $this->linterConfiguration->getSniffConfigurations();
133
        $parameters = [];
134
135
        foreach ($sniffs as $sniff) {
136
            if ($sniff['class'] === IndentationSniff::class) {
137
                $parameters = $sniff['parameters'];
138
                break;
139
            }
140
        }
141
142
        $indentationCharacter = ' ';
143
        $useSpaces = $parameters['useSpaces'] ?? false;
144
        if ($useSpaces === false) {
145
            $indentationCharacter = "\t";
146
        }
147
        $indentPerLevel = $parameters['indentPerLevel'] ?? 2;
148
        return str_repeat($indentationCharacter, $indentPerLevel);
149
    }
150
}
151