A Symfony bundle that provides command-line utilities for automatically adding license headers and strict type declarations to PHP files.
This tool helps maintain consistent code standards across PHP projects by automatically:
- Adding license headers to PHP files
- Adding
declare(strict_types=1)declarations - Processing individual files or entire directories recursively
composer require mechemsi/license- Clone the repository:
git clone https://github.com/yourusername/license.git
cd license- Install dependencies:
composer installThe tool provides three main command-line utilities:
Adds license headers to PHP files based on the template in bin/license:
# Add license to a single file
./bin/add-license path/to/file.php
# Add license to all PHP files in a directory
./bin/add-license path/to/directoryFeatures:
- Automatically detects if a license header already exists
- Preserves existing code structure
- Inserts license after the PHP opening tag
- Processes
.php,.phpt, and.phtmlfiles
Adds declare(strict_types=1); to PHP files for type safety:
# Add to a single file
./bin/add-line path/to/file.php
# Add to all PHP files in a directory
./bin/add-line path/to/directoryFeatures:
- Automatically detects if declaration already exists
- Properly handles files with existing comments
- Places declaration in the correct position (after opening tag, before namespace)
- Supports files with PHPDoc comments
The license text template is stored in bin/license. Modify this file to customize the license header that will be added to your files.
Current template:
/**
* @summary License
*
* @license MIT
*
* @author Mechemsi
*/- File Discovery: Recursively scans directories for PHP files (
.php,.phpt,.phtml) - Token Analysis: Uses PHP's
token_get_all()to parse file structure - Detection: Checks for existing license headers (PHPDoc comments)
- Insertion: Places the license header after the PHP opening tag
- Preservation: Maintains original file formatting and structure
- File Discovery: Same recursive scanning as license addition
- Token Analysis: Parses PHP tokens to understand file structure
- Smart Placement:
- After
<?phptag for simple files - Before namespace declaration if present
- After PHPDoc comments if they exist
- After
- Duplicate Prevention: Skips files that already have the declaration
.php- Standard PHP files.phpt- PHP test files.phtml- PHP template files
- Only processes files with PHP opening tags (
<?php) - Preserves existing whitespace and formatting
- Shows which files were modified
- Skips files that already have the required additions
# Add license headers to entire src directory
./bin/add-license src/
# Output:
# src/Controller/UserController.php (changed)
# src/Entity/User.php (changed)
# src/Service/UserService.php
# ...# Add strict types to a specific file
./bin/add-line src/Entity/User.php
# Output:
# src/Entity/User.php (changed)Before:
<?php
namespace App\Entity;
class User
{
private string $name;
}After (with license and strict types):
<?php
/**
* @summary License
*
* @license MIT
*
* @author Mechemsi
*/
declare(strict_types=1);
namespace App\Entity;
class User
{
private string $name;
}To use a custom license template:
- Edit the
bin/licensefile with your license text - Run
add-licensecommand to apply the new template
Add to .git/hooks/pre-commit:
#!/bin/bash
./bin/add-line src/
./bin/add-license src/
git add -uAdd to composer.json:
{
"scripts": {
"add-headers": [
"./bin/add-line src/",
"./bin/add-license src/"
],
"cs-fix": [
"@add-headers",
"php-cs-fixer fix"
]
}
}- PHP 7.0 or higher
- Composer (for dependency management)
license/
├── bin/
│ ├── add-license # Script to add license headers
│ ├── add-line # Script to add strict type declarations
│ └── license # License template file
├── src/ # Source code (if using as Symfony bundle)
├── vendor/ # Composer dependencies
├── composer.json # Package configuration
├── composer.lock # Dependency lock file
└── README.md # This file
Run the tools on test files to verify functionality:
# Create a test file
echo '<?php class Test {}' > test.php
# Add strict types
./bin/add-line test.php
# Add license
./bin/add-license test.php
# Verify the result
cat test.php- Fork the repository
- Create a feature branch (
git checkout -b feature/improvement) - Make your changes
- Test thoroughly
- Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/improvement) - Create a Pull Request
Issue: "Path not found" error
- Solution: Ensure the path exists and is accessible
Issue: Files not being processed
- Solution: Check file extensions are
.php,.phpt, or.phtml
Issue: Headers added multiple times
- Solution: The tool should detect existing headers, but check the detection logic in the source
Issue: Incorrect placement of declarations
- Solution: Ensure your PHP files have proper syntax before processing
This project is licensed under the MIT License - see the LICENSE file for details.
- Mechemsi - dominykas.vilipas@gmail.com
- Based on concepts from dg/php54-arrays by David Grudl
- Strict types addition script contributions by Jan Vanura
For issues, questions, or contributions, please:
- Check the Issues page
- Create a new issue if your problem isn't already listed
- Contact the maintainer at dominykas.vilipas@gmail.com
- Initial release
- License header addition functionality
- Strict type declaration addition functionality
- Support for processing individual files and directories
- Automatic detection of existing headers/declarations