Passed
Push — master ( 9b7912...982aac )
by Janis
02:15
created

AbstractFeatureContext::normalizeTableNode()   B

Complexity

Conditions 9
Paths 18

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 31
nc 18
nop 1
dl 0
loc 55
rs 8.0555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Tests\Behat\Bootstrap;
4
5
use Behat\Behat\Context\Context;
6
use Behat\Gherkin\Node\TableNode;
7
use Janisbiz\LightOrm\ConnectionPool;
8
use Symfony\Component\Yaml\Parser;
9
10
abstract class AbstractFeatureContext implements Context
11
{
12
    const CONFIG_FILE_NAME = 'light-orm.yaml';
13
14
    /**
15
     * @var array
16
     */
17
    private $config;
18
19
    /**
20
     * @var ConnectionPool
21
     */
22
    protected $connectionPool;
23
24
    /**
25
     * @var string
26
     */
27
    protected $rootDir;
28
29
    public function __construct()
30
    {
31
        $this->connectionPool = new ConnectionPool();
32
        $this->rootDir = \implode(
33
            '',
34
            [
35
                __DIR__,
36
                DIRECTORY_SEPARATOR,
37
                '..',
38
                DIRECTORY_SEPARATOR,
39
                '..',
40
                DIRECTORY_SEPARATOR,
41
                '..',
42
            ]
43
        );
44
    }
45
46
    /**
47
     * @param null|string $connectionName
48
     *
49
     * @return array
50
     * @throws \Exception
51
     */
52
    protected function getConnectionConfig(?string $connectionName = null): array
53
    {
54
        if (null !== $connectionName) {
55
            if (!isset($this->getConfig()['connections'][$connectionName])) {
56
                throw new \Exception(\sprintf('Could not get connection "%s"', $connectionName));
57
            }
58
59
            return $this->getConfig()['connections'][$connectionName];
60
        }
61
62
        return $this->getConfig()['connections'];
63
    }
64
65
    /**
66
     * @param string $connectionName
67
     *
68
     * @return array
69
     * @throws \Exception
70
     */
71
    protected function getWritersConfig(string $connectionName): array
72
    {
73
        if (!isset($this->getConfig()['generator'][$connectionName]['writers'])) {
74
            throw new \Exception(\sprintf('Could not get writer config for connection "%s"', $connectionName));
75
        }
76
77
        return $this->getConfig()['generator'][$connectionName]['writers'];
78
    }
79
80
    /**
81
     * @param TableNode $tableNode
82
     *
83
     * @return TableNode
84
     */
85
    protected function normalizeTableNode(TableNode $tableNode): TableNode
86
    {
87
        $tableNodeParsedArray = [
88
            0 => \array_map(
89
                function (string $column): string {
90
                    return \explode(':', $column)[0];
91
                },
92
                $tableNode->getRow(0)
93
            ),
94
        ];
95
96
        foreach ($tableNode as $rowIndex => $nodeRow) {
97
            $nodeRowParsed = [];
98
            foreach ($nodeRow as $cellNameWithDataType => $cellValue) {
99
                $cellNameWithDataType = \explode(':', $cellNameWithDataType);
100
                $cellDataType = $cellNameWithDataType[1] ?? false;
101
102
                if (false !== $cellDataType) {
103
                    if (0 === \strpos($cellDataType, '?')) {
104
                        if ('' === $cellValue) {
105
                            $cellValue = null;
106
                        } else {
107
                            $cellDataType = \ltrim($cellDataType, '?');
108
                            \settype($cellValue, $cellDataType);
109
                        }
110
                    } else {
111
                        \settype($cellValue, $cellDataType);
112
                    }
113
                }
114
115
                $nodeRowParsed[] = $cellValue;
116
            }
117
118
            $tableNodeParsedArray[$rowIndex + 1] = $nodeRowParsed;
119
        }
120
121
        /**
122
         * Handling node validation. This is known exception, as it checks for type of string in cell values. If it is
123
         * not string, exception is thrown, but rest validation is valid.
124
         */
125
        try {
126
            new TableNode($tableNodeParsedArray);
127
        } catch (\Throwable $e) {
128
            if (70 !== $e->getLine() || 'Table is not two-dimensional.' !== $e->getMessage()) {
129
                throw $e;
130
            }
131
        }
132
133
        $tableNodeParsed = new TableNode([]);
134
135
        $tableProperty = new \ReflectionProperty($tableNodeParsed, 'table');
136
        $tableProperty->setAccessible(true);
137
        $tableProperty->setValue($tableNodeParsed, $tableNodeParsedArray);
138
139
        return $tableNodeParsed;
140
    }
141
142
    /**
143
     * @return array
144
     */
145
    private function getConfig(): array
146
    {
147
        if (null === $this->config) {
148
            $this->config = (new Parser())
149
                ->parseFile(\implode(
150
                    '',
151
                    [
152
                        JANISBIZ_LIGHT_ORM_BEHAT_CONFIG_DIR,
153
                        static::CONFIG_FILE_NAME,
154
                    ]
155
                ))['light-orm']
156
            ;
157
158
            foreach ($this->config['generator'] as &$connectionConfigs) {
159
                foreach ($connectionConfigs['writers'] as &$writerConfig) {
160
                    $writerConfig['directory'] = \preg_replace(
161
                        '/\/\\\/',
162
                        DIRECTORY_SEPARATOR,
163
                        $writerConfig['directory']
164
                    );
165
                }
166
            }
167
        }
168
169
        return $this->config;
170
    }
171
}
172