Closed ghostwriter closed 1 year ago
I'm sorry but this isn't the proper avenue for such a request;
My mistake, could you point me to the proper avenue for such a request?
also, similar modifications were suggested in the past and shut down, since it doesn't make sense to do it at this level (see comment below)
I understand, but this is a new/separate argument for why it makes sense to do it in this manner, at this level; now that we have IDEs and Static Analysis tools that support generic typehints.
This will allow users to use code completion in IDEs and static analyzers will be able to correctly determine the types and provide proper feedback.
or are too minimal to have a new major.
The reason for a new major is due to:
PHP <7.4
in composer.json
get
method has changed.
mixed
return type (available since PHP 8.0)waiting on this MR. Any ETA when this will get merged ?
If accepted, this PR will:
Bump minimum supported PHP version to 8.0
Declare strict types on the Exception interfaces
Add "mixed" return type to get method
Add generics to "ContainerInterface"; "get" method
removed generics because php-fig does not have a policy on generics in specs.
**The types of the `ContainerInterface` can be specified when you’re type-hinting:** ```php /** * @template T of mixed */ interface ContainerInterface { /** * @param string $id * * @return T */ public function get(string $id): mixed; } ``` **If we don’t want our class to be generic:** ```php //eg. 1 /** * @implements ContainerInterface
*/
class IntContainer implements ContainerInterface {
public function get(string $id): mixed { return 42; }
}
/**
* @param ContainerInterface $container
*/
function integers(ContainerInterface $container): void {
$container->get('integer'); // 42
}
integers(new IntContainer());
```
```php
//eg. 2
/**
* @implements ContainerInterface
*/
class StringContainer implements ContainerInterface {
public function get(string $id): mixed { return '#BLM'; }
}
/**
* @param ContainerInterface $container
*/
function strings(ContainerInterface $container): void {
$container->get('string'); // #BLM
}
strings(new StringContainer());
```
**Preserving the genericness is done by repeating the same `@template` tags above the child class and passing it to `@extends` and `@implements` tags:**
```php
/**
* @template T of mixed
* @implements ContainerInterface
*/
class GenericContainer implements ContainerInterface {
public function get(string $id): mixed {
/** @var array $items*/
$items = [new \stdClass, 42, '#BLM', null];
return $items[array_rand($items)];
}
}
/**
* @template T
* @param ContainerInterface $container
*/
function containers(ContainerInterface $container): void {
$container->get('mixed'); // 42 | #BLM | \stdClass | null
}
containers(new GenericContainer());
containers(new StringContainer());
containers(new IntContainer());
```
https://psalm.dev/r/e91e467ce4
https://phpstan.org/r/ef49b3fd-3645-4e34-ac60-5b1bf4a7b474