Test Setup Failed
Push — master ( 7a093e...068245 )
by Nick
05:17
created

DsnParser::getPort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Meanbee\LibMageConf\Util;
4
5
class DsnParser
6
{
7
    /**
8
     * @param $dsnString
9
     */
10
    public function __construct($dsnString)
11
    {
12
        $this->dsn = $dsnString;
0 ignored issues
show
Bug introduced by
The property dsn does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
13
    }
14
15
    /**
16
     * @return string
17
     */
18
    public function getPort()
19
    {
20
        return $this->getDsnParts()['port'];
21
    }
22
23
    /**
24
     * @return string
25
     */
26
    public function getHost()
27
    {
28
        return $this->getDsnParts()['host'];
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    protected function getDsnParts()
35
    {
36
        $colonPosition = strrpos($this->dsn, ':');
37
38
        if ($colonPosition === false) {
39
            $host = $this->dsn;
40
            $port = '';
41
        } else {
42
            $host = substr($this->dsn, 0, $colonPosition);
43
            $port = substr($this->dsn, $colonPosition + 1);
44
        }
45
46
        return [
47
            'host' => $host,
48
            'port' => $port
49
        ];
50
    }
51
}