Completed
Push — master ( 598fe1...4a905b )
by Michael
03:41
created

SqlSubsTrait::getSqlSubs()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
ccs 0
cts 18
cp 0
rs 8.6737
cc 6
eloc 17
nc 2
nop 1
crap 42
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains trait SqlSubsTrait.
5
 *
6
 * PHP version 7.0+
7
 *
8
 * LICENSE:
9
 * This file is part of Yet Another Php Eve Api Library also know as Yapeal
10
 * which can be used to access the Eve Online API data and place it into a
11
 * database.
12
 * Copyright (C) 2016 Michael Cummings
13
 *
14
 * This program is free software: you can redistribute it and/or modify it
15
 * under the terms of the GNU Lesser General Public License as published by the
16
 * Free Software Foundation, either version 3 of the License, or (at your
17
 * option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful, but WITHOUT
20
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
22
 * for more details.
23
 *
24
 * You should have received a copy of the GNU Lesser General Public License
25
 * along with this program. If not, see
26
 * <http://spdx.org/licenses/LGPL-3.0.html>.
27
 *
28
 * You should be able to find a copy of this license in the COPYING-LESSER.md
29
 * file. A copy of the GNU GPL should also be available in the COPYING.md file.
30
 *
31
 * @author    Michael Cummings <[email protected]>
32
 * @copyright 2016 Michael Cummings
33
 * @license   LGPL-3.0+
34
 */
35
namespace Yapeal\Sql;
36
37
use Yapeal\Container\ContainerInterface;
38
39
/**
40
 * Trait SqlSubsTrait.
41
 */
42
trait SqlSubsTrait
43
{
44
    /**
45
     * Takes one or more SQL statements with comments and normalizes EOLs and also removes most comments.
46
     *
47
     * NOTE: Any comment 'code' like used by MySQL are also removed.
48
     *
49
     * @param string $sql
50
     * @param array  $replacements Expects associative array like from getSqlSubs().
51
     *
52
     * @return string
53
     */
54
    protected function getCleanedUpSql(string $sql, array $replacements): string
55
    {
56
        while (false !== strpos($sql, "\n  ")) {
57
            $sql = str_replace("\n  ", "\n ", $sql);
58
        }
59
        $replacements = array_reverse($replacements);
60
        $replacements["\n)"] = ')';
61
        $replacements["\n "] = ' ';
62
        $replacements["\r\n"] = "\n";
63
        $replacements = array_reverse($replacements);
64
        // Normalize line ends and change pretty multiple lines sql and comments into single line ones
65
        // and do replacements.
66
        $sql = str_replace(array_keys($replacements), array_values($replacements), $sql);
67
        /**
68
         * @var string[] $statements
69
         */
70
        $statements = explode("\n", $sql);
71
        // Filter out non-sql lines like comments and blank lines.
72
        $statements = array_filter($statements,
73
            function ($statement) {
74
                /** @noinspection IfReturnReturnSimplificationInspection */
75
                if (0 === strpos($statement, '-- ')
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !(0 === strpos($s...ev($statement), '/*'));.
Loading history...
76
                    || '' === trim($statement)
77
                    || (0 === strpos($statement, '/* ') && 0 === strpos(strrev($statement), '/*'))
78
                ) {
79
                    return false;
80
                }
81
                return true;
82
            });
83
        return implode("\n", $statements);
84
    }
85
    /**
86
     * Uses Sql section settings to make a filtered list of replacement pairs for SQL statements.
87
     *
88
     * @param ContainerInterface $dic
89
     *
90
     * @return array
91
     */
92
    protected function getSqlSubs(ContainerInterface $dic)
93
    {
94
        $keys = $dic->keys();
95
        $platform = '.' . $dic['Yapeal.Sql.platform'];
96
        $filteredKeys = array_filter($keys,
97
            function ($key) use ($platform) {
98
                $classes = ['Yapeal.Sql.CommonQueries', 'Yapeal.Sql.Connection', 'Yapeal.Sql.Creator'];
99
                $isPlatform = false !== strpos($key, $platform);
100
                $hasPlatforms = false !== strpos($key, 'Platforms.');
101
                $isHandlers = false !== strpos($key, 'Handlers.');
102
                if ($isHandlers || ($hasPlatforms && !$isPlatform)) {
103
                    return false;
104
                }
105
                return !(false === strpos($key, 'Yapeal.Sql.') || in_array($key, $classes, true));
106
            });
107
        $replacements = [];
108
        foreach ($filteredKeys as $key) {
109
            $subName = '{' . substr($key, strrpos($key, '.') + 1) . '}';
110
            $replacements[$subName] = $dic[$key];
111
        }
112
        return $replacements;
113
    }
114
}
115