Completed
Push — master ( 7c306d...d9f77c )
by Yannick
24:48
created

get_all_php_files()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 6
nop 1
dl 0
loc 25
rs 6.7272
c 0
b 0
f 0
1
<?php /* For licensing terms, see /license.txt */
2
/**
3
 * Cron script to list unused, but defined, language variables.
4
 *
5
 * @package chamilo.cron.lang
6
 */
7
/**
8
 * Includes and declarations.
9
 */
10
die();
11
require_once __DIR__.'/../../inc/global.inc.php';
12
$path = api_get_path(SYS_LANG_PATH).'english';
13
ini_set('memory_limit', '128M');
14
/**
15
 * Main code.
16
 */
17
$terms = [];
18
$list = SubLanguageManager::get_lang_folder_files_list($path);
19
foreach ($list as $entry) {
20
    $file = $path.'/'.$entry;
21
    if (is_file($file)) {
22
        $terms = array_merge($terms, SubLanguageManager::get_all_language_variable_in_file($file, true));
23
    }
24
}
25
// get only the array keys (the language variables defined in language files)
26
$defined_terms = array_flip(array_keys($terms));
27
$terms = null;
28
echo count($defined_terms)." terms were found in language files<br />";
29
30
// now get all terms found in all PHP files of Chamilo (this takes some
31
// time and memory)
32
$usedTerms = [];
33
$l = strlen(api_get_path(SYS_PATH));
34
$files = getAllPhpFiles(api_get_path(SYS_PATH));
35
// Browse files
36
foreach ($files as $file) {
37
    //echo 'Analyzing '.$file."<br />";
38
    $shortFile = substr($file, $l);
39
    //echo 'Analyzing '.$shortFile."<br />";
40
    $lines = file($file);
41
    // Browse lines inside file $file
42
    foreach ($lines as $line) {
43
        $myTerms = [];
44
        $res = preg_match_all('/get_lang\(\'(\\w*)\'\)/', $line, $myTerms);
45
        if ($res > 0) {
46
            foreach ($myTerms[1] as $term) {
47
                if (substr($term, 0, 4) == 'lang') {
48
                    $term = substr($term, 4);
49
                }
50
                $usedTerms[$term] = $shortFile;
51
            }
52
        } else {
53
            $res = 0;
54
            $res = preg_match_all('/\{[\'"](\\w*)[\'"]\|get_lang\}/', $line, $myTerms);
55
            if ($res > 0) {
56
                foreach ($myTerms[1] as $term) {
57
                    if (substr($term, 0, 4) == 'lang') {
58
                        $term = substr($term, 4);
59
                    }
60
                    $usedTerms[$term] = $shortFile;
61
                }
62
            }
63
        }
64
    }
65
    flush();
66
}
67
68
// Compare defined terms VS used terms. Used terms should be smaller than
69
// defined terms, and this should prove the concept that there are much
70
// more variables than what we really use
71
if (count($usedTerms) < 1) {
72
    die("No used terms<br />\n");
73
} else {
74
    echo "The following terms were defined but never used: <br />\n<table>";
75
}
76
$i = 1;
77
foreach ($defined_terms as $term => $file) {
0 ignored issues
show
Comprehensibility Bug introduced by
$file is overwriting a variable from outer foreach loop.
Loading history...
78
    // remove "lang" prefix just in case
79
    if (substr($term, 0, 4) == 'lang') {
80
        $term = substr($term, 4);
81
    }
82
    if (!isset($usedTerms[$term])) {
83
        echo "<tr><td>$i</td><td>$term</td></tr>\n";
84
        $i++;
85
    }
86
}
87
echo "</table>\n";
88
89