database::update()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 3
1
<?php
2
3
namespace florinp\messenger\libs;
4
5
use PDO;
6
7
class database extends \PDO
8
{
9
	protected $extension_manager;
10
11
	public function __construct()
12
	{
13
		global $phpbb_root_path;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
14
15
		$database = $phpbb_root_path.'store/messenger.db';
16
		parent::__construct('sqlite:'.$database);
17
		parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setAttribute() instead of __construct()). Are you sure this is correct? If so, you might want to change this to $this->setAttribute().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
18
	}
19
20
	/**
21
	 * @param string $sql
22
	 * @return array
23
	 */
24
	public function select($sql, $array = array(), $fetchMode = PDO::FETCH_ASSOC)
25
	{
26
		$sth = $this->prepare($sql);
27
		foreach ($array as $key => $value) {
28
			$sth->bindValue("$key", $value);
29
		}
30
31
		$sth->execute();
32
		return $sth->fetchAll($fetchMode);
33
	}
34
35
	/**
36
	 * @param string $table
37
	 * @return bool
38
	 */
39
	public function insert($table, $data)
40
	{
41
		//ksort($data);
42
43
		$fieldNames = implode('`, `', array_keys($data));
44
		$fieldValues = ':'.implode(', :', array_keys($data));
45
46
		$sth = $this->prepare("INSERT INTO $table (`$fieldNames`) VALUES ($fieldValues)");
47
48
		foreach ($data as $key => $value) {
49
			$sth->bindValue(":$key", $value);
50
		}
51
52
		return $sth->execute();
53
	}
54
55
	/**
56
	 * @param string $table
57
	 * @param array $data
58
	 * @param string $where
59
	 * @return bool
60
	 */
61
	public function update($table, $data, $where)
62
	{
63
		ksort($data);
64
65
		$fieldDetails = NULL;
66
		foreach ($data as $key => $value) {
67
			$fieldDetails .= "`$key`=:$key,";
68
		}
69
		$fieldDetails = rtrim($fieldDetails, ',');
70
71
		$sth = $this->prepare("UPDATE $table SET $fieldDetails WHERE $where");
72
73
		foreach ($data as $key => $value) {
74
			$sth->bindValue(":$key", $value);
75
		}
76
77
		return $sth->execute();
78
	}
79
80
	/**
81
	 * @param string $table
82
	 * @param string $where
83
	 * @param int $limit
84
	 * @return int
85
	 */
86
	public function delete($table, $where, $limit = 1)
87
	{
88
		return $this->exec("DELETE FROM $table WHERE $where LIMIT $limit");
89
	}
90
91
	protected function find($path, $prefix, $suffix)
92
	{
93
		$finder = $this->extension_manager->get_finder();
94
95
		return $finder
96
			->set_extensions(array('phpbb/messenger'))
97
			->prefix($prefix)
98
			->suffix($suffix)
99
			->core_path("$path/")
100
			->extension_directory("/$path")
101
			->find();
102
	}
103
}
104