Created
October 13, 2020 20:12
-
-
Save mohsin/d913df056992f11ed632e7d466cb3bb9 to your computer and use it in GitHub Desktop.
Find duplicate strings in Laravel resource lang folders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This program takes in a Laravel Resource folder strings and finds the frequency of the strings i.e. repeatation. | |
* Usage: php duplicate-lang-str.php ~/Desktop/resources/lang/en/ | |
*/ | |
if (!isset($argv[1])) { | |
die(sprintf("\e[37;41mFolder path unspecified\e[39;49m\nCommand usage: php %s <folder-path>", $argv[0])); | |
} | |
if (!file_exists($argv[1])) { | |
die("\e[37;41mDirectory does not exist in the given path!\e[39;49m"); | |
} | |
if (!is_dir($argv[1])) { | |
die("\e[37;41mGiven path is not a valid directory!\e[39;49m"); | |
} | |
$files = getPhpFiles($argv[1]); | |
$multiArr = []; | |
foreach ($files as $file) { | |
$multiArr[] = require $file; | |
} | |
$resultArr = []; | |
$resultCountArr = []; | |
flatten_arr($multiArr, $resultArr, $resultCountArr); | |
foreach ($resultArr as $key => $value) { | |
if (isset($argv[2])) { | |
if ($resultCountArr[$key] >= ((int) $argv[2])) { | |
echo sprintf("%s - %d times\n", $value, $resultCountArr[$key]); | |
} | |
} else { | |
echo sprintf("%s - %d times\n", $value, $resultCountArr[$key]); | |
} | |
} | |
function flatten_arr($multiArr, &$valuesArr = [], &$valuesCountArr = []) | |
{ | |
foreach ($multiArr as $key => $value) { | |
if (is_array($value)) { | |
flatten_arr($value, $valuesArr, $valuesCountArr); | |
} else { | |
if (($key = array_search($value, $valuesArr)) !== false) { | |
$valuesCountArr[$key]++; | |
} else { | |
$valuesArr[] = $value; | |
$valuesCountArr[] = 1; | |
} | |
} | |
} | |
} | |
function config() {} // In case it's defined | |
function getPhpFiles($startDir, &$phpFiles = []) | |
{ | |
if ($handle = opendir($startDir)) { | |
while (false !== ($entry = readdir($handle))) { | |
if ($entry != "." && $entry != "..") { | |
$fullPath = $startDir . DIRECTORY_SEPARATOR . $entry; | |
if (is_dir($fullPath)) { | |
getPhpFiles($fullPath, $phpFiles); | |
} else { | |
if (pathinfo($fullPath)['extension'] == 'php') { | |
$phpFiles[] = $fullPath; | |
} | |
} | |
} | |
} | |
closedir($handle); | |
} | |
return $phpFiles; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment