Test Setup Failed
Push — test ( e445a1...b7242e )
by Jonathan
03:10
created

getDocstringWithoutComments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Kint\Object\Representation;
4
5
class DocstringRepresentation extends Representation
6
{
7
    public $file = null;
8
    public $line = null;
9
    public $class = null;
10
    public $hints = array('docstring');
11
12
    public function __construct($docstring, $file, $line, $class = null)
13
    {
14
        parent::__construct('Docstring');
15
16
        $this->file = $file;
17
        $this->line = $line;
18
        $this->class = $class;
19
        $this->contents = $docstring;
20
    }
21
22
    /**
23
     * Returns the representation's docstring without surrounding comments.
24
     *
25
     * Note that this will not work flawlessly.
26
     *
27
     * On comments with whitespace after the stars the lines will begin with
28
     * whitespace, since we can't accurately guess how much of an indentation
29
     * is required.
30
     *
31
     * And on lines without stars on the left this may eat bullet points.
32
     *
33
     * Long story short: If you want the docstring read the contents. If you
34
     * absolutely must have it without comments (ie renderValueShort) this will
35
     * probably do.
36
     *
37
     * @return string|null Docstring with comments stripped
38
     */
39
    public function getDocstringWithoutComments()
40
    {
41
        if (!$this->contents) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->contents of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42
            return null;
43
        }
44
45
        $string = substr($this->contents, 3, -2);
46
        $string = preg_replace('/^\s*\*\s*?(\S|$)/m', '\1', $string);
47
48
        return trim($string);
49
    }
50
}
51