Last active
September 28, 2022 02:38
-
-
Save brendt/09026efba38a2eae952556aa274c268f to your computer and use it in GitHub Desktop.
analyse-attribute-usage.php
This file contains 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 | |
require_once __DIR__ . '/vendor/autoload.php'; | |
function scan(string $path): array | |
{ | |
$o_dir = new RecursiveDirectoryIterator($path); | |
$o_iter = new RecursiveIteratorIterator($o_dir); | |
$files = []; | |
foreach ($o_iter as $o_info) { | |
if ($o_info->getExtension() === 'php') { | |
$files[] = $o_info->getPathname(); | |
} | |
} | |
return $files; | |
} | |
function getPerPackageCsv() | |
{ | |
$perPackageCsv = fopen(__DIR__ . '/attributes-per-package.csv', 'w'); | |
fputcsv($perPackageCsv, [ | |
'package', | |
'count', | |
]); | |
return $perPackageCsv; | |
} | |
function getPerPackagePerAttributeCsv() | |
{ | |
$perPackagePerAttributeCsv = fopen(__DIR__ . '/attributes-per-package-per-attribute.csv', 'w'); | |
fputcsv($perPackagePerAttributeCsv, [ | |
'package', | |
'attribute', | |
'count', | |
]); | |
return $perPackagePerAttributeCsv; | |
} | |
function getPerFileCsv() | |
{ | |
$perFileCsv = fopen(__DIR__ . '/attributes-per-file.csv', 'w'); | |
fputcsv($perFileCsv, [ | |
'vendor', | |
'package', | |
'path', | |
'count', | |
]); | |
return $perFileCsv; | |
} | |
function getPerAttributeCsv() | |
{ | |
$packageCsv = fopen(__DIR__ . '/attributes.csv', 'w'); | |
fputcsv($packageCsv, [ | |
'attribute', | |
'count', | |
]); | |
return $packageCsv; | |
} | |
$packages = glob(__DIR__ . '/sources/**/**'); | |
$totalCount = 0; | |
$perFileCsv = getPerFileCsv(); | |
$perAttributeCsv = getPerAttributeCsv(); | |
$perPackageCsv = getPerPackageCsv(); | |
$perPackagePerAttributeCsv = getPerPackagePerAttributeCsv(); | |
$packageCount = count($packages); | |
$perAttributeCount = []; | |
foreach ($packages as $i => $packagePath) { | |
$perPackagePerAttributeCount = []; | |
$memory = memory_get_usage() / 1_000_000; | |
echo "[{$i}/{$packageCount}] Scanning {$packagePath}\n"; | |
echo "{$memory}MB\n"; | |
$packageName = explode('/', $packagePath); | |
$lastIndex = array_key_last($packageName); | |
[$vendor, $package] = [$packageName[$lastIndex - 1], $packageName[$lastIndex]]; | |
if ($vendor === 'jetbrains') { | |
continue; | |
} | |
$phpFiles = scan($packagePath); | |
$countForPackage = 0; | |
foreach ($phpFiles as $phpFile) { | |
$contents = file_get_contents($phpFile); | |
preg_match_all('/(\s|^)#\[([\\\\\w\s,]+)/', $contents, $matches); | |
$attributesInFile = ($matches[2] ?? []); | |
$countForPackage += count($attributesInFile); | |
$row = [ | |
'vendor' => $vendor, | |
'package' => $package, | |
'path' => $phpFile, | |
'count' => count($attributesInFile), | |
]; | |
foreach ($attributesInFile as $attribute) { | |
$perAttributeCount[$attribute] ??= 0; | |
$perAttributeCount[$attribute]++; | |
$perPackagePerAttributeCount[$attribute] ??= 0; | |
$perPackagePerAttributeCount[$attribute]++; | |
} | |
fputcsv($perFileCsv, $row); | |
} | |
fputcsv($perPackageCsv, [ | |
'package' => "{$vendor}/{$package}", | |
'count' => $countForPackage, | |
]); | |
foreach ($perPackagePerAttributeCount as $attribute => $count) { | |
fputcsv($perPackagePerAttributeCsv, [ | |
'package' => "{$vendor}/{$package}", | |
'attribute' => $attribute, | |
'count' => $count, | |
]); | |
} | |
echo "{$vendor}/{$package}: {$countForPackage}\n"; | |
echo "\n"; | |
$totalCount += count($phpFiles); | |
} | |
foreach ($perAttributeCount as $attribute => $count) { | |
fputcsv($perAttributeCsv, [ | |
'attribute' => $attribute, | |
'count' => $count, | |
]); | |
} | |
fclose($perFileCsv); | |
fclose($perAttributeCsv); | |
echo "Done\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment