Passed
Branch master (fc5382)
by Fabian
03:13
created

GetCertificate   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
eloc 18
dl 0
loc 48
ccs 0
cts 19
cp 0
rs 10
c 4
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCertificate() 0 8 2
A getIntermediate() 0 14 3
A getAlternativeLinks() 0 14 3
1
<?php
2
3
namespace LE_ACME2\Response\Order;
4
5
use LE_ACME2\Response\AbstractResponse;
6
7
class GetCertificate extends AbstractResponse {
8
9
    protected $_pattern = '~(-----BEGIN\sCERTIFICATE-----[\s\S]+?-----END\sCERTIFICATE-----)~i';
10
11
12
    public function getCertificate() : string {
13
14
        if(preg_match_all($this->_pattern, $this->_raw->body, $matches))  {
15
16
            return $matches[0][0];
17
        }
18
19
        throw new \RuntimeException('Preg_match_all has returned false - invalid pattern?');
20
    }
21
22
    public function getIntermediate() : string {
23
24
        if(preg_match_all($this->_pattern, $this->_raw->body, $matches))  {
25
26
            $result = '';
27
28
            for($i=1; $i<count($matches[0]); $i++)  {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
29
30
                $result .= "\n" . $matches[0][$i];
31
            }
32
            return $result;
33
        }
34
35
        throw new \RuntimeException('Preg_match_all has returned false - invalid pattern?');
36
    }
37
38
    /**
39
     * @return string[]
40
     */
41
    public function getAlternativeLinks() : array {
42
43
        $result = [];
44
45
        foreach($this->_raw->header as $line) {
46
            $matches = [];
47
            preg_match_all('/^link: <(.*)>;rel="alternate"$/', $line, $matches);
48
49
            if(isset($matches[1][0])) {
50
                $result[] = $matches[1][0];
51
            }
52
        }
53
54
        return $result;
55
    }
56
}