Sniffer Snippet to create brackets on a new line

Is there any snippet code that allows / force {} to put newlines for each function / method?

Basically, forcing something like this:

if (TRUE)
{
     // Code logic
}
else
{
    // Code Logic
}

and

public function test()
{
     // Code logic
}
+5
source share
1 answer

Yes, there is a ready one. It is called OpeningFunctionBraceBsdAllmanSniff, and you can find it under /path/to/CodeSniffer/Standards/Generic/Sniffs/Functions. But this is only for function declarations.

For control structures, you can take /path/to/Standards/Squiz/Sniffs/ControlStructures/ControlSignatureSniff.phpand configure an array of templates from

protected function getPatterns()
{
    return array(
            'try {EOL...} catch (...) {EOL',
            'do {EOL...} while (...);EOL',
            'while (...) {EOL',
            'for (...) {EOL',
            'if (...) {EOL',
            'foreach (...) {EOL',
            '} else if (...) {EOL',
            '} elseif (...) {EOL',
            '} else {EOL',
           );

}//end getPatterns()

to, i.e.

protected function getPatterns()
{
    return array(
            'try {EOL...} catch (...) {EOL',
            'do {EOL...} while (...);EOL',
            'while (...) {EOL',
            'for (...) {EOL',
            'if (...)EOL{',              // that what you need
            'foreach (...) {EOL',
            '} else if (...) {EOL',
            '} elseif (...) {EOL',
            '} elseEOL{',               // and this
           );

}//end getPatterns()

If you need to apply the same rule to a different management structure, you can go the same way by changing the patterns in the array.

: , , , getPatterns().

+4

All Articles