A PHPStan extension for the thesis packages: precise types where the generics of PHPDoc cannot express them. One extension for all packages: it does nothing for a package you do not use.
composer require --dev thesis/phpstan-extensionWith phpstan/extension-installer that is all. Otherwise include the config:
includes:
- vendor/thesis/phpstan-extension/extension.neonRequires PHPStan 2.3+.
all() and allSettled() of an array shape of callables return the shape of their results, a type per key:
use function PHPStan\Testing\assertType;
use function Thesis\Async\all;
use function Thesis\Async\allSettled;
$report = all([
'sales' => loadSales(...),
'costs' => loadCosts(...),
]);
assertType('array{sales: Sales, costs: Costs}', $report);
// without the extension: array<'sales'|'costs', Sales|Costs>
[$results, $errors] = allSettled(['sales' => loadSales(...), 'costs' => loadCosts(...)]);
assertType('array{sales?: Sales, costs?: Costs}', $results);
assertType('array{sales?: Throwable, costs?: Throwable}', $errors);all() and map() keep what is known of the keys of their input: a shape gives a shape, a list gives a list,
a non-empty array a non-empty one.
use function Thesis\Async\map;
/** @var non-empty-list<string> $urls */
assertType('non-empty-list<Response>', map($urls, 10, fetch(...)));
// without the extension: array<int<0, max>, Response>
assertType('array{sales: Report, costs: Report}', map(['sales' => $sales, 'costs' => $costs], 2, report(...)));
// without the extension: array<'sales'|'costs', Report>Without such a key or for a generator, the type of the PHPDoc stays. race(), timeout(), group() and the others
need no extension: their generics are precise.