Doc #77838 [Com]: version_compare() returns null when invalid operator given

[email protected]@bugs.php.net ("bernie dot durfee at renaissance dot com" X-PHP-Bug : 77838X-PHP-Site : PHPX-PHP-Type : Documentation ProblemX-PHP-Version : IrrelevantX-PHP-Category : PHP options/info functionsX-PHP-OS : anyX-PHP-Status : ClosedX-PHP-Old-Status : ClosedIn-Reply-To :) Fri, 17 Nov 2023 13:52:19 +0000
Newsgroups php.doc.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=77838&edit=1

 ID:                 77838
 Comment by:         bernie dot durfee at renaissance dot com
 Reported by:        ravage at bluewin dot ch
 Summary:            version_compare() returns null when invalid operator
                     given
 Status:             Closed
 Type:               Documentation Problem
 Package:            PHP options/info functions
 Operating System:   any
 PHP Version:        Irrelevant
 Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

The docs still don't reflect that the return value that could be null.


Previous Comments:
------------------------------------------------------------------------
[2020-02-07 06:05:08] [email protected]

Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=442996b447e3d77d5cda9a50da78007e739a1d3f
Log: Fix #77838: version_compare() returns null when invalid operator given

------------------------------------------------------------------------
[2019-04-03 17:25:17] [email protected]

Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=fbb9a28e62178d7374b37c16cd6f83b7dd8fd9a2
Log: Fix #77838: version_compare() returns null when invalid operator given

------------------------------------------------------------------------
[2019-04-03 17:22:11] [email protected]

This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

------------------------------------------------------------------------
[2019-04-03 17:21:42] [email protected]

Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=347125
Log: Fix #77838: version_compare() returns null when invalid operator given

------------------------------------------------------------------------
[2019-04-03 14:46:59] ravage at bluewin dot ch

Description:
------------
According to the documentation of the version_compare() function, it returns either true or false, if there is an operator parameter given.

https://www.php.net/manual/en/function.version-compare.php

But it seems it can also return null, if there is an invalid operator string given.
Invalid can be at least either a totally wrong operator, e.g. "invalid", a space " " or a valid operator string pre- or suffixed with a space, e.g " ge".

This is certainly a documentation issue but for the future this could be improved by throwing an exception, e.g. InvalidArgumentException.

By the way this was reported as a comment on the function's documentation page at https://www.php.net/manual/en/function.version-compare.php#106715

Test script:
---------------
<?php
var_dump(version_compare('3.7.5', '3.7.0', 'ge')); // true, OK
var_dump(version_compare('3.7.5', '3.7.0', ' ')); // null, should throw invalid argument exception at best
var_dump(version_compare('3.7.5', '3.7.0', ' ge')); // null, should throw invalid argument exception at best
var_dump(version_compare('3.7.5', '3.7.0', 'ge ')); // null, should throw invalid argument exception at best
var_dump(version_compare('3.7.5', '3.7.0', 'invalid')); // null, should throw invalid argument exception at best
?>

https://3v4l.org/CrO0J

Expected result:
----------------
That the possible return value of null is officially documented at https://www.php.net/manual/en/function.version-compare.php.
Even better would be an exception thrown, e.g. InvalidArgumentException.

Actual result:
--------------
null (officially undocumented)


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=77838&edit=1