ActiveRecord   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 32
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A prepare() 0 3 1
A getConn() 0 3 1
A conn() 0 4 1
A exec() 0 3 1
A execute() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * activerecord.php é uma biblioteca PHP que permite ao desenvolvedor
7
 * trabalhar com banco de dados usando o padrão de projeto Active Record
8
 * (https://en.wikipedia.org/wiki/Active_record_pattern)
9
 */
10
11
namespace Bonfim\ActiveRecord;
12
13
use PDO;
14
use PDOStatement;
15
16
class ActiveRecord
17
{
18
    private static $dbh;
19
    private static $sth;
20
21 30
    public static function conn(string $dsn, string $user, string $pass)
22
    {
23 30
        ActiveRecord::$dbh = new PDO($dsn, $user, $pass);
24 30
        ActiveRecord::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
25 30
    }
26
27 30
    private static function getConn(): PDO
28
    {
29 30
        return ActiveRecord::$dbh;
30
    }
31
32 30
    private static function prepare(string $query): PDOStatement
33
    {
34 30
        return ActiveRecord::$sth = ActiveRecord::getConn()->prepare(trim($query));
35
    }
36
37 30
    protected static function execute(string $query, array $parameters = []): ?PDOStatement
38
    {
39 30
        if (ActiveRecord::prepare($query)->execute($parameters)) {
40 30
            return ActiveRecord::$sth;
41
        }
42
        return null;
43
    }
44
45 30
    public static function exec(string $sql): int
46
    {
47 30
        return ActiveRecord::$dbh->exec($sql);
48
    }
49
}
50