Skip to content
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

Dev: using filter_* functions instead direct access to global variables #4968

Closed
WinterSilence opened this issue Jul 27, 2021 · 18 comments
Closed
Labels
breaking change Pull requests that may break existing functionalities dev waiting for info Issues or pull requests that need further clarification from the author

Comments

@WinterSilence
Copy link
Contributor

Example:

$_GET['foo'] = 1;
var_export($_GET); // ['foo' => 1]
var_export(filter_input_array(INPUT_GET)); // []

As you can see filter_input_array() return initial data, it's better use in CI because users (or vendor packages) can change globals before calling CI methods.

If there are no objections, then I will replace direct access to global variables everywhere. I will write sniff to fix this problem in future too.

@kenjis
Copy link
Member

kenjis commented Oct 25, 2021

As you can see filter_input_array() return initial data, it's better use in CI because users (or vendor packages) can change globals before calling CI methods.

filter_input_array(INPUT_GET)) returns the GET data that only in the incomming request?
If so, the change you suggrest is too restrictive for the current design of CI4,
and may break the exsisting test code.

The current test/product code is heavily depends on the suprer global variables.

@WinterSilence
Copy link
Contributor Author

WinterSilence commented Oct 25, 2021

@kenjis please see manual, it's only example to display: filter_input_array() returning initial data without changes.

@kenjis
Copy link
Member

kenjis commented Oct 25, 2021

Sorry, I know INPUT_GET is example.

If we use filter_input_array(), we can't change the data., right?
If we can't change the data, we can't write test code.

@WinterSilence
Copy link
Contributor Author

@kenjis
Copy link
Member

kenjis commented Oct 25, 2021

@WinterSilence What is your point?

@WinterSilence
Copy link
Contributor Author

@ kenjis one test = one request, change globals in test is bad practice

@kenjis
Copy link
Member

kenjis commented Oct 25, 2021

@WinterSilence CI4 has a lot of (test/production) code that depends on super global variables.
If we add this change, will all our existing tests pass?

@WinterSilence
Copy link
Contributor Author

WinterSilence commented Oct 25, 2021

@kenjis i don't check all test, but if they are written correctly, then it must be work fine.

@kenjis
Copy link
Member

kenjis commented Oct 26, 2021

I confirmed the behavior.

<?php
namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {
        $_GET['foo'] = 1;
        d($_GET); // ['foo' => 1]
        d(filter_input_array(INPUT_GET)); // []
    }

}

Screenshot 2021-10-26 10 20 39

@kenjis kenjis added the breaking change Pull requests that may break existing functionalities label Oct 26, 2021
@WinterSilence
Copy link
Contributor Author

@kenjis

but if they are written correctly, then it must be work fine.

it's not correct tests.

You can keep "pure" request data if any user or vendor library can change they. I didn't say that the right decisions are easy. And I didn't say that it's enough to replace global variables with a call filter_*().

@kenjis
Copy link
Member

kenjis commented Oct 27, 2021

@WinterSilence
Copy link
Contributor Author

WinterSilence commented Oct 27, 2021

@kenjis
Copy link
Member

kenjis commented Oct 27, 2021

https://phpunit.readthedocs.io/en/9.5/configuration.html#appendixes-configuration-php-post

But super globals change per test. If we can't change them, we can't write enough test cases.

mosk request class

Do you mean mock the class to test? And how do you mock the IncomingRequest?

https://blog.cloudflare.com/using-guzzle-and-phpunit-for-rest-api-testing/

It is not unit testing. We need fast unit tests.

@WinterSilence
Copy link
Contributor Author

WinterSilence commented Oct 27, 2021

@kenjis One test = one request - see @backupglobals

  1. IncomingRequest must use $this->globals['...'] instead direct access to $_...
    protected function populateGlobals(string $method)
    pass global variables by $config into constructor and initialize $this->globals
  2. IncomingRequest use getenv('QUERY_STRING') - you can call setenv('QUERY_STRING', '...')

It is not unit testing.

What do you think this is?

@kenjis
Copy link
Member

kenjis commented Oct 28, 2021

I think injecting the superglobals data into IncomingRequest is the way to go.
2. is not good. It is essentially the same as using superglobals in the IncomingRequest.
Superglobals or environment variables. And QUERY_STRING is only for $_GET.
What about other data?

By the way, what do you want to achieve with this filter_* function?
For example, Services::request() returns the IncomingRequest like this.
Will your goal be achieved?

$request->setGlobal('get', filter_input_array(INPUT_GET));

@kenjis kenjis added the waiting for info Issues or pull requests that need further clarification from the author label Oct 29, 2021
@kenjis
Copy link
Member

kenjis commented Apr 15, 2022

No feedback. So I close.

I'm still not sure this should be implemented or not, but before implementing this we need to rework all classes that handles the super globals.
These classes should be injected super global values as constructor parameters, not using super globals inside.

@kenjis kenjis closed this as completed Apr 15, 2022
@WinterSilence
Copy link
Contributor Author

@kenjis sorry, miss your answer

By the way, what do you want to achieve with this filter_* function?

I want keep initial data of request. Now request data can be changed by vendor/user code, framework must ignore this changes. If developer wants to make changes, then he must do so by explicitly changing the corresponding method or property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Pull requests that may break existing functionalities dev waiting for info Issues or pull requests that need further clarification from the author
Projects
None yet
Development

No branches or pull requests

2 participants