Generator::generate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XL2TP;
6
7
use RuntimeException;
8
use XL2TP\Interfaces\ConfigInterface;
9
use XL2TP\Interfaces\GeneratorInterface;
10
11
class Generator implements GeneratorInterface
12
{
13
    /**
14
     * @var \XL2TP\Interfaces\ConfigInterface
15
     */
16
    public $config;
17
18
    /**
19
     * Generator constructor.
20
     *
21
     * @param \XL2TP\Interfaces\ConfigInterface $config
22
     */
23 4
    public function __construct(ConfigInterface $config)
24
    {
25 4
        $this->config = $config;
26 4
    }
27
28
    /**
29
     * Render section by parameters in array
30
     *
31
     * @param \XL2TP\Section $section Name of section: global, default, lac, lnc
32
     *
33
     * @return string
34
     */
35 2
    private function render(Section $section): string
36
    {
37 2
        $name = $section->section;
38 2
        if ('global' !== $section->section) {
39 2
            $name .= $section->suffix ? ' ' . $section->suffix : '';
40
        }
41
42 2
        $result = "[$name]" . PHP_EOL;
43 2
        foreach ($section->parameters as $key => $val) {
44 2
            $result .= "$key = " . (is_numeric($val) ? $val : '"' . $val . '"') . PHP_EOL;
45
        }
46
47 2
        return $result . PHP_EOL;
48
    }
49
50
    /**
51
     * Generate L2TP configuration by parameters from memory
52
     *
53
     * @return string
54
     * @throws \RuntimeException If was set bad config
55
     */
56 3
    public function generate(): string
57
    {
58 3
        if (!$this->config instanceof ConfigInterface) {
59 1
            throw new RuntimeException('Provided config is invalid');
60
        }
61
62 2
        $result = '';
63 2
        foreach ($this->config->sections as $section) {
0 ignored issues
show
Bug introduced by
Accessing sections on the interface XL2TP\Interfaces\ConfigInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
64 2
            $result .= $this->render($section);
65
        }
66
67 2
        return $result;
68
    }
69
}
70