Completed
Push — master ( 6e0d04...74be15 )
by Wanderson
02:31
created

Database   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 48
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
connect() 0 1 ?
A getPDO() 0 3 1
A instance() 0 3 1
A __construct() 0 9 1
A validateConnection() 0 7 2
1
<?php
2
3
namespace Win\Connection;
4
5
/**
6
 * Conexão com banco de dados
7
 *
8
 */
9
use PDO;
10
use Win\Mvc\Application;
11
use Win\Mvc\View;
12
13
abstract class Database {
14
15
	/** @var \static */
16
	static protected $instance;
17
18
	/** @var PDO */
19
	protected $pdo;
20
21
	/**
22
	 * Cria e retorna conexao PDO
23
	 * @param string[] $dbConfig
24
	 * @return PDO
25
	 */
26
	abstract function connect(&$dbConfig);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
28
	/** @return PDO */
29
	final public function getPDO() {
30
		return $this->pdo;
31
	}
32
33
	/** @return static */
34
	final static function instance() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
		return self::$instance;
36
	}
37
38
	/**
39
	 * Cria uma conexão com um banco de dados
40
	 * @param string[] $dbConfig
41
	 */
42
	public function __construct($dbConfig) {
43
		self::$instance = $this;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this of type this<Win\Connection\Database> is incompatible with the declared type object<static> of property $instance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
45
		$errorLevel = error_reporting(0);
46
		$this->pdo = $this->connect($dbConfig);
47
		error_reporting($errorLevel);
48
49
		$this->validateConnection();
50
	}
51
52
	protected function validateConnection() {
53
		if ($this->pdo->errorCode()) {
54
			$data['error'] = $this->pdo->errorInfo();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
55
			Application::app()->view = new View('500', $data);
56
			Application::app()->setTitle('Problemas de Conexão');
57
		}
58
	}
59
60
}
61