|
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 |
|
|
|
|
|
|
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
|
|
|
|
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.