Completed
Push — master ( 03a881...28de70 )
by Damian
09:16
created

ClassContentRemover::remove_class_content()   C

Complexity

Conditions 15
Paths 51

Size

Total Lines 64
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 33
nc 51
nop 2
dl 0
loc 64
rs 6.0057
c 0
b 0
f 0

How to fix   Long Method    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
namespace SilverStripe\Core\Manifest;
4
5
/**
6
 * Class ClassContentRemover
7
 * @package SilverStripe\Core\Manifest
8
 *
9
 * A utility class to clean the contents of a PHP file containing classes.
10
 *
11
 * It removes any code with in `$cut_off_depth` number of curly braces.
12
 */
13
class ClassContentRemover
14
{
15
16
    /**
17
     * @param string $filePath
18
     * @param int $cutOffDepth The number of levels of curly braces to go before ignoring the content
19
     *
20
     * @return string
21
     */
22
    public static function remove_class_content($filePath, $cutOffDepth = 1)
23
    {
24
25
        // Use PHP's built in method to strip comments and whitespace
26
        $contents = php_strip_whitespace($filePath);
27
28
        if (!trim($contents)) {
29
            return $contents;
30
        }
31
32
        if (!preg_match('/\b(?:class|interface|trait)/i', $contents)) {
33
            return '';
34
        }
35
36
        // tokenize the file contents
37
        $tokens = token_get_all($contents);
38
        $cleanContents = '';
39
        $depth = 0;
40
        $startCounting = false;
41
        // iterate over all the tokens and only store the tokens that are outside $cutOffDepth
42
        foreach ($tokens as $token) {
43
            // only store the string literal of the token, that's all we need
44
            if (!is_array($token)) {
45
                $token = [
46
                    T_STRING,
47
                    $token,
48
                    null
49
                ];
50
            }
51
52
            // only count if we see a class/interface/trait keyword
53
            if (!$startCounting && in_array($token[0], [T_CLASS, T_INTERFACE, T_TRAIT])) {
54
                $startCounting = true;
55
            }
56
57
            // use curly braces as a sign of depth
58
            if ($token[1] === '{') {
59
                if ($depth < $cutOffDepth) {
60
                    $cleanContents .= $token[1];
61
                }
62
                if ($startCounting) {
63
                    ++$depth;
64
                }
65
            } elseif ($token[1] === '}') {
66
                if ($startCounting) {
67
                    --$depth;
68
69
                    // stop counting if we've just come out of the
70
                    // class/interface/trait declaration
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
                    if ($depth <= 0) {
72
                        $startCounting = false;
73
                    }
74
                }
75
                if ($depth < $cutOffDepth) {
76
                    $cleanContents .= $token[1];
77
                }
78
            } elseif ($depth < $cutOffDepth) {
79
                $cleanContents .= $token[1];
80
            }
81
        }
82
83
        // return cleaned class
84
        return trim($cleanContents);
85
    }
86
}
87