Skip to content

mechemsi/license

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

PHP License Management Tool

A Symfony bundle that provides command-line utilities for automatically adding license headers and strict type declarations to PHP files.

Overview

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

Installation

Via Composer

composer require mechemsi/license

Manual Installation

  1. Clone the repository:
git clone https://github.com/yourusername/license.git
cd license
  1. Install dependencies:
composer install

Usage

The tool provides three main command-line utilities:

1. Add License Headers

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/directory

Features:

  • Automatically detects if a license header already exists
  • Preserves existing code structure
  • Inserts license after the PHP opening tag
  • Processes .php, .phpt, and .phtml files

2. Add Strict Type Declarations

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/directory

Features:

  • 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

3. License Template

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
 */

How It Works

License Header Addition (add-license)

  1. File Discovery: Recursively scans directories for PHP files (.php, .phpt, .phtml)
  2. Token Analysis: Uses PHP's token_get_all() to parse file structure
  3. Detection: Checks for existing license headers (PHPDoc comments)
  4. Insertion: Places the license header after the PHP opening tag
  5. Preservation: Maintains original file formatting and structure

Strict Types Addition (add-line)

  1. File Discovery: Same recursive scanning as license addition
  2. Token Analysis: Parses PHP tokens to understand file structure
  3. Smart Placement:
    • After <?php tag for simple files
    • Before namespace declaration if present
    • After PHPDoc comments if they exist
  4. Duplicate Prevention: Skips files that already have the declaration

File Processing

Supported File Types

  • .php - Standard PHP files
  • .phpt - PHP test files
  • .phtml - PHP template files

Processing Rules

  • 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

Examples

Example 1: Adding License to a Project

# 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
# ...

Example 2: Adding Strict Types

# Add strict types to a specific file
./bin/add-line src/Entity/User.php

# Output:
# src/Entity/User.php (changed)

Before and After

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;
}

Configuration

Custom License Template

To use a custom license template:

  1. Edit the bin/license file with your license text
  2. Run add-license command to apply the new template

Integration with Development Workflow

Pre-commit Hook

Add to .git/hooks/pre-commit:

#!/bin/bash
./bin/add-line src/
./bin/add-license src/
git add -u

Composer Scripts

Add to composer.json:

{
    "scripts": {
        "add-headers": [
            "./bin/add-line src/",
            "./bin/add-license src/"
        ],
        "cs-fix": [
            "@add-headers",
            "php-cs-fixer fix"
        ]
    }
}

Requirements

  • PHP 7.0 or higher
  • Composer (for dependency management)

Development

Project Structure

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

Testing

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

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/improvement)
  3. Make your changes
  4. Test thoroughly
  5. Commit your changes (git commit -am 'Add new feature')
  6. Push to the branch (git push origin feature/improvement)
  7. Create a Pull Request

Troubleshooting

Common Issues

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

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Acknowledgments

  • Based on concepts from dg/php54-arrays by David Grudl
  • Strict types addition script contributions by Jan Vanura

Support

For issues, questions, or contributions, please:

  1. Check the Issues page
  2. Create a new issue if your problem isn't already listed
  3. Contact the maintainer at dominykas.vilipas@gmail.com

Changelog

Version 1.0.0

  • Initial release
  • License header addition functionality
  • Strict type declaration addition functionality
  • Support for processing individual files and directories
  • Automatic detection of existing headers/declarations

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages