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

docs: fix PHPDoc types in Filter class. #7451

Merged
merged 3 commits into from
May 9, 2023

Conversation

ping-yee
Copy link
Contributor

Description
See #6310

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@ping-yee
Copy link
Contributor Author

ping-yee commented Apr 25, 2023

I don't change the return type in run() function in Filters.php and two functions of FilterInterface.php because I am not sure that Is there any possibility that these methods will param/return types other than object|null|void?

@MGatner
Copy link
Member

MGatner commented Apr 26, 2023

I think the filter return values need to remain mixed because the docs specify they can return any non-empty value to short circuit.

https://codeigniter4.github.io/CodeIgniter4/incoming/filters.html#creating-a-filter

@ping-yee
Copy link
Contributor Author

ping-yee commented Apr 26, 2023

Ok I got it. Thanks you @MGatner !
But I have a idea that If they can return any non-empty value, can we put all the return type object|resource|array|string|int|float|bool|null on their param/return type?

@kenjis
Copy link
Member

kenjis commented Apr 26, 2023

I don't think a filter returns resource.

system/Filters/Filters.php Outdated Show resolved Hide resolved
@kenjis
Copy link
Member

kenjis commented Apr 26, 2023

Personally, I would like to reduce the number of allowed types like the following.
I think for stopping filters only non-empty string is sufficient. But it is a breaking change.

--- a/system/Filters/FilterInterface.php
+++ b/system/Filters/FilterInterface.php
@@ -31,7 +31,7 @@ interface FilterInterface
      *
      * @param array|null $arguments
      *
-     * @return mixed
+     * @return RequestInterface|ResponseInterface|string|void
      */
     public function before(RequestInterface $request, $arguments = null);
 
@@ -43,7 +43,7 @@ interface FilterInterface
      *
      * @param array|null $arguments
      *
-     * @return mixed
+     * @return ResponseInterface|void
      */
     public function after(RequestInterface $request, ResponseInterface $response, $arguments = null);
 }

@ping-yee
Copy link
Contributor Author

I think for stopping filters only non-empty string is sufficient. But it is a breaking change.

I changed the PHPDoc return type first, then I will open another PR to handle this.

@ping-yee
Copy link
Contributor Author

@kenjis I want to check what will need to be change in Filter class if we want to stopping filters only non-empty string.

@kenjis
Copy link
Member

kenjis commented Apr 27, 2023

If we change the code, It is a breaking change.
https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md#breaking-changes
So I think it is better to make non-empty values except string deprecated (stating that in the user guide).
Then in the next or next next minor versions we permit only non-empty string.

@ping-yee
Copy link
Contributor Author

Then in the next or next next minor versions we permit only non-empty string.

I am willing to help with this BC feature in the future!

So I think it is better to make non-empty values except string deprecated (stating that in the user guide).

So do I need to modify the user guide in this PR? Or need to another PR to handle this pre-work.

And I wonder if there is anything else that needs to be modified in this PR.

@@ -31,7 +31,7 @@ interface FilterInterface
*
* @param array|null $arguments
*
* @return mixed
* @return RequestInterface|ResponseInterface|string|void
Copy link
Member

@kenjis kenjis Apr 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit odd, but correct because now we can return any non-empty value.

Suggested change
* @return RequestInterface|ResponseInterface|string|void
* @return mixed|RequestInterface|ResponseInterface|void

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with this change, but I would not mind if we left it as it was (only mixed).
I don't have a strong opinion.

@kenjis
Copy link
Member

kenjis commented Apr 30, 2023

I agree with the future change to only allow non-empty strings, but I don't know if the team would agree to that.

@codeigniter4/core-team Any comment?

Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to introduce these return-type changes, can we do it once, without breaking it down into further releases? Are we afraid that this would break people's code entirely?

I was thinking about a scenario where this could break the app for good, and I couldn't find anything.

@@ -31,7 +31,7 @@ interface FilterInterface
*
* @param array|null $arguments
*
* @return mixed
* @return RequestInterface|ResponseInterface|string|void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with this change, but I would not mind if we left it as it was (only mixed).
I don't have a strong opinion.

@ping-yee
Copy link
Contributor Author

ping-yee commented May 1, 2023

@kenjis @michalsn @MGatner I have been change the return type mixed in FilterInterface, review please.

I agree with the future change to only allow non-empty strings.

I will open the other PR to handle this after this PR is merged.

If we want to introduce these return-type changes, can we do it once, without breaking it down into further releases? Are we afraid that this would break people's code entirely?

I think the better way is add a notice at user guide page first and may be update non-empty string in next version as Kenjis says.

@kenjis
Copy link
Member

kenjis commented May 1, 2023

@michalsn Changing the return type means changing the if condition:

// Ignore an empty result
if (empty($result)) {
continue;
}
return $result;

This is a breaking change.

@michalsn
Copy link
Member

michalsn commented May 1, 2023

@kenjis If we want to change the return type, we can't avoid a breaking change.
I would just target this to 4.4 and that's it.

Adding a note to the user guide first and then changing it in the future gives us nothing. Developers who already have built their apps most likely won't see the note. They will notice it when we introduce a change into the code - hopefully reading the release notes.

The real question is if other developers take advantage of the current implementation. Because for me, that's the only real problem.

@kenjis
Copy link
Member

kenjis commented May 2, 2023

The real question is if other developers take advantage of the current implementation. Because for me, that's the only real problem.

I don't know. But it is documented as non-empty value, some devs may be using true or something. I don't think many devs actually use this feature.

It is possible that this change could actually break existing applications. So I don't strongly advocate making the change.

However, I don't think there is a need to allow any non-empty value. We don't even use the return value itself, so if we want to stop later filters, I think one type value for it is sufficient.

@ping-yee
Copy link
Contributor Author

ping-yee commented May 8, 2023

So... Is there a consensus here?

@michalsn @kenjis @MGatner

Or can we have a notice for user guide first and change return type to non-empty string in 4.4?

I think have a notice in user guide may not make the developers who have been implemented to make changes, but it can prevent future devs who need this method from defining other types.

@michalsn
Copy link
Member

michalsn commented May 8, 2023

The current changes seem fine.

I don't see a good reason to change the return signature for FilterInterface and introduce a BC break for that reason.

I'm okay with adding a note to the user guide to return only non-empty strings.

@kenjis
Copy link
Member

kenjis commented May 9, 2023

This PR is okay.

@kenjis kenjis merged commit 42b174f into codeigniter4:develop May 9, 2023
Copy link
Collaborator

@iRedds iRedds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that PSR-15 was already 5 years ago, the use of the approach applied in CI surprises me a little.
In addition, the implementation of filter processing directly contradicts the description of filter methods.

@@ -31,7 +31,7 @@ interface FilterInterface
*
* @param array|null $arguments
*
* @return RequestInterface|ResponseInterface|string|void
* @return mixed
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the return type should be like this. Which corresponds to the description of the method.
That is, either we complete the processing of the request by returning a response. Or further processing of the request continues and the method returns nothing.

Suggested change
* @return mixed
* @return ResponseInterface|void

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to stop the later filters?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not understand the question.

Copy link
Contributor Author

@ping-yee ping-yee May 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will return other types that are used for stop later filters.

https://codeigniter4.github.io/CodeIgniter4/incoming/filters.html#before-filters

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any filter that returns a non-empty result dequeues filters that, for example, are responsible for access.

I think this is a stupid idea.

But if such an implementation is needed (to shoot yourself in the foot), then you can throw a FilterStopPropagation exception.

@@ -43,7 +43,7 @@ public function before(RequestInterface $request, $arguments = null);
*
* @param array|null $arguments
*
* @return ResponseInterface|void
* @return mixed
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it is more acceptable to return only ResponeInterface here.

Suggested change
* @return mixed
* @return ResponseInterface

@ping-yee ping-yee deleted the docs-replace-filter branch June 13, 2023 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants