The lifetime of a variable represents the time a variable exists in a script. It is calculated from the first line to the last line using a given variable.
Favoring variables with a short lifetime can improve code readability and help prevent potential errors. A variable with a long lifetime will likely span from one end of your script to the other, making it difficult to understand. This tool helps monitor the lifetime of your variables and ensures they are placed as close to their usage as possible.
This tool is distributed as a PHP Archive (PHAR):
wget https://github.com/DeGraciaMathieu/php-variable-lifetime-analyzer/raw/main/builds/variable-lifetime-analyzer
php variable-lifetime-analyzer --version
php variable-lifetime-analyzer analyze {path}
php variable-lifetime-analyzer analyze app
❀ PHP Var Lifetime Analyzer ❀
117 files found.
392 variables found.
┌───────────────────────────┬──────────────────────────────────────┬───────────────────────┐
│ Average variable lifetime │ Average variable lifetime per method │ Max variable lifetime │
├───────────────────────────┼──────────────────────────────────────┼───────────────────────┤
│ 2.18 │ 1.61 │ 22.00 │
└───────────────────────────┴──────────────────────────────────────┴───────────────────────┘
-
Average variable lifetime : This represents the average lifetime of variables across the entire project. It includes all variables from all methods and can be influenced by variables with very long lifetimes.
-
Average variable lifetime per method : This is the average of the lifetimes of variables within each method, averaged across all methods. It gives an indication of the average variable lifetime at the method level and is less influenced by extreme values.
-
Max variable lifetime : The maximum lifetime observed for any variable in the project. This value indicates the longest duration any single variable is in scope during the project's execution.
Options | Description |
---|---|
--json | Output the analysis results in JSON format |
Tip
Other analysis tools are available.