PhpFormatter::format()   C
last analyzed

Complexity

Conditions 13
Paths 105

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 42
rs 6.575
cc 13
nc 105
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the SexyField package.
5
 *
6
 * (c) Dion Snoeijen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare (strict_types = 1);
13
14
namespace Tardigrades\SectionField\Generator;
15
16
class PhpFormatter
17
{
18
    public static function format(string $string): string
19
    {
20
        $tabs = 0;
21
        $startCounter = 0;
22
        $result = '';
23
        $use = true;
24
        foreach (preg_split("/((\r?\n)|(\r\n?))/", $string) as $line) {
25
            $line = str_replace('    ', '', $line);
26
            if (strpos($line, '}') !== false) {
27
                $tabs--;
28
            }
29
            $indent = '';
30
            for ($i = 0; $i < $tabs; $i++) {
31
                $indent .= '    ';
32
            }
33
            if ($line !== '') {
34
                if (substr($line, 0, 3) === '/**') {
35
                    if ($startCounter >= 1) {
36
                        $result .= PHP_EOL;
37
                    }
38
                    $startCounter++;
39
                }
40
41
                if (substr($line, 0, 3) === 'use' && $use) {
42
                    $result .= PHP_EOL;
43
                    $use = false;
44
                }
45
46
                if (substr($line, 0, 6) === 'public' ||
47
                    substr($line, 0, 9) === 'namespace' ||
48
                    substr($line, 0, 5) === 'class'
49
                ) {
50
                    $result .= PHP_EOL;
51
                }
52
                $result .= $indent . $line . PHP_EOL;
53
            }
54
            if (strpos($line, '{') !== false) {
55
                $tabs++;
56
            }
57
        }
58
59
        return $result;
60
    }
61
}
62