rfc:apprise_on_invalid_arithmetic_operands

PHP RFC: Apprise on Invalid Arithmetic Operands

Introduction

Following on from the warning about invalid strings in arithmetic RFC, this RFC proposes to warn developers when they are using invalid operands with the arithmetic operators. The overall aim is therefore to help developers catch mistakes in their code when doing something non-sensical.

This is a WIP. Feel free to update this RFC with other cases where you'd like to see E_WARNINGs or E_NOTICEs emitted (as well as cases of promoting E_NOTICEs or demoting E_WARNINGs).

EDIT related: https://bugs.php.net/bug.php?id=67904

Proposal

This RFC seeks to do the following:

Emit an E_WARNING on resource operands

var_dump(1 + fopen(__FILE__, 'r')); // currently outputs int(6)

Reasoning: It is non-sensical behaviour to want to use a resource's ID, particularly without even notifying the programmer (†).

Emit an E_WARNING on null operands

var_dump(1 + null); // currently outputs int(1)

Reasoning: If empty strings (“”) now emit an E_WARNING, then other non-numeric falsy values should too

Promote the E_NOTICE for object conversions to an E_WARNING

var_dump(1 + new StdClass); // currently outputs int(2) && Notice: Object of class stdClass could not be converted to int...

Reasoning: The coercion is completely non-sensical. It will always coerce an object to int(1) in this context (†), and so the error level should be raised.

(†) Even this, I feel is non-sensical. If other truthy values (such as “abc”) are coerced to int(0), then both objects and resources should too. Right now, objects coerce to int(1), and resource coerces to their resource ID. For now though, this is considered out of the scope of this RFC.

Backward Incompatible Changes

What breaks, and what is the justification for it?

Proposed PHP Version(s)

PHP 7.1

RFC Impact

To SAPIs

Describe the impact to CLI, Development web server, embedded PHP etc.

To Existing Extensions

Will existing extensions be affected?

To Opcache

To be done...

New Constants

None.

php.ini Defaults

If there are any php.ini settings then list:

  • hardcoded default values
  • php.ini-development values
  • php.ini-production values

Open Issues

Make sure there are no open issues when the vote starts!

Unaffected PHP Functionality

List existing areas/features of PHP that will not be changed by the RFC.

This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise.

Future Scope

This sections details areas where the feature might be improved in future, but that are not currently proposed in this RFC.

Proposed Voting Choices

Include these so readers know where you are heading and can discuss the proposed voting options.

State whether this project requires a 2/3 or 50%+1 majority (see voting)

Patches and Tests

Links to any external patches and tests go here.

If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed.

Make it clear if the patch is intended to be the final patch, or is just a prototype.

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged to
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

Links to external references, discussions or RFCs

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/apprise_on_invalid_arithmetic_operands.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1