-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: complete Superglobals implementation
#9858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.7
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I patched the latest commit 4.7 - there is an error.
1) CodeIgniter\Models\PaginateModelTest::testMultiplePager
Failed asserting that '<!-- DEBUG-VIEW START 1 SYSTEMPATH/Pager/Views/default_full.php -->\n
...
<!-- DEBUG-VIEW ENDED 1 SYSTEMPATH/Pager/Views/default_full.php -->\n
' [ASCII](length: 843) contains "?page_valid=1"" [ASCII](length: 14).
/development/tests/system/Models/PaginateModelTest.php:103A lot of changes - I couldn't see them all at once.
UPD: Fixes #9392
| public function __construct( | ||
| ?array $server = null, | ||
| ?array $get = null, | ||
| ?array $post = null, | ||
| ?array $cookie = null, | ||
| ?array $files = null, | ||
| ?array $request = null, | ||
| ) { | ||
| $this->server = $server ?? $_SERVER; | ||
| $this->get = $get ?? $_GET; | ||
| $this->post = $post ?? $_POST; | ||
| $this->cookie = $cookie ?? $_COOKIE; | ||
| $this->files = $files ?? $_FILES; | ||
| $this->request = $request ?? $_REQUEST; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use constructor property promotion here and instead of null default we use directly the superglobals.
| public function __construct( | |
| ?array $server = null, | |
| ?array $get = null, | |
| ?array $post = null, | |
| ?array $cookie = null, | |
| ?array $files = null, | |
| ?array $request = null, | |
| ) { | |
| $this->server = $server ?? $_SERVER; | |
| $this->get = $get ?? $_GET; | |
| $this->post = $post ?? $_POST; | |
| $this->cookie = $cookie ?? $_COOKIE; | |
| $this->files = $files ?? $_FILES; | |
| $this->request = $request ?? $_REQUEST; | |
| } | |
| public function __construct( | |
| private array $server = $_SERVER, | |
| private array $get = $_GET, | |
| private array $post = $_POST, | |
| private array $cookie = $_COOKIE, | |
| private array $files = $_FILES, | |
| private array $request = $_REQUEST, | |
| ) { | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly, we can't do that with superglobals, but I introduced property promotion.
system/Superglobals.php
Outdated
| * | ||
| * @return array<array-key, mixed>|float|int|string|null | ||
| */ | ||
| public function server(string $key): array|float|int|string|null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the getters, how about the idea of introducing a default parameter, liked mixed $default = null so that on invocation we can define already the default value in case missing instead of doing ?? <default>?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
system/Superglobals.php
Outdated
| private array $files; | ||
|
|
||
| /** | ||
| * @param array<string, array|float|int|string>|null $server |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's currently a disconnect in this PR for the types a superglobal can set vs the types it can get. The set types is currently less precise than the get types. To make sync easier, I suggest to add phpstan-types in the class declaration phpdoc for each superglobal, something like:
/**
* @phpstan-type server_items array<array-key, mixed>|float|int|string
* etc.
*/
class SuperglobalsThen here and the setters and getters below we can simply plug the phpstan type, like so:
| * @param array<string, array|float|int|string>|null $server | |
| * @param array<string, server_items>|null $server |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done... I think. Please verify when you have time.
|
@neznaika0 What do you mean by saying "I patched the latest commit 4.7"? Have you cloned this branch and run tests locally? This branch is working on the latest Anyway, I found the potential issue and hopefully fixed it. The strange thing is that it didn't pop up during tests. |
|
@michalsn I use few git features - it's easy to lose changes. I downloaded https://github.com/codeigniter4/CodeIgniter4/pull/9858.patch and I applied it on 4.7 at local. |
|
@neznaika0 Ok, please verify if the issue still exists after recent changes - thanks. |
| * @return server_items|null | ||
| */ | ||
| public function server(string $key): array|float|int|string|null | ||
| public function server(string $key, mixed $default = null): array|float|int|string|null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have an mixed argument, since this should match server_items (and other places), you can add PHPDoc.
I'm sorry if this looks like a complication. You can leave it as it is
|
Good. Error is missing |
Description
This PR completes the implementation of the
Superglobalsclass and migrates the entire codebase to use it consistently.I started work on this about a month ago, and it turned out to be a tedious task. The scope of changes is relatively large, as this migration could not be done incrementally.
Since this class is internal and not intended for end-user consumption, no changelog entry has been added.
See: #7866
Checklist: