Completed
Push — master ( c1a156...8cadde )
by Kirill
08:14
created

DescriptionProvider::trim()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Ast\Support;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Railt\SDL\Ast\Value\StringValueNode;
14
15
/**
16
 * Trait DescriptionProvider
17
 */
18
trait DescriptionProvider
19
{
20
    /**
21
     * @return null|string
22
     */
23 119
    public function getDescription(): ?string
24
    {
25 119
        $description = $this->first('Description', 1);
0 ignored issues
show
Bug introduced by
It seems like first() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
26
27 119
        if ($description instanceof RuleInterface) {
28
            /** @var StringValueNode $value */
29 16
            $value = $description->getChild(0);
30
31 16
            return $this->trim($value->toPrimitive());
32
        }
33
34 108
        return null;
35
    }
36
37
    /**
38
     * @param string $content
39
     * @return string
40
     */
41
    private function trim(string $content): string
42
    {
43 16
        $lines = \array_map(function(string $line): string {
44 16
            return \ltrim($line, '#');
45 16
        }, \explode("\n", $content));
46
47 16
        return \trim(\implode("\n", $lines), "\n\r");
48
    }
49
}
50