Completed
Push — master ( b57571...eaf5ea )
by Michael
06:33
created

SqlSubsTrait::getCleanedUpSql()   C

Complexity

Conditions 7
Paths 2

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 19
cts 19
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 2
nop 2
crap 7
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 2
    protected function getCleanedUpSql(string $sql, array $replacements): string
55
    {
56
        // Remove multi-space indents.
57 2
        while (false !== strpos($sql, "\n  ")) {
58 1
            $sql = str_replace("\n  ", "\n ", $sql);
59
        }
60 2
        $replacements = array_reverse($replacements);
61 2
        $replacements["\n)"] = ')';
62 2
        $replacements["\n "] = ' ';
63 2
        $replacements["\r\n"] = "\n";
64 2
        $replacements = array_reverse($replacements);
65
        // Normalize line ends and change pretty multiple lines sql and comments into single line ones
66
        // and do replacements.
67 2
        $sql = str_replace(array_keys($replacements), array_values($replacements), $sql);
68
        /**
69
         * @var string[] $lines
70
         */
71 2
        $lines = explode("\n", $sql);
72
        // Filter out non-sql lines like comments and blank lines.
73 2
        $lines = array_filter($lines,
74
            function ($line) {
75 2
                $line = trim($line);
76 2
                $nonSql = '' === $line
77 2
                    || 0 === strpos($line, '--')
78 2
                    || (0 === strpos($line, '/* ') && 0 === strpos(strrev($line), '/*'))
79 2
                    || (0 === strpos($line, '/** ') && 0 === strpos(strrev($line), '/*'));
80 2
                return !$nonSql;
81 2
            });
82 2
        return implode("\n", $lines);
83
    }
84
    /**
85
     * Uses Sql section settings to make a filtered list of replacement pairs for SQL statements.
86
     *
87
     * @param ContainerInterface $dic
88
     *
89
     * @return array
90
     */
91 4
    protected function getSqlSubs(ContainerInterface $dic)
92
    {
93 4
        $keys = $dic->keys();
94 4
        $platform = '.' . $dic['Yapeal.Sql.platform'];
95
        /**
96
         * @var array $filteredKeys
97
         */
98 4
        $filteredKeys = array_filter($keys,
99 4
            function ($key) use ($platform) {
100 4
                if (0 !== strpos($key, 'Yapeal.Sql.')) {
101 3
                    return false;
102
                }
103 4
                $filtered = in_array($key, ['Yapeal.Sql.CommonQueries', 'Yapeal.Sql.Connection', 'Yapeal.Sql.Creator'], true)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
104 4
                    || false !== strpos($key, 'Handlers.')
105 4
                    || (false !== strpos($key, 'Platforms.') && false === strpos($key, $platform));
106 4
                return !$filtered;
107 4
            });
108 4
        $replacements = [];
109 4
        foreach ($filteredKeys as $key) {
110 4
            $subName = '{' . substr($key, strrpos($key, '.') + 1) . '}';
111 4
            $replacements[$subName] = $dic[$key];
112
        }
113 4
        return $replacements;
114
    }
115
}
116