cmgmyr/phploc
Laravel-friendly wrapper around phpLOC for measuring PHP project size and structure. Generate lines-of-code, classes, methods, complexity-style stats from the command line or within builds to quickly gauge codebase scope and trends.
Installation:
composer require --dev cmgmyr/phploc
Ensure the package is registered in composer.json under require-dev.
Publish the Artisan Command: Run:
php artisan vendor:publish --provider="Cmgmyr\PHPLOC\PhpLocServiceProvider"
This creates a phploc.php config file in config/ (default path: config/phploc.php).
First Use Case: Run a basic analysis on your Laravel project:
php artisan phploc
By default, it scans app/, routes/, and database/ directories. Verify the output matches your expectations (e.g., class counts, LOC trends).
Customize Directories:
Update config/phploc.php to exclude or include specific paths:
'directories' => [
base_path('app'),
base_path('routes'),
base_path('tests'), // Example: Add tests
],
'exclude' => [
'app/Helpers/*', // Example: Exclude helpers
],
php artisan phploc before major changes to capture a baseline. Compare post-refactor metrics to validate improvements (e.g., reduced LOC, lower cyclomatic complexity).
php artisan phploc --output=pre-refactor.json
--log to append results to a file (e.g., storage/logs/phploc.log) and review trends over time:
php artisan phploc --log --output-format=json
- name: Check LOC
run: |
LOC=$(php artisan phploc --logical-lines | grep "Logical Lines of Code" | awk '{print $3}')
if [ "$LOC" -gt 5000 ]; then
echo "LOC exceeds threshold ($LOC > 5000)"
exit 1
fi
php artisan phploc --output-format=json | jq '.metrics["Average Complexity per Method"]' > complexity.json
phploc.php to skip vendor/, bootstrap/cache/, and storage/framework/:
'exclude' => [
'vendor/*',
'bootstrap/cache/*',
'storage/framework/*',
],
app/Http/Controllers/ and app/Services/ for high-impact analysis:
php artisan phploc app/Http/Controllers/ app/Services/
--output-format=html to create a visual report (save to storage/phploc-report.html):
php artisan phploc --output-format=html --output=storage/phploc-report.html
php artisan phploc --output-format=json --output=storage/phploc-metrics.json
False LOC Counts:
phploc.php:
'exclude' => [
'resources/views/*',
'config/*',
'*.blade.php',
],
Performance on Large Codebases:
vendor/ or monorepos can be slow.--progress (if available in future versions) to monitor execution.Config File Missing:
php artisan phploc without publishing the config may throw errors.config/phploc.php with defaults:
return [
'directories' => [base_path('app'), base_path('routes')],
'exclude' => [],
'logical_lines_only' => false,
];
Deprecated CLI Options:
--names (replaced by --suffix) may appear in old docs.Silent Failures:
chmod -R 755 app/).^7.4 || ^8.0 is met).--verbose (if supported) or check Laravel logs:
php artisan phploc 2>&1 | grep -i error
Inconsistent Metrics:
php artisan cache:clear
php artisan phploc --force
Custom Metrics:
Cmgmyr\PHPLOC\Application class to add project-specific metrics (e.g., "Laravel-specific complexity").getMetrics() in a service provider.Pre/Post-Scan Hooks:
registering and booted events in PhpLocServiceProvider to run logic before/after analysis:
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
(new \Cmgmyr\PHPLOC\Console\PhpLocCommand())
->setMetricsCallback(function ($metrics) {
// Log custom metrics here
}),
]);
}
}
Parallel Processing:
parallel-lint-style workflows).Service Provider Binding:
$this->app->singleton('phploc', function ($app) {
return (new \Cmgmyr\PHPLOC\Application())
->addDirectory(base_path('app'))
->setExcludes([base_path('vendor')]);
});
Dynamic Directory Scanning:
tests/ in production):
'directories' => env('APP_ENV') === 'production'
? [base_path('app/Http')]
: [base_path('app'), base_path('tests')],
Integration with Laravel Mix:
npm run dev or npm run build for frontend-backend correlation:
// webpack.mix.js
mix.webpackConfig({
plugins: [
{
apply: (compiler) => {
compiler.hooks.done.tap('PhpLoc', () => {
require('child_process').execSync('php artisan phploc --log');
});
}
}
]
});
How can I help you explore Laravel packages today?