Passed
Push — master ( 5545f1...83deac )
by Kirill
03:22
created

Comment   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 14 3
A replace() 0 9 1
A prepareLine() 0 8 2
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Reactor\Partial;
13
14
use Spiral\Reactor\ReplaceableInterface;
15
16
/**
17
 * Wraps docBlock comment (by representing it as string lines).
18
 */
19
class Comment extends Source implements ReplaceableInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     * @return self
24
     */
25
    public function replace($search, $replace): Comment
26
    {
27
        $lines = $this->getLines();
28
29
        array_walk($lines, static function (&$line) use ($search, $replace): void {
30
            $line = str_replace($search, $replace, $line);
31
        });
32
33
        return $this->setLines($lines);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function render(int $indentLevel = 0): string
40
    {
41
        if ($this->isEmpty()) {
42
            return '';
43
        }
44
45
        $result = $this->addIndent("/**\n", $indentLevel);
46
        foreach ($this->getLines() as $line) {
47
            $result .= $this->addIndent(" * {$line}\n", $indentLevel);
48
        }
49
50
        $result .= $this->addIndent(' */', $indentLevel);
51
52
        return $result;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function prepareLine(string $line): ?string
59
    {
60
        $line = trim($line);
61
        if (in_array($line, ['/*', '/**', '*/'], true)) {
62
            return null;
63
        }
64
65
        return parent::prepareLine(preg_replace('/^(\s)*(\*)+\s?/', '', $line));
66
    }
67
}
68