Skip to content

Instantly share code, notes, and snippets.

@totten
Last active Aug 29, 2015
Embed
What would you like to do?
Analyze PHP tokens to determine frequency of lowerCamel, under_score, etc.
find CRM/ Civi/ api/ tests/phpunit/ -name '*.php' | php /tmp/tok.php
...
Array
(
[ALL_CAP] => 34330
[bareword] => 298321
[lowerCamel] => 150877
[Mixed_Camel_Under_Score] => 42276
[under_score] => 23259
[_lowerCamel] => 17054
[_under_score] => 20396
[Upper] => 1323
[UpperCamel] => 1350
)
<?php
$files = explode("\n", file_get_contents('php://stdin'));
$cats = array();
foreach ($files as $file) {
if (empty($file)) continue;
$toks = token_get_all(file_get_contents($file));
foreach ($toks as $tok) {
if (is_array($tok)) {
switch (token_name($tok[0])){
case 'T_VARIABLE':
case 'T_STRING':
$expr = str_replace('$', '', $tok[1]);
$cat = 'other';
if (preg_match('/^[0-9\.]+$/', $expr)) {
$cat = 'numeric';
}
elseif (preg_match('/^[a-z0-9]+$/', $expr)) {
$cat = 'bareword';
}
elseif (preg_match('/^[A-Z_]+$/', $expr)) {
$cat = 'ALL_CAP';
}
elseif (preg_match('/^[a-z0-9]+_[a-z0-9_]*$/', $expr)) {
$cat = 'under_score';
}
elseif (preg_match('/^_[a-z0-9_]+$/', $expr)) {
$cat = '_under_score';
}
elseif (preg_match('/^[A-Z][a-z0-9]+$/', $expr)) {
$cat = 'Upper';
}
elseif (preg_match('/^[a-z0-9]+[A-Z]+[a-zA-Z0-9]*$/', $expr)) {
$cat = 'lowerCamel';
}
elseif (preg_match('/^_[a-z0-9]+[A-Z]+[a-zA-Z0-9]*$/', $expr)) {
$cat = '_lowerCamel';
}
elseif (preg_match('/^[A-Z][a-z0-9]*[A-Z][a-zA-Z0-9]*$/', $expr)) {
$cat = 'UpperCamel';
}
elseif (preg_match('/^[a-zA-Z0-9_]+$/', $expr) && preg_match('/_/', $expr) && preg_match('/[A-Z]/', $expr) ) {
$cat = 'Mixed_Camel_Under_Score';
}
# echo "$expr ==> $cat\n";
if (!isset($cats[$cat])) $cats[$cat] = 0;
$cats[$cat]++;
}
}
}
echo ".";
}
print_r($cats);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment