Escaping   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 0
dl 0
loc 92
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A extractParenthesis() 0 8 2
A escapeString() 0 4 1
A escapeInteger() 0 4 1
A escapeDouble() 0 4 1
A escapeNULL() 0 4 1
A escapeBoolean() 0 4 2
A escapeDateTime() 0 5 1
1
<?php
2
3
namespace ORM\Dbal;
4
5
use DateTime;
6
use DateTimeZone;
7
8
trait Escaping
9
{
10
    /** @var string */
11
    protected $quotingCharacter = '"';
12
    /** @var string */
13
    protected $identifierDivider = '.';
14
    /** @var string */
15
    protected $booleanTrue = '1';
16
    /** @var string */
17
    protected $booleanFalse = '0';
18
19
    /**
20
     * Extract content from parenthesis in $type
21
     *
22
     * @param string $type
23
     * @return string
24
     */
25
    protected function extractParenthesis($type)
26
    {
27
        if (preg_match('/\((.+)\)/', $type, $match)) {
28
            return $match[1];
29
        }
30
31
        return null;
32
    }
33
34
    /**
35
     * Escape a string for query
36
     *
37
     * @param string $value
38
     * @return string
39
     */
40
    protected function escapeString($value)
41
    {
42
        return $this->entityManager->getConnection()->quote($value);
0 ignored issues
show
Bug introduced by
The property entityManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
    }
44
45
    /**
46
     * Escape an integer for query
47
     *
48
     * @param int $value
49
     * @return string
50
     */
51
    protected function escapeInteger($value)
52
    {
53
        return (string) $value;
54
    }
55
56
    /**
57
     * Escape a double for Query
58
     *
59
     * @param double $value
60
     * @return string
61
     */
62
    protected function escapeDouble($value)
63
    {
64
        return (string) $value;
65
    }
66
67
    /**
68
     * Escape NULL for query
69
     *
70
     * @return string
71
     */
72
    protected function escapeNULL()
73
    {
74
        return 'NULL';
75
    }
76
77
    /**
78
     * Escape a boolean for query
79
     *
80
     * @param bool $value
81
     * @return string
82
     */
83
    protected function escapeBoolean($value)
84
    {
85
        return ($value) ? $this->booleanTrue : $this->booleanFalse;
86
    }
87
88
    /**
89
     * Escape a date time object for query
90
     *
91
     * @param DateTime $value
92
     * @return mixed
93
     */
94
    protected function escapeDateTime(DateTime $value)
95
    {
96
        $value->setTimezone(new DateTimeZone('UTC'));
97
        return $this->escapeString($value->format('Y-m-d\TH:i:s.u\Z'));
98
    }
99
}
100