Skip to content

Commit 342bbf8

Browse files
authored
Merge pull request #4 from typephp-php/next-minor-released
Next minor released docs
2 parents 5c53624 + 104ce26 commit 342bbf8

10 files changed

Lines changed: 872 additions & 292 deletions

File tree

‎docs/.vitepress/config.mts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export default defineConfig({
6767
{ text: 'Property Validation', link: '/core-concepts/property-validation' },
6868
{ text: 'Inline Variables', link: '/core-concepts/inline-variables' },
6969
{ text: 'Magic Annotations', link: '/core-concepts/magic-annotations' },
70+
{ text: 'Violation Reporting & Auditing', link: '/core-concepts/violation-reporting' },
7071
]
7172
},
7273
{

‎docs/core-concepts/function-contracts.md‎

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ registerUser(-5, 'Alice', 'admin');
3939

4040
## Sensitive Parameter Value Masking (PHP 8.2+ `#[SensitiveParameter]`)
4141

42-
PHP 8.2 introduced the native `#[SensitiveParameter]` attribute to protect sensitive values (such as passwords, API keys, secret PINs, and bearer tokens) from leaking into stack traces and error monitoring logs (e.g. Sentry, Datadog, log aggregators).
42+
PHP 8.2 introduced the native `#[SensitiveParameter]` attribute to protect sensitive values (such as passwords, API keys, secret PINs, and bearer tokens) from leaking into stack traces and error monitoring logs (e.g. Sentry, Datadog, Bugsnag, CloudWatch).
4343

4444
TypePHP natively detects `#[SensitiveParameter]` on functions, class methods, constructors, promoted properties, and inherited interface contracts.
4545

4646
### Why Value Masking Matters
4747

48-
For standard parameters, TypePHP provides rich value inspection (such as `negative int (-5) given` or `empty string ('') given`). However, for sensitive parameters, printing the raw value would risk exposing credentials in exception messages and log files:
48+
For standard parameters, TypePHP provides rich, descriptive error messages to make debugging effortless (such as `negative int (-5) given` or `empty string ('') given`). However, for sensitive parameters, printing the raw value risks exposing confidential credentials in exception messages and log files:
4949

5050
```php
5151
use SensitiveParameter;
@@ -91,6 +91,85 @@ TypePHP enforces sensitive value masking across all declaration styles:
9191

9292
---
9393

94+
## Global Value Redaction (`redact_values => true`)
95+
96+
While `#[SensitiveParameter]` is ideal for targeted parameters, relying **solely** on attributes has three major limitations in enterprise and production codebases:
97+
98+
1. **Human Error & Coverage Gaps:** Developers often forget to annotate every sensitive parameter across hundreds of controllers, commands, and DTOs.
99+
2. **Unsupported Targets:** `#[SensitiveParameter]` only applies to method/function parameters. It **cannot** protect return values (`@return`), class property assignments (`$model->apiKey = ...`), or local inline variables (`/** @var ... */ $token = ...`).
100+
3. **PHP Version Restrictions:** `#[SensitiveParameter]` is only available on PHP 8.2+. Applications running on PHP 8.1 cannot use it.
101+
102+
To solve this, TypePHP provides the **Global Value Redaction** toggle (`'redact_values' => true`).
103+
104+
### Why Global Redaction is Useful
105+
106+
When enabled, TypePHP treats **every single parameter, return value, class property, and local variable** across your entire application as sensitive:
107+
108+
```
109+
# Without Redaction (Development Mode)
110+
TypeError: OrderService::checkout(): Argument $creditCard must be 'valid', string '4111-2222-3333-4444' given
111+
TypeError: Property User::$secretToken must be positive-int, negative int (-42) given
112+
TypeError: Return value must be of type positive-int, int (-999) returned
113+
114+
# With Global Redaction (Production / Staging / Audit Mode)
115+
TypeError: OrderService::checkout(): Argument $creditCard must be 'valid', string given
116+
TypeError: Property User::$secretToken must be positive-int, int given
117+
TypeError: Return value must be of type positive-int, int returned
118+
```
119+
120+
#### Key Benefits:
121+
* **Zero Code Modifications:** Protects legacy codebases, monolithic apps, and thousands of existing classes without requiring you to manually touch a single file or add attributes.
122+
* **PHP 8.1+ Compatibility:** Provides enterprise-grade credential masking even if your project has not upgraded to PHP 8.2 yet.
123+
* **Full-Spectrum Protection:** Redacts values across **all four** boundaries:
124+
- Function & method arguments (`@param`).
125+
- Function & method returns (`@return`).
126+
- Class property assignments (both instance and static `$this->prop` / `self::$prop`).
127+
- Inline `@var` variable reassignments and destructuring (`$var = ...`).
128+
* **Compliance Ready:** Essential for meeting strict data privacy standards (**HIPAA, GDPR, PCI-DSS, SOC 2**) by ensuring raw PII, payment details, or credentials are never written to error log aggregators (Sentry, Datadog) or TypePHP JSON audit reports.
129+
130+
### How to Enable Global Redaction
131+
132+
You can activate global redaction via configuration file, `composer.json`, or environment variable:
133+
134+
#### 1. In `typephp.php`:
135+
```php
136+
return [
137+
'redact_values' => true,
138+
];
139+
```
140+
141+
#### 2. In `composer.json`:
142+
```json
143+
{
144+
"extra": {
145+
"typephp": {
146+
"redact-values": true
147+
}
148+
}
149+
}
150+
```
151+
152+
#### 3. Via Environment Variable (Recommended for Production & CI):
153+
```bash
154+
export TYPEPHP_REDACT_VALUES=true
155+
```
156+
157+
---
158+
159+
## Comparison: `#[SensitiveParameter]` vs. `'redact_values'`
160+
161+
| Feature | `#[SensitiveParameter]` (Attribute) | `'redact_values' => true` (Global Toggle) |
162+
| :--- | :--- | :--- |
163+
| **Scope** | Granular (per-parameter opt-in) | Universal (project-wide) |
164+
| **Protected Targets** | Function/method parameters only | Parameters, returns, properties, local variables, reports |
165+
| **PHP Version** | PHP 8.2+ only | PHP 8.1, 8.2, 8.3, 8.4, 8.5+ |
166+
| **Code Changes Required** | Yes (manual attribute placement) | None (zero configuration overhead) |
167+
| **Ideal Environment** | Local development (detailed errors elsewhere, masked on secrets) | Staging, production, CI/CD audit runs, compliance-restricted systems |
168+
169+
> **Best Practice:** Use `#[SensitiveParameter]` in your source code for explicit self-documentation. In your **production** and **staging** environments, set `TYPEPHP_REDACT_VALUES=true` as a fail-safe catch-all to guarantee that no secret or PII ever leaks into your centralized logs.
170+
171+
---
172+
94173
## Native Parameter Nullability & DocBlock Refinement (`?Type` and `Type|null`)
95174

96175
In modern PHP, parameters frequently declare native nullable typehints:

0 commit comments

Comments
 (0)