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

Backfill Parameters #3938

Merged
merged 1 commit into from
Nov 28, 2020
Merged

Backfill Parameters #3938

merged 1 commit into from
Nov 28, 2020

Conversation

MGatner
Copy link
Member

@MGatner MGatner commented Nov 28, 2020

Description
This PR addresses the now-deprecated(PHP 8) use of mandatory parameters following optional parameters. Split off from #3931.

Checklist:

  • Securely signed commits
  • Component(s) with PHPdocs
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@MGatner
Copy link
Member Author

MGatner commented Nov 28, 2020

Rebasing 4.1 and trying again.

@MGatner MGatner reopened this Nov 28, 2020
@MGatner MGatner mentioned this pull request Nov 28, 2020
5 tasks
@MGatner MGatner requested a review from paulbalandan November 28, 2020 15:00
@MGatner
Copy link
Member Author

MGatner commented Nov 28, 2020

Looks good! @paulbalandan you still around by chance?

@MGatner
Copy link
Member Author

MGatner commented Nov 28, 2020

Since this was approved in the other PR I am going to proceed with the merge.

@MGatner MGatner merged commit dbeffb7 into codeigniter4:4.1 Nov 28, 2020
@MGatner MGatner deleted the prep-4.1 branch November 28, 2020 15:50
Copy link
Member

@paulbalandan paulbalandan left a comment

Choose a reason for hiding this comment

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

I was influenced by PHPStan's extra strict rules in that empty() implies weak comparison and we should strive to be more specific what 'emptiness' we are checking. We already have a rector rule that changes empty checks on arrays to === [] and I believe we should do the same for scalar types.

{
if (empty($alias))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (empty($alias))
if ($alias === '')

{
if (empty($alias))
{
throw new InvalidArgumentException('You must supply the parameter: alias.');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new InvalidArgumentException('You must supply the parameter: alias.');
throw new InvalidArgumentException('You must supply the parameter: alias.'); // @codeCoverageIgnore

Comment on lines +134 to +137
if (empty($uri) || empty($userAgent))
{
throw new InvalidArgumentException('You must supply the parameters: uri, userAgent.');
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (empty($uri) || empty($userAgent))
{
throw new InvalidArgumentException('You must supply the parameters: uri, userAgent.');
}
if ($uri === null || $userAgent === null)
{
throw new InvalidArgumentException('You must supply the parameters: uri, userAgent.'); // @codeCoverageIgnore
}

{
if (empty($origWidth) || empty($origHeight))
{
throw new InvalidArgumentException('You must supply the parameters: origWidth, origHeight.');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new InvalidArgumentException('You must supply the parameters: origWidth, origHeight.');
throw new InvalidArgumentException('You must supply the parameters: origWidth, origHeight.'); // @codeCoverageIgnore

@@ -352,13 +353,18 @@ public function required($str = null): bool
* required_with[password]
*
* @param string|null $str
* @param string $fields List of fields that we should check if present
* @param string|null $fields List of fields that we should check if present
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @param string|null $fields List of fields that we should check if present
* @param string $fields List of fields that we should check if present

Comment on lines +361 to +366
public function required_with($str = null, string $fields = null, array $data = []): bool
{
if (is_null($fields) || empty($data))
{
throw new InvalidArgumentException('You must supply the parameters: fields, data.');
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public function required_with($str = null, string $fields = null, array $data = []): bool
{
if (is_null($fields) || empty($data))
{
throw new InvalidArgumentException('You must supply the parameters: fields, data.');
}
public function required_with($str = null, string $fields = '', array $data = []): bool
{
if ($fields === '' || $data === [])
{
throw new InvalidArgumentException('You must supply the parameters: fields, data.'); // @codeCoverageIgnore
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Same as below, $fields is allowed to be an empty string so we need to add the null option. This could probably be addressed on calling classes but it is also possible that someone is calling this directly (though I really hope not).

Comment on lines +413 to +424
* @param string|null $fields
* @param array $data
*
* @return boolean
*/
public function required_without($str = null, string $fields, array $data): bool
public function required_without($str = null, string $fields = null, array $data = []): bool
{
if (is_null($fields) || empty($data))
{
throw new InvalidArgumentException('You must supply the parameters: fields, data.');
}

Copy link
Member

Choose a reason for hiding this comment

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

Same comment with required_with

Comment on lines +209 to +215
protected function processRules(string $field, string $label = null, $value, $rules = null, array $data = null): bool
{
if (is_null($data))
{
throw new InvalidArgumentException('You must supply the parameter: data.');
}

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
protected function processRules(string $field, string $label = null, $value, $rules = null, array $data = null): bool
{
if (is_null($data))
{
throw new InvalidArgumentException('You must supply the parameter: data.');
}
protected function processRules(string $field, string $label = null, $value, $rules = null, array $data = []): bool
{
if ($data === [])
{
throw new InvalidArgumentException('You must supply the parameter: data.'); // @codeCoverageIgnore
}

Copy link
Member Author

Choose a reason for hiding this comment

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

This one has to add null as an input option because [] is actually valid input and we're trying to catch cases where the parameter was not supplied at all.

@paulbalandan
Copy link
Member

Since this was approved in the other PR I am going to proceed with the merge.

Oh man! I was a little too late. 😂

@MGatner
Copy link
Member Author

MGatner commented Nov 28, 2020

Ah sorry! Those are good changes. I am working on the class aliases but I'll get these done up later today or tomorrow.

@sfadschm
Copy link
Contributor

sfadschm commented Dec 2, 2020

Did this PR also cover functions with nullable optional arguments (or typed arguments that default to null)?
The link supplied in #3957 indicates that such definitions (while still being wrong) do not trigger the PHP8.0 deprecation message.

@MGatner
Copy link
Member Author

MGatner commented Dec 2, 2020

What is wrong with nullable optional parameters?

This PR dealt with the deprecated use of mandatory parameters following optional ones.

@sfadschm
Copy link
Contributor

sfadschm commented Dec 2, 2020

Sorry for the confusion, I will try to elaborate:
Required arguments should never be put after optional arguments, or PHP8 will throw a deprecation message. This has been dealt with in this PR.

However, according to the link provided in the referenced issue, PHP8 will not show such message in the following case (typed optional argument defaulting to null):
function test(string $param1 = null, string $param2)

As far as I understand the deprecation, this type of argument list would still be incorrect, although it does not trigger the deprecation message.

Therefore I was asking whether the selection of functions targeted by this PR was based on the PHP8 deprecation messages or if all functions with optional parameters before required ones have been changed.
In the first case, this PR might have missed some of the functions with argument lists like shown in the example.

Hope this makes it clearer :-)

@MGatner
Copy link
Member Author

MGatner commented Dec 2, 2020

While I would like to see that category changed as well, the focus of this PR was actual deprecation notices as they arose during PHP 8 testing. I am unhappy that these parameter inputs exist to begin with, but without breaking the methods for all users we can't really change that in a clean way.

If you identify any of the above and want to submit a non-breaking PR we could certainly resolve. I'll be honest I didn't know that was a separate category!

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.

3 participants