php-fig / container

MIT License
9.95k stars 53 forks source link

Prepare for v3 release #48

Closed ghostwriter closed 1 year ago

ghostwriter commented 1 year ago

If accepted, this PR will:

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

ghostwriter commented 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:

deepsidhu1313 commented 1 year ago

waiting on this MR. Any ETA when this will get merged ?