Completed
Push — master ( 8d3e79...9daf84 )
by Edson
04:12
created

IncludeTpl   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A include() 0 7 3
A __toString() 0 3 1
1
<?php
2
3
namespace Bonfim\Tpl;
4
5
class IncludeTpl
6
{
7
    private $content;
8
    private $config;
9
    private $pattern = '/{\s?include \'?"?(.*?)"?\'?\s?}/is';
10
11 4
    public function __construct(string $content, array $config)
12
    {
13 4
        $this->config  = $config;
14 4
        $this->content = $content;
15
16 4
        $this->include();
17 4
    }
18
19 4
    public function __toString(): string
20
    {
21 4
        return $this->content;
22
    }
23
24 4
    public function include()
25
    {
26 4
        if (preg_match_all($this->pattern, $this->content, $matches, PREG_SET_ORDER)) {
27 2
            for ($i = 0; $i < count($matches); $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...
28 2
                $match = $matches[$i];
29 2
                $replace = file_get_contents("{$this->config['template_dir']}/{$match[1]}.html");
30 2
                $this->content = str_replace($matches[$i][0], $replace, $this->content);
31
            };
32
        }
33 4
    }
34
}
35