You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 1cc1d73
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/core-concepts/inline-variables.md
+40-1Lines changed: 40 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Inline Variables (`@var`)
2
2
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.
4
4
5
5
---
6
6
@@ -30,6 +30,45 @@ TypePHP validates all supported type categories on inline variable assignments:
30
30
31
31
---
32
32
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
+
33
72
## Single-Line vs. Multi-Line Annotations
34
73
35
74
TypePHP supports both single-variable single-line docblocks and multi-variable docblocks (such as for `list()` array destructuring or multiple assignments):
|**`'params'`**|`true`| Enforces parameter `@param` contracts on functions and methods. |
186
197
|**`'returns'`**|`true`| Enforces return `@return` contracts on functions and methods. |
187
198
|**`'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. |
188
200
|**`'respect_ignore_tags'`**|`true`| Respects `@typephp-ignore` and `@typephp-ignore-file` tags. Set to `false` in CI/CD to force audit checks. |
189
201
|**`'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. |
190
202
|**`'magic_properties'`**|`true`| Enforces class-level `@property`, `@property-read`, and `@property-write` annotations on dynamic writes (`__set`). |
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**:
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:
0 commit comments