Skip to content

Commit de29fb8

Browse files
committed
Add documentation for native parameter nullability and DocBlock refinement
1 parent 86f1b1e commit de29fb8

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,71 @@ registerUser(-5, 'Alice', 'admin');
3737
3838
---
3939

40+
## Native Parameter Nullability & DocBlock Refinement (`?Type` and `Type|null`)
41+
42+
In modern PHP, parameters frequently declare native nullable typehints:
43+
* **PHP 7.1+ nullable shorthand:** `?array $tags = null`
44+
* **PHP 8.0+ native union syntax:** `int|null $id = null` or `array|null $options = null`
45+
46+
### The Inherent Syntax Limitation in Native PHP
47+
48+
PHP's native type system cannot express inner collection or refinement types in method signatures. You cannot natively write `?string[] $tags = null` or `?positive-int $id = null` in PHP.
49+
50+
Because of this language limitation, developers in major enterprise libraries (including **Symfony**, **Laravel**, and **Doctrine**) universally follow this convention:
51+
1. They declare the **outer nullability** natively in PHP: `?array $groups = null`
52+
2. They use the **DocBlock strictly to describe the inner elements**: `/** @param string[] $groups */`
53+
54+
```php
55+
// Common pattern in Symfony and Laravel:
56+
/**
57+
* @param string[] $groups // DocBlock omits |null because native PHP already allows null!
58+
*/
59+
public function configure(?array $groups = null): void
60+
{
61+
// ...
62+
}
63+
```
64+
65+
### How TypePHP Resolves Native Nullability (The Pragmatic Merge)
66+
67+
By default (`'respect_native_nullability' => true`), TypePHP merges native parameter nullability with the DocBlock's inner type:
68+
69+
> **Native `?array`** + **DocBlock `string[]`** $\rightarrow$ **Effective Contract: `?string[]` (`string[]|null`)**
70+
71+
This provides complete type safety without breaking real-world code:
72+
73+
This provides complete type safety without breaking real-world code:
74+
* **Passing `null` (or omitting default argument):** Accepted cleanly.
75+
* **Passing `['admin', 'editor']`:** Accepted, and every element is verified as a valid string.
76+
* **Passing `[12345]`:** Rejected with `TypeError: Argument $groups[0] must be of type string, int (12345) given`.
77+
* **Passing `'not_an_array'`:** Rejected with `TypeError: Argument $groups must be of type array, string given`.
78+
79+
### Supported Native Nullability Syntaxes
80+
81+
TypePHP automatically detects and respects all native PHP nullability declarations:
82+
83+
| Native PHP Signature | DocBlock Annotation | Effective Evaluated Contract |
84+
| :--- | :--- | :--- |
85+
| `?array $tags = null` | `@param string[] $tags` | **`?string[]`** |
86+
| `int\|null $id = null` | `@param positive-int $id` | **`?positive-int`** |
87+
| `array\|null $opts = null` | `@param array{timeout: int} $opts` | **`array{timeout: int}\|null`** |
88+
| `?int $score = null` | `@param int<1, 100> $score` | **`int<1, 100>\|null`** |
89+
90+
### Strict Pedantic Mode (`'respect_native_nullability' => false`)
91+
92+
If your project requires 100% strict DocBlock auditing where DocBlocks are treated as the absolute literal law, set `'respect_native_nullability' => false` in `typephp.php`:
93+
94+
```php
95+
// typephp.php
96+
return [
97+
'respect_native_nullability' => false,
98+
];
99+
```
100+
101+
In strict mode, if a native parameter is `?array $tags = null` but the DocBlock says `@param string[] $tags` (omitting `|null`), passing `null` will be strictly rejected with a `TypeError`.
102+
103+
---
104+
40105
## Tooling Annotation Priority Hierarchy (`@phpstan-*` > `@psalm-*` > `@*`)
41106

42107
Modern PHP packages and frameworks (such as **Doctrine Collections**, **Symfony**, and **Laravel**) frequently declare both broad IDE-fallback annotations and strict static analysis contracts on the exact same method signature:

0 commit comments

Comments
 (0)