Skip to content

Commit 1cc1d73

Browse files
authored
Merge pull request #1 from typephp-php/internal-improvements-23
Update Docs
2 parents 2b7702b + 743705f commit 1cc1d73

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

‎docs/core-concepts/inline-variables.md‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Inline Variables (`@var`)
22

3-
While parameter and return contracts protect function boundaries, inline `@var` annotations enforce type safety on local variable assignments, reassignments, and direct return statements inside function bodies or PHP scripts.
3+
While parameter and return contracts protect function boundaries, inline `@var` annotations enforce type safety on local variable assignments, reassignments, compound operations, and direct return statements inside function bodies or PHP scripts.
44

55
---
66

@@ -30,6 +30,45 @@ TypePHP validates all supported type categories on inline variable assignments:
3030

3131
---
3232

33+
## Compound Assignments & Increment/Decrement Support
34+
35+
TypePHP automatically tracks and validates inline `@var` types when variables or object properties are modified using compound assignment operators (`*=`, `+=`, `-=`, `/=`, `%=`, `.=`, `**=`, etc.) or increment/decrement operators (`++`, `--`):
36+
37+
```php
38+
<?php
39+
40+
/** @var positive-int $score */
41+
$score = 10;
42+
43+
$score += 5; // Valid (15)
44+
45+
$score *= -2;
46+
// Throws: TypeError: Variable $score must be of type positive-int, negative int (-30) given
47+
48+
/** @var int<1, 5> $count */
49+
$count = 5;
50+
51+
$count++;
52+
// Throws: TypeError: Variable $count must be of type int<1, 5>, int (6) given
53+
```
54+
55+
This applies identically to class properties and static properties:
56+
```php
57+
class GameSession
58+
{
59+
/** @var positive-int */
60+
public int $score = 10;
61+
62+
public function penalty(): void
63+
{
64+
$this->score *= -1;
65+
// Throws: TypeError: Property GameSession::$score must be of type positive-int
66+
}
67+
}
68+
```
69+
70+
---
71+
3372
## Single-Line vs. Multi-Line Annotations
3473

3574
TypePHP supports both single-variable single-line docblocks and multi-variable docblocks (such as for `list()` array destructuring or multiple assignments):

‎docs/getting-started/configuration.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ return [
4848
*/
4949
'strict_return_generic_invariance' => true,
5050

51+
/*
52+
|--------------------------------------------------------------------------
53+
| Vendor Boundary Only Enforcement
54+
|--------------------------------------------------------------------------
55+
| When true (default), whitelisted vendor packages (e.g. Illuminate\Collections)
56+
| only enforce type contracts on calls originating from application code (included paths).
57+
| Internal vendor-to-vendor or vendor-self calls bypass strict enforcement.
58+
| Set to false for strict pedantic enforcement across all vendor internals.
59+
*/
60+
'vendor_boundary_only' => true,
61+
5162
/*
5263
|--------------------------------------------------------------------------
5364
| Respect Ignore Docblock Tags
@@ -185,6 +196,7 @@ return [
185196
| **`'params'`** | `true` | Enforces parameter `@param` contracts on functions and methods. |
186197
| **`'returns'`** | `true` | Enforces return `@return` contracts on functions and methods. |
187198
| **`'strict_return_generic_invariance'`** | `true` | Enforces strict generic return invariance matching PHPStan Level MAX (e.g. returning `Collection<Dog>` where `Collection<Animal>` is expected is rejected unless `@template-covariant` or `<covariant Animal>` is specified). Set to `false` (pragmatic mode) for frameworks (Laravel, Shopware) where collection classes omit covariance annotations. |
199+
| **`'vendor_boundary_only'`** | `true` | When `true` (default), whitelisted vendor classes (e.g. `Illuminate\Support\Collection`) only enforce type contracts when called from application code (`app/**`, `src/**`, `tests/**`). Calls originating from excluded vendor files or internal self-calls bypass strict checking. Set to `false` for strict pedantic auditing across all vendor code. |
188200
| **`'respect_ignore_tags'`** | `true` | Respects `@typephp-ignore` and `@typephp-ignore-file` tags. Set to `false` in CI/CD to force audit checks. |
189201
| **`'respect_native_nullability'`** | `true` | When `true` (default), permits `null` if native PHP explicitly declares nullable syntax (`?Type` or `Type\|null = null`) even if omitted in the DocBlock. Set to `false` for strict pedantic enforcement. |
190202
| **`'magic_properties'`** | `true` | Enforces class-level `@property`, `@property-read`, and `@property-write` annotations on dynamic writes (`__set`). |
@@ -200,6 +212,34 @@ return [
200212

201213
---
202214

215+
## Vendor Boundary Isolation (`vendor_boundary_only`)
216+
217+
When you whitelist a third-party vendor class (such as `Illuminate\Support\Collection` or a Symfony component), that class is often called thousands of times by the framework's own internal subsystems (e.g. routing, service containers, template compilers, or other third-party packages).
218+
219+
Frameworks often rely on dynamic PHP engine features and loose typing internally (e.g. passing temporary array lists, loose keys, or untyped closures), which can cause false-positive type errors during application boot or test setup.
220+
221+
### How Boundary Isolation Works
222+
223+
TypePHP solves this by acting as an **Application Boundary Airlock**:
224+
225+
```
226+
[Your Application Code] ──calls──► [Vendor Class] ──► TypePHP STRICTLY ENFORCES types!
227+
[Vendor / Framework] ──calls──► [Vendor Class] ──► TypePHP BYPASSES (Native execution speed)
228+
[Vendor Internal Self] ──calls──► [Vendor Class] ──► TypePHP BYPASSES (Internal implementation detail)
229+
```
230+
231+
1. **Calls from Application Code:** When your code in `app/`, `src/`, or `tests/` calls a method on a whitelisted vendor class (e.g. `$collection->add(4)` on a `Collection<int, string>`), TypePHP **strictly validates all parameters and returns**.
232+
2. **Calls from Vendor Code:** When an excluded vendor package or framework internal (e.g. Spatie, Inertia, or Laravel's Application Kernel) calls that same method, TypePHP **instantly short-circuits** with zero overhead, allowing the framework to operate with native speed and flexibility.
233+
3. **Happy-Path Zero Overhead:** TypePHP resolves caller origins in sub-microseconds with $O(1)$ path memoization, ensuring your test suite and local environment run at near-native speeds.
234+
235+
To force TypePHP to enforce strict types across all vendor internals (pedantic mode for package authors or audits), set:
236+
237+
```php
238+
'vendor_boundary_only' => false,
239+
```
240+
241+
---
242+
203243
## Inline Variable Categories Reference (`inline_vars`)
204244

205245
You can toggle specific categories of local `@var` variable checks without disabling function boundary contracts:

0 commit comments

Comments
 (0)