Repository: codeigniter4/CodeIgniter4 (PHP)

Issue:
Bug: CLIRequest::getCookie() does not exist
### PHP Version

8.0

### CodeIgniter4 Version

4.2.7

### CodeIgniter4 Installation Method

Composer (using `codeigniter4/appstarter`)

### Which operating systems have you tested for this bug?

Windows, Linux

### Which server did you use?

cli

### Database

_No response_

### What happened?

When running a controller through CLI if the controller runs get_cookie() an error occurs.

### Steps to Reproduce

Create a controller:

	public function index()
	{
		$model = new TestModel();

                helper('cookie');
                $remember = get_cookie('remember');

		return 'success';
	}

run  php C:\xampp\portal\public\index.php public/test/index

### Expected Output

expected 'success'

### Anything else?

I tried the set_cookie() method to see if it causes error and it did not.

I understand you cant set/retrieve cookies via CLI but this causes errors using Myth-Auth and calling a controller. It checks the remember me cookie and throws the error:

```
[Error]

Call to undefined method CodeIgniter\HTTP\CLIRequest::getCookie()

at SYSTEMPATH\Helpers\cookie_helper.php:81
```

As a quick work around I copied the `IncomingRequest::getCookie()` method to `CLIRequest` and things are working.

Perhaps `cookie_helper`should not call `getCookie()` on a CLI request.


Discussion:
> I understand you cant set/retrieve cookies via CLI but this causes errors using Myth-Auth and calling a controller. It checks the remember me cookie and throws the error:

Yes, you cannot use cookies via CLI.

What's the exact problem  using Myth-Auth and calling a controller?
Why do you need to run the controller via CLI?
Can you get the correct result when running the controller via CLI  if no error ocurrs?

Myth-Auth calls authentication check and calls getCookie.

My controllers output json responses that can be used for webpage but responses are output to a file when called from CLI. Its how I do multithreading of jobs. A collector looks for these files and reports status for individual job.

It makes sense to not throw an error. PHP's cookie methods do not throw errors on CLI.
>Can you get the correct result when running the controller via CLI if no error ocurrs?

The error goes away when adding the getCookie() method to CLIRequest.

With Myth-Auth any request calls the get_cookie() method. Perhaps it could be a Myth-Auth issue but I still think that calling get_cookie() on a CLI request shouldn't cause an error. It should just return null.
Myth-Auth LocalAuthenticator::

```php
    public function check(): bool
    {
        if ($this->isLoggedIn())
        {
            // Do we need to force the user to reset their password?
            if ($this->user && $this->user->force_pass_reset)
            {
                throw new RedirectException(route_to('reset-password') .'?token='.$this->user->reset_hash);
            }

            return true;
        }

        // Check the remember me functionality.
        helper('cookie');
        $remember = get_cookie('remember');

        if (empty($remember))
        {
            return false;
        }
```

> Myth-Auth calls authentication check and calls getCookie.

Are you saying that the error occurs because the Myth Auth controller filter works when running via CLI?

Personally, I think CLIRequest should have all the methods that IncommingRequest has.
In this case, `getCookie()` that returns null.

>Are you saying that the error occurs because the Myth Auth controller filter works when running via CLI?

Yes. I have everything locked down except a public route. But even calling the public route its running this check() method. I can't recall all the details of how I set it up though.

>Personally, I think CLIRequest should have all the methods that IncommingRequest has.

I agree! This is why I made the issue here and not on Myth-Auth. The set_cookie() method doesn't seem to have any issues because its on the response object. By the same logic you would need to cut the headers out of the response for a CLIResponse. But this is unnecessary.

We should add the getCookie() method returning null.

It seems a lot of these methods could be on the Request class and then override where needed. This might be better than adding them all to CLIRequest.

I sent PR to add this method to CLIRequest for now.
isAJAX() is another method that could potentially cause errors. The scripts that I have the cookie error with I use for both CLI and ajax. I don't use the isAJAX() method though so haven't had the error but its a possibility. The scripts I have are used to sync data between different datasources and my data warehouse. I have some applications built on top of the data warehouse that depend on this data. I have regular data syncs that run via CLI on a schedule. Sometimes however, when using the application you might need to run a sync on demand to update the data. I do this with ajax calling the same scripts. The scripts output a json response which is used both for logging job status from CLI as well as in ajax.
> It seems a lot of these methods could be on the Request class and then override where needed. This might be better than adding them all to CLIRequest.

It is not clear what the current Request class is.
Or it will have too many responsibilities. See #6573




Patch:
diff --git a/system/HTTP/CLIRequest.php b/system/HTTP/CLIRequest.php
index e81ae9cabbd9..64c0f0adae6d 100644
--- a/system/HTTP/CLIRequest.php
+++ b/system/HTTP/CLIRequest.php
@@ -272,6 +272,20 @@ public function getGetPost($index = null, $filter = null, $flags = null)
         return $this->returnNullOrEmptyArray($index);
     }
 
+    /**
+     * This is a place holder for calls from cookie_helper get_cookie().
+     *
+     * @param array|string|null $index  Index for item to be fetched from $_COOKIE
+     * @param int|null          $filter A filter name to be applied
+     * @param mixed             $flags
+     *
+     * @return array|null
+     */
+    public function getCookie($index = null, $filter = null, $flags = null)
+    {
+        return $this->returnNullOrEmptyArray($index);
+    }
+
     /**
      * @param array|string|null $index
      *


Test patch:
diff --git a/tests/system/HTTP/CLIRequestTest.php b/tests/system/HTTP/CLIRequestTest.php
index b294d1621b2b..cbec9b22a33e 100644
--- a/tests/system/HTTP/CLIRequestTest.php
+++ b/tests/system/HTTP/CLIRequestTest.php
@@ -616,4 +616,11 @@ public function testGetLocale()
     {
         $this->assertSame('en', $this->request->getLocale());
     }
+
+    public function testGetCookie()
+    {
+        $this->assertNull($this->request->getCookie('TESTY'));
+
+        $this->assertSame($this->request->getCookie(), []);
+    }
 }