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

ClassContentRemover   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 0
dl 0
loc 74
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C remove_class_content() 0 64 15
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