These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Database |
||
4 | * |
||
5 | * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
||
6 | * @license http://opensource.org/licenses/MIT MIT License |
||
7 | */ |
||
8 | |||
9 | namespace Pimf\Pdo; |
||
10 | |||
11 | /** |
||
12 | * Connection management to PostgreSQL |
||
13 | * |
||
14 | * @package Database |
||
15 | * @author Gjero Krsteski <[email protected]> |
||
16 | */ |
||
17 | class Postgre extends Connector |
||
18 | { |
||
19 | protected $options = array( |
||
20 | \PDO::ATTR_CASE => \PDO::CASE_LOWER, |
||
21 | \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, |
||
22 | \PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL, |
||
23 | \PDO::ATTR_STRINGIFY_FETCHES => false, |
||
24 | ); |
||
25 | |||
26 | /** |
||
27 | * @param array $config |
||
28 | * |
||
29 | * @return \Pimf\Database |
||
30 | */ |
||
31 | View Code Duplication | public function connect(array $config) |
|
0 ignored issues
–
show
|
|||
32 | { |
||
33 | $dsn = "pgsql:host={$config['host']};dbname={$config['database']}"; |
||
34 | |||
35 | if (isset($config['port'])) { |
||
36 | $dsn .= ";port={$config['port']}"; |
||
37 | } |
||
38 | |||
39 | $connection = new \Pimf\Database($dsn, $config['username'], $config['password'], $this->options($config)); |
||
40 | |||
41 | // set to UTF-8 which should be fine for most scenarios. |
||
42 | if (isset($config['charset'])) { |
||
43 | $connection->prepare("SET NAMES '{$config['charset']}'")->execute(); |
||
44 | } |
||
45 | |||
46 | // If a schema has been specified |
||
47 | if (isset($config['schema'])) { |
||
48 | $connection->prepare("SET search_path TO '{$config['schema']}'")->execute(); |
||
49 | } |
||
50 | |||
51 | return $connection; |
||
52 | } |
||
53 | } |
||
54 |
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.