nilportugues/php_backslasher
CLI tool that scans PHP code and prefixes internal functions/constants (plus true/false/null) with a leading backslash for faster resolution, especially with OPcache. Run php bin/php_backslasher fix to update a directory.
Installation:
composer require --dev nilportugues/php_backslasher
Add to composer.json under require-dev to ensure it’s only installed in development environments.
First Run (Dry Run):
php bin/php_backslasher fix app/
Use --dry-run (if supported) or manually review changes with git diff to verify correctness.
First Use Case:
app/Http/Controllers) to test the tool’s behavior.strlen, count, json_encode) or constants (e.g., PHP_EOL, JSON_PRETTY_PRINT).app/Providers/ (Service providers often use internal functions like app(), config(), route()).app/Http/Controllers/ (Controllers frequently call response(), redirect(), abort()).app/Helpers/ or custom utility files (likely to contain raw internal function calls).config/ (e.g., app.php, filesystems.php) may use constants like DIRECTORY_SEPARATOR or PHP_INT_MAX.vendor/ (risk of breaking third-party packages).config/cache.php or generated files (may be overwritten).Add to composer.json to run automatically after composer install:
"scripts": {
"post-install-cmd": [
"@php_backslasher"
],
"php_backslasher": "php bin/php_backslasher fix app"
}
composer install; exclude in production builds.Use a pre-commit hook to catch unqualified internal functions before they’re merged:
# .git/hooks/pre-commit
#!/bin/bash
php bin/php_backslasher fix --dry-run app/ | grep -q "ERROR" && exit 1
Create a custom Artisan command for IDE-friendly execution:
// app/Console/Commands/BackslasherCommand.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class BackslasherCommand extends Command
{
protected $signature = 'backslasher:fix {directory? : Directory to fix}';
public function handle()
{
$directory = $this->argument('directory') ?? app_path();
$this->call('vendor:publish', ['--provider' => 'nilportugues\php_backslasher\ServiceProvider']);
shell_exec("php bin/php_backslasher fix {$directory}");
}
}
Use the tool incrementally:
# Fix only files modified in the last week
git log --since="1 week ago" --name-only --pretty=format: | xargs php bin/php_backslasher fix
composer require --dev nilportugues/php_backslasher
php bin/php_backslasher fix app/Http/Controllers --dry-run
git diff
.github/workflows/laravel.yml:
- name: Run BackSlasher
run: composer php_backslasher
vendor/ or config/ if issues arise.php artisan tinker --bench --memory --iterations=100
Measure baseline performance (e.g., route execution time).php_backslasher before php-cs-fixer to avoid rework:
"scripts": {
"post-install-cmd": [
"php bin/php_backslasher fix app",
"@php-cs-fixer fix"
]
}
--exclude to skip sensitive files:
php bin/php_backslasher fix app/ --exclude="app/Providers/AppServiceProvider.php"
git stash
php bin/php_backslasher fix app/
git diff --cached | grep -q "^+" && echo "Review changes before commit" && exit 1
False Positives:
strlen), the tool will incorrectly add a backslash.
call_user_func('strlen') or create_function() will break.
PHP 8+ Edge Cases:
array|string).match ($x) { ... }).str_contains($haystack, $needle, $offset)).Namespace Conflicts:
use function declarations (e.g., use function strlen;), the tool may conflict.
use function for built-ins or exclude such files.Performance Myth:
php -i | grep opcache.enable
Tool Limitations:
\strlen).git diff first.Dry Run: Simulate changes without modifying files:
php bin/php_backslasher fix app/ --dry-run # If supported
# Or manually:
git stash
php bin/php_backslasher fix app/
git diff --cached
git stash pop
Log Output: Redirect output to a file for review:
php bin/php_backslasher fix app/ > backslasher.log 2>&1
Test on a Clone: Work on a fresh clone of the repo to avoid accidental merges:
git clone <repo> temp-fix
cd temp-fix
composer install
php bin/php_backslasher fix app/
Symfony Console Dependency:
The tool requires symfony/console (v2–4). If missing, install it:
composer require symfony/console "^4"
Zend Code Dependency:
The tool uses zendframework/zend-code (v3). Laravel may not include this by default:
composer require zendframework/zend-code:~3
Case Sensitivity:
The tool is case-sensitive. Ensure your filesystem and editor preserve case (e.g., strlen vs. StrLen).
How can I help you explore Laravel packages today?