Download the PHP package joeymckenzie/nasastan without Composer
On this page you can find all versions of the php package joeymckenzie/nasastan. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download joeymckenzie/nasastan
More information about joeymckenzie/nasastan
Files in joeymckenzie/nasastan
Package nasastan
Short Description A PHPStan extension to enforce NASA's Power of Ten rules.
License MIT
Informations about the package nasastan
🚀 NASAStan
PHPStan extension that enforces NASA's Power of Ten rules in your PHP code.
Table of Contents
- Motivation
- Getting started
- Usage
- Power of Ten Rules
- [Rules]()
- 1. Avoid complex flow constructs
- 2. All loops must have fixed bounds
- 3. Avoid heap memory allocation after initialization
- 4. Restrict functions to a single printed page
- 5. Use a minimum of two runtime assertions per function
- 6. Restrict the scope of data to the smallest possible
- 7. Check the return value of all non-void functions
- 8. Use the preprocessor only for header files and simple macros
- 9. Limit pointer use to a single dereference
- 10. Compile with all possible warnings active
- References
Why should I use this extension?
Great question. I'm still trying to figure out an answer to that myself.
Getting started
To get started, install the package with composer:
Usage
If you're using phpstan/extension-installer, you're all set!
If not, however, include the extension in your PHPStan configuration:
Not all rules may be applicable to PHP. Yes, I'm aware these rules are primarily targeted at C/C++ and some may not be nonsensical for PHP's use case. The rules are opt-in, allowing for a "choose your own" adventure with rules you may prefer while disabling ones that you may want in your project.
Configuring NASAStan
To use NASAStan with its default configuration, add the extension to your phpstan.neon file:
Customizing rules
You can customize NASAStan's behavior by overriding configuration parameters in your phpstan.neon file.
Enabling or disabling rules
By default, all NASAStan rules are enabled. To select only specific rules:
To exclude specific rules while keeping others enabled:
Rule-specific configuration
Each rule has its own customizable parameters. Here are examples for each rule:
Rule 2: Fixed upper bounds on loops
Rule 3: No heap allocation after initialization
Rule 4: Restrict function length
Rule 5: Minimum assertions per function
Rule 6: Restrict data scope
Rule 7: Check return values
Rule 9: Limit dereferences
Rule 10: Compile with all warnings active
Complete configuration example
Here's an example of a complete configuration with customized values:
Default Values
If you don't override a configuration parameter, NASAStan will use the following default values:
| Parameter | Default Value |
|---|---|
| enabledRules | All rules (1-7, 9-10) |
| exceptRules | [] (empty array) |
| maxAllowedIterations | 1000 |
| maxLinesPerFunction | 60 |
| includeComments | true |
| includeBlankLines | true |
| minimumAssertionsRequired | 2 |
| maxClassProperties | 10 |
| maxAllowedDereferences | 1 |
For complete details on all available configuration options, refer to the
NASAStanConfiguration.php class or the default
extension.neon file.
Original NASA Power of Ten Rules
- Avoid complex flow constructs, such as goto and recursion.
- All loops must have fixed bounds. This prevents runaway code.
- Avoid heap memory allocation after initialization.
- Restrict functions to a single printed page.
- Use a minimum of two runtime assertions per function.
- Restrict the scope of data to the smallest possible.
- Check the return value of all non-void functions, or cast to void to indicate the return value is useless.
- Use the preprocessor only for header files and simple macros.
- Limit pointer use to a single dereference, and do not use function pointers.
- Compile with all possible warnings active; all warnings should then be addressed before release of the software.
Rules
Rule #1
Avoid complex flow constructs, such as goto and recursion
Disallows the use of goto statements and recursive functions. The following code would be in direct violation of this
rule and reported on by NASAStan:
Rule #2
All loops must have fixed bounds. This prevents runaway code
Enforces all loops within PHP code to have a fixed upper bound. Things like while(true), do-while(true), Generator
types, and array types greater than the configurable upper-bound will cause NASAStan to flag for errors.
Unbound while loops
With Generator types
Rule #3
Avoid heap memory allocation after initialization
Constricts resource allocation to only be allowed within approved initialization methods. Things like newing up
objects outside of constructors, array allocations, etc. will cause NASAStan to report on this rule.
Rule #4
Restrict functions to a single printed page
Enforces a strict method length rule within a function or method. Can be adjusted through configuration with a default set to 60 lines.
Rule #5
Use a minimum of two runtime assertions per function
Requires at least two runtime assertions either in the form of assert() methods, exceptions, or test-based assertions
like PHPUnit's Assert.
Rule #6
Restrict the scope of data to the smallest possible
This rule limits the number of properties on classes and their visibility. Properties may be whitelisted within configuration telling NASAStan to ignore reporting on these instances.
Rule #7
Check the return value of all non-void functions, or cast to void to indicate the return value is useless
This rule enforces all method and function values to be used. Any unused values will be reported by NASAStan.
Rule #8
Use the preprocessor only for header files and simple macros
This rule does not apply to PHP and has been deliberately ignored. PHP does not use preprocessors for header files nor allows for the use of traditional macros.
Rule #9
Limit pointer use to a single dereference, and do not use function pointers
This rule limits the number of times functions or properties may be derefenced (or called) within code. The amount of dereferences is configurable.
Rule #10
Compile with all possible warnings active; all warnings should then be addressed before release of the software
This rules bans the use of @ error suppression symbols and enforces the use of strict type declarations.