PHPSQLParserUtils::revokeEscaping()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 5
nc 2
nop 1
1
<?php
2
3
/**
4
 * parser-utils.php.
5
 *
6
 * These are utility functions for the PHPSQLParser.
7
 *
8
 * Copyright (c) 2010-2012, Justin Swanhart
9
 * with contributions by André Rothe <[email protected], [email protected]>
10
 *
11
 * All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without modification,
14
 * are permitted provided that the following conditions are met:
15
 *
16
 *   * Redistributions of source code must retain the above copyright notice,
17
 *     this list of conditions and the following disclaimer.
18
 *   * Redistributions in binary form must reproduce the above copyright notice,
19
 *     this list of conditions and the following disclaimer in the documentation
20
 *     and/or other materials provided with the distribution.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31
 * DAMAGE.
32
 */
33
namespace SQLParser;
34
35
/**
36
 * This class implements some helper functions.
37
 *
38
 * @author arothe
39
 */
40
class PHPSQLParserUtils extends PHPSQLParserConstants
41
{
42
    /**
43
     * Prints an array only if debug mode is on.
44
     *
45
     * @param array $s
0 ignored issues
show
Bug introduced by
There is no parameter named $s. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
46
     * @param bool  $return, if true, the formatted array is returned via return parameter
0 ignored issues
show
Documentation introduced by
There is no parameter named $return,. Did you maybe mean $return?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
47
     */
48
    protected function preprint($arr, $return = false)
0 ignored issues
show
Coding Style introduced by
preprint uses the super-global variable $_ENV which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
49
    {
50
        $x = '<pre>';
51
        $x .= print_r($arr, 1);
52
        $x .= '</pre>';
53
        if ($return) {
54
            return $x;
55
        } else {
56
            if (isset($_ENV['DEBUG'])) {
57
                print $x."\n";
58
            }
59
        }
60
    }
61
62
    /**
63
     * Ends the given string $haystack with the string $needle?
64
     *
65
     * @param string $haystack
66
     * @param string $needle
67
     */
68 View Code Duplication
    protected function endsWith($haystack, $needle)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        $length = strlen($needle);
71
        if ($length == 0) {
72
            return true;
73
        }
74
75
        $start = $length * -1;
76
77
        return (substr($haystack, $start) === $needle);
78
    }
79
80
    /**
81
     * Revokes the escaping characters from an expression.
82
     */
83
    protected function revokeEscaping($sql)
84
    {
85
        $result = trim($sql);
86
        if (($result[0] === '`') && ($result[strlen($result) - 1] === '`')) {
87
            $result = substr($result, 1, -1);
88
        }
89
90
        return str_replace('``', '`', $result);
91
    }
92
93
    /**
94
     * This method removes parenthesis from start of the given string.
95
     * It removes also the associated closing parenthesis.
96
     */
97
    protected function removeParenthesisFromStart($token)
98
    {
99
        $parenthesisRemoved = 0;
100
101
        $trim = trim($token);
102
        if ($trim !== '' && $trim[0] === '(') { // remove only one parenthesis pair now!
103
            ++$parenthesisRemoved;
104
            $trim[0] = ' ';
105
            $trim = trim($trim);
106
        }
107
108
        $parenthesis = $parenthesisRemoved;
109
        $i = 0;
110
        $string = 0;
111
        while ($i < strlen($trim)) {
112
            if ($trim[$i] === '\\') {
113
                $i += 2; # an escape character, the next character is irrelevant
114
                continue;
115
            }
116
117
            if ($trim[$i] === "'" || $trim[$i] === '"') {
118
                ++$string;
119
            }
120
121
            if (($string % 2 === 0) && ($trim[$i] === '(')) {
122
                ++$parenthesis;
123
            }
124
125
            if (($string % 2 === 0) && ($trim[$i] === ')')) {
126
                if ($parenthesis == $parenthesisRemoved) {
127
                    $trim[$i] = ' ';
128
                    --$parenthesisRemoved;
129
                }
130
                --$parenthesis;
131
            }
132
            ++$i;
133
        }
134
135
        return trim($trim);
136
    }
137
138
    public function getLastOf($array)
139
    {
140
        // $array is a copy of the original array, so we can change it without sideeffects
141
        if (!is_array($array)) {
142
            return false;
143
        }
144
145
        return array_pop($array);
146
    }
147
148
    /**
149
     * translates an array of objects into an associative array.
150
     */
151
    public function toArray($tokenList)
152
    {
153
        $expr = array();
154
        foreach ($tokenList as $token) {
155
            $expr[] = $token->toArray();
156
        }
157
158
        return (empty($expr) ? false : $expr);
159
    }
160
}
161