Test Failed
Push — master ( 2e358d...6afeb6 )
by
unknown
04:36 queued 02:44
created

AdapterPool   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 39
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 5 1
A remove() 0 5 1
A get() 0 4 1
1
<?php
2
3
namespace Silk\Database;
4
5
use Zend\Db\Adapter\Adapter;
6
use Zend\Db\TableGateway\TableGateway;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Silk\Database\TableGateway.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
8
/**
9
 * AdapterPool
10
 *
11
 * @author  Lucas A. de Araújo <[email protected]>
12
 * @package Silk\Database
13
 */
14
class AdapterPool
15
{
16
    /**
17
     * Pool of adapters
18
     *
19
     * @var array
20
     */
21
    protected static $pool = [];
22
23
    /**
24
     * @param $key
25
     * @param Adapter $adapter
26
     * @return $this
27
     */
28
    public function add($key, Adapter $adapter)
29
    {
30
        self::$pool[$key] = $adapter;
31
        return $this;
32
    }
33
34
    /**
35
     * @param $key
36
     * @return $this
37
     */
38
    public function remove($key)
39
    {
40
        unset(self::$pool[$key]);
41
        return $this;
42
    }
43
44
    /**
45
     * @param $key
46
     * @return Adapter
47
     */
48
    public function get($key)
49
    {
50
        return self::$pool[$key];
51
    }
52
}
53