MySQLite   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
c 2
b 0
f 0
dl 0
loc 72
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerMethod() 0 14 4
A getPublicMethodData() 0 15 3
A createFunctions() 0 11 3
1
<?php
2
3
namespace Mhorninger\MySQLite;
4
5
use PDO;
6
use ReflectionClass;
7
use ReflectionMethod;
8
use Mhorninger\MySQLite\MySQL\Miscellaneous;
9
use Mhorninger\MySQLite\MySQL\StringExtended;
10
use Mhorninger\MySQLite\MySQL\NumericExtended;
11
use Mhorninger\MySQLite\MySQL\DateTimeExtended;
12
13
/**
14
 * MySQLite is the extension Vectorface's MySQLite extension.
15
 * @see \Vectorface\MySQLite\MySQLite
16
 */
17
class MySQLite extends \Vectorface\MySQLite\MySQLite
18
{
19
    use DateTimeExtended;
0 ignored issues
show
introduced by
The trait Mhorninger\MySQLite\MySQL\DateTimeExtended requires some properties which are not provided by Mhorninger\MySQLite\MySQLite: $d, $h
Loading history...
20
    use Miscellaneous;
21
    use NumericExtended;
22
    use StringExtended;
23
24
    /**
25
     * Get information about functions that are meant to be exposed by this class.
26
     *
27
     * @return int[] An associative array composed of function names mapping to accepted parameter counts.
28
     */
29
    protected static function getPublicMethodData()
30
    {
31
        $data = [];
32
33
        $ref = new ReflectionClass(__CLASS__);
34
        $methods = $ref->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_STATIC);
35
        foreach ($methods as $method) {
36
            if (strpos($method->name, 'mysql_') !== 0) {
37
                continue;
38
            }
39
40
            $data[$method->name] = $method->getNumberOfRequiredParameters();
41
        }
42
43
        return $data;
44
    }
45
46
    /**
47
     * Add MySQLite compatibility functions to a PDO object.
48
     *
49
     * @param \PDO $pdo    A PDO instance to which the MySQLite compatibility functions should be added.
50
     * @param string[] $fnList A list of functions to create on the SQLite database. (Omit to create all.)
51
     * @return \PDO Returns a reference to the PDO instance passed in to the function.
52
     */
53
    public static function &createFunctions(\PDO &$pdo, array $fnList = null)
54
    {
55
        if ($pdo->getAttribute(PDO::ATTR_DRIVER_NAME) !== 'sqlite') {
56
            throw new InvalidArgumentException('Expecting a PDO instance using the SQLite driver');
0 ignored issues
show
Bug introduced by
The type Mhorninger\MySQLite\InvalidArgumentException was not found. Did you mean InvalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
57
        }
58
59
        foreach (static::getPublicMethodData() as $method => $paramCount) {
60
            static::registerMethod($pdo, $method, $paramCount, $fnList);
61
        }
62
63
        return $pdo;
64
    }
65
66
    /**
67
     * Register a method as an SQLite funtion.
68
     *
69
     * @param PDO $pdo        A PDO instance to which the MySQLite compatibility functions should be added.
70
     * @param string $method     The internal method name.
71
     * @param int $paramCount The suggested parameter count.
72
     * @param string[] $fnList     A list of functions to create on the SQLite database, or empty for all.
73
     * @return bool Returns true if the method was registed. False otherwise.
74
     */
75
    protected static function registerMethod(\PDO &$pdo, $method, $paramCount, array $fnList = null)
76
    {
77
        $function = substr($method, 6); /* Strip 'mysql_' prefix to get the function name. */
78
79
        /* Skip functions not in the list. */
80
        if (! empty($fnList) && ! in_array($function, $fnList)) {
81
            return false;
82
        }
83
84
        if ($paramCount) {
85
            return $pdo->sqliteCreateFunction($function, [__CLASS__, $method], $paramCount);
86
        }
87
88
        return $pdo->sqliteCreateFunction($function, [__CLASS__, $method]);
89
    }
90
}
91