Last active
December 19, 2015 04:08
-
-
Save ekho/5894525 to your computer and use it in GitHub Desktop.
Shows duplicated classes in two 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 | |
if ($argc != 3) { | |
echo 'Usage:', PHP_EOL, "\tphp ", basename($argv[0]), ' <source directory 1> <source directory 2>', PHP_EOL; | |
exit(1); | |
} | |
function token_get_namespaces($tokens) | |
{ | |
$tokens = array_map( | |
function ($token) { | |
if (is_array($token)) { | |
unset($token[2]); | |
} | |
return $token; | |
}, | |
$tokens | |
); | |
$namespaces = array(); | |
foreach (array_keys($tokens, array(T_NAMESPACE, "namespace")) as $index) { | |
$namespaces[$index] = implode( | |
'', | |
array_map( | |
function ($token) { | |
return $token[1]; | |
}, | |
array_filter( | |
array_slice( | |
$tokens, | |
$index + 1, | |
min( | |
array_search(';', array_slice($tokens, $index + 1)) ? : PHP_INT_MAX, | |
array_search('{', array_slice($tokens, $index + 1)) ? : PHP_INT_MAX | |
) | |
), | |
function ($token) { | |
return is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING)); | |
} | |
) | |
) | |
); | |
} | |
return $namespaces; | |
} | |
function token_get_classes($tokens, $namespace = null) | |
{ | |
$tokens = array_map( | |
function ($token) { | |
if (is_array($token)) { | |
unset($token[2]); | |
} | |
return $token; | |
}, | |
$tokens | |
); | |
$classes = array(); | |
foreach (array_keys($tokens, array(T_CLASS, "class")) as $index) { | |
$classes[$index] = ($namespace ? '\\' . $namespace . '\\' : '') . reset( | |
array_map( | |
function ($token) { | |
return $token[1]; | |
}, | |
array_filter( | |
array_slice( | |
$tokens, | |
$index + 1, | |
array_search('{', array_slice($tokens, $index + 1)) ? : null | |
), | |
function ($token) { | |
return is_array($token) && $token[0] = T_STRING && trim($token[1]); | |
} | |
) | |
) | |
); | |
} | |
return $classes; | |
} | |
$classLists = array_map( | |
function ($dir) { | |
$iterator = new RegexIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)), '/\.php$/i', RecursiveRegexIterator::GET_MATCH); | |
$classes = array(); | |
foreach ($iterator as $phpClassFile => $object) { | |
$tokens = token_get_all(file_get_contents($phpClassFile)); | |
$namespaces = token_get_namespaces($tokens); | |
if (!empty($namespaces)) { | |
foreach ($namespaces as $index => $namespace) { | |
unset($namespaces[$index]); | |
$classes = array_merge( | |
$classes, | |
token_get_classes( | |
array_slice( | |
$tokens, | |
$index + 1, | |
empty($namespaces) ? null : reset(array_keys($namespaces)) - $index - 2 | |
), | |
$namespace | |
) | |
); | |
} | |
} else { | |
$classes = array_merge($classes, token_get_classes($tokens)); | |
} | |
} | |
return $classes; | |
}, | |
array_slice($argv, 1) | |
); | |
$intersectedClasses = call_user_func_array('array_intersect', $classLists); | |
echo implode(PHP_EOL, $intersectedClasses), PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment