Lynter is a PHP code analysis tool focused on restricting specific usages within your codebase. It allows you to enforce custom rules, such as restricting certain functions, variables, or classes, ensuring that your code adheres to specific standards. Lynter can be used in continuous integration pipelines or as a pre-commit hook in your Git workflow.
- Installation
- Usage
- Configuration
- Built-in Rules
- Excluding Files and Directories
- Parallel Execution
- Output Formats
- Examples
- Testing
- Contributing
- License
To install Lynter, use Composer:
composer require --dev lynter/lynter
Make sure to include Lynter in your composer.json
file's require-dev
section for development dependencies.
Lynter can be run from the command line to analyze PHP files or directories. Here's the basic syntax:
./vendor/bin/lynter analyze [options] <paths>
--config=<file>
: Specify the path to the YAML configuration file (default:lynter.yml
).--output=<format>
: Specify the output format (raw
,json
). Default israw
.--parallel=<number>
: Specify the number of parallel processes to use for analysis.
./vendor/bin/lynter analyze src --config=lynter.yml --output=raw --parallel=4
This command will analyze the src
directory using the configuration file lynter.yml
, output results in raw format, and utilize 4 parallel processes.
Lynter is configured using a YAML file. The default configuration file is lynter.yml
. You can specify custom rules, messages, and exclusions in this file.
rules:
- name: restrict-functions
rule: restrictFunction
matcher: exact
values:
- eval
- exec
- shell_exec
message: "This function '{value}' is not allowed."
- name: restrict-functions-regex
rule: restrictFunction
matcher: pattern
values:
- '/^debug_/' # Restricts any function starting with "debug_"
message: "This function matching '{value}' is not allowed."
- name: restrict-variables
rule: restrictVariable
matcher: exact
values:
- $_GET
- $_POST
message: "This variable '{value}' is restricted."
- name: restrict-variables-regex
rule: restrictVariable
matcher: pattern
values:
- '/^\$temp/' # Restricts any variable starting with "$temp"
message: "This variable matching '{value}' is restricted."
- name: restrict-classes
rule: restrictClass
matcher: exact
values:
- MyRestrictedClass
message: "Instantiation of '{value}' is not allowed."
- name: restrict-classes-regex
rule: restrictClass
matcher: pattern
values:
- '/^Legacy/' # Restricts any class starting with "Legacy"
message: "Instantiation of class matching '{value}' is not allowed."
exclude:
- vendors
- tests
- rules: Define the rules for restricting functions, variables, and classes. Each rule type (
restrictFunction
,restrictVariable
,restrictClass
) accepts an array of values to restrict, a matcher (exact
orpattern
), and a custom message template. - exclude: Specify directories or files to exclude from analysis.
Lynter comes with the following built-in rules:
Restrict the usage of specific functions.
rules:
- name: restrict-functions
rule: restrictFunction
matcher: exact
values:
- eval
- exec
message: "This function '{value}' is not allowed."
Restrict the usage of specific global variables.
rules:
- name: restrict-variables
rule: restrictVariable
matcher: exact
values:
- $_GET
- $_POST
message: "This variable '{value}' is restricted."
Restrict the instantiation of specific classes.
rules:
- name: restrict-classes
rule: restrictClass
matcher: exact
values:
- MyRestrictedClass
message: "Instantiation of '{value}' is not allowed."
You can exclude specific files or directories from being analyzed by using the exclude
option in your configuration file.
exclude:
- vendor/
- tests/
- src/legacy/
Lynter supports parallel execution to speed up the analysis of large codebases. You can specify the number of parallel processes using the --parallel
option.
./vendor/bin/lynter analyze src --parallel=4
This will run Lynter with 4 parallel processes.
Lynter supports two output formats: raw
and json
.
The default output format is raw
, which provides a human-readable summary of the issues.
For machine-readable output, use the json
format.
./vendor/bin/lynter analyze src --output=json
./vendor/bin/lynter analyze src --config=lynter.yml
./vendor/bin/lynter analyze src/Example.php src/AnotherExample.php --config=lynter.yml
./vendor/bin/lynter analyze src --config=lynter.yml --parallel=4
./vendor/bin/lynter analyze src --output=json
Lynter includes a set of PHPUnit tests to ensure the integrity of the tool.
To run the tests, use the following command:
./vendor/bin/phpunit
This will execute the test suite and provide feedback on any issues.
Contributions are welcome! Please fork this repository, make your changes, and submit a pull request.
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Write tests for your changes.
- Ensure all tests pass.
- Submit a pull request.
This project follows PSR-12 coding standards. Please ensure your code adheres to these guidelines.
Lynter is open-source software licensed under the MIT license. See the LICENSE
file for more information.