Tschallacka
Tschallacka

Reputation: 28742

How preserve whitespace in code sample in phpdoc comment

I have a code sample like this:

$this->someConfig['settingname'] = [
                                    'model' => 'foo\bar\baz',
                                    'columns' => ['bla','blo','bleh'],
                                    'sortorder'=> 'asc',
                                    'defaultsort' => 'bla',
                                    ];

If I add it to the php doc it turns into this:

$this->someConfig['settingname'] = [ 'model' => 'foo\bar\baz', 'columns' => ['bla','blo','bleh'], 'sortorder'=> 'asc', 'defaultsort' => 'bla', ];

if I surround it by pre tags I get this

$this->someConfig['settingname'] = [
'model' => 'foo\bar\baz',
'columns' => ['bla','blo','bleh'],
'sortorder'=> 'asc',
'defaultsort' => 'bla',
];

which is closer but still not what I want.

I want the whitespacing formatting to stay preserved.

How do I achieve this?

Upvotes: 1

Views: 277

Answers (1)

Tschallacka
Tschallacka

Reputation: 28742

You can achieve this by by instead of using an asterisk in front of the commented code a &nbsp; character. Also wrap your code in <pre> tags to make sure newlines are preserved, or use <BR> tags after every line.

It will translate into a whitespace character, and all whitespace characters after the initial &nbsp; character will be included.

Using the   character will have the added benefit your sample code can be copied and pasted without any weird things showing up or having to be cleaned up

 /** 
 * My code sample
 * <pre>
 &nbsp; $this->someConfig['settingname'] = [
 &nbsp;                                   'model' => 'foo\bar\baz',
 &nbsp;                                   'columns' => ['bla','blo','bleh'],
 &nbsp;                                   'sortorder'=> 'asc',
 &nbsp;                                   'defaultsort' => 'bla',
 &nbsp;                                   ];
 * </pre>
 * bla bla bla
 */

enter image description here

Upvotes: 1

Related Questions