check the php version compatibility of a php script - php

I have a php script. I want to check the php version compatibility that each function of this script has.
For example the script has the hash() function. If we look at php.net site it says that this function was added (PHP 5 >= 5.1.2, PECL hash >= 1.1).
So assuming that this script has only this function we say that it is compatible with php 5.1.2 and onwards. Is there any software tool that can scan the script and tell you the changelog of every function and possibly gives you the lowest possible score e.g. (php version 4.3 and on)? It is a bit tedious to check manually every function in the script.
Thank you in advance

As PHP supports dynamic function/method names, just searching through the source code would not give reliable results.
Example:
$function = 'strpos';
$result = $function(...);
You might use xdebug function traces together with a 100% test coverage. This could give you all function names which had been called during a test suite run with 100% coverage - meaning all code has run. But even this isn't reliable as you would have to make sure, that your test suite covered all possible dynamic function names what is impossible. (assuming the max length of a function name is unlimited)
My final answer is: this isn't reliably possible.

Related

Can a php application instantiate multiple RNGs

Update - this question applies to PHP < 8.2 - for 8.2+, You have access to Randomizer
PHP supports `mt_rand()` and `mt_srand()`, but these are global and static.
I notice in the PHP C source that mt_rand uses a engine_mt19937 instance; is there a way in PHP to instantiate our own instance of this RNG so we can control its seed?
On PHP 8.2+, yes. See #AlexHowansky's answer for that. On earlier PHP versions, no.
Your link to the source code points to the master branch, which already has the PHP 8.2 logic in it since it was officially released last month. That is why the master branch includes the reference to an MT19937 engine.
On older versions (e.g. the latest 8.1 branch) you can see that mt_rand() did not use a configurable engine but just checked if it was already seeded. PHP 8.1 and earlier used a single global generator for mt_rand() so it is not possible to have multiple RNGs with different seeds if you want to use the native functions.
Having said that, you could probably roll your own implementation of an MT19937 RNG for PHP 8.1. Others have.
is there a way in PHP to instantiate our own instance of this RNG so we can control its seed?
With PHP 8.2+, yes. See the \Random classes. You can do something like:
$rand1 = new \Random\Engine\Mt19937($seed1);
$value1 = $rand1->generate();
$rand2 = new \Random\Engine\Mt19937($seed2);
$value2 = $rand2->generate();

PECL stats_rand_gen_normal always returns the same value

With PHP 7.3, I'm trying to use the PECL stats extension: https://pecl.php.net/package/stats
Especially, I'm interested in generating random values following a normal distribution, with the function stats_rand_gen_normal
In my script, the function always returns the same result.
I tried in CLI, to be sure :
php -r "echo(stats_rand_gen_normal(0,1));"
Every execution prints the same number.
So it's a random value which is always the same... What am I missing here?
This seems to me like a bug too and I have make a bug report.
There is a solution on stackoverflow using a custom function.
You should use stats_rand_setall() to seed values to the random generator.

Allow lack of space after function parenthesis if there is a return type hint in PHP (CodeSniffer)

I am using the MediaWiki coding standard for php_codesniffer. Thing is, that is created for PHP version <7.0.0. Let's take the following not-formatted code snippet:
function test(){}
The sniffer will report an error, that is needs space between ) and { (the rule is Generic.Functions.OpeningFunctionBraceKernighanRitchie.SpaceAfterBracket)
Now that's ok, it is normal (for me at least) to write
function test() {}
But when it comes to PHP 7 and the function has a return type hint, I want it formatted like this
function test(): string {}
So no space between ) and :, but spaces between string and other tokens there. What is the rule I have to write to achieve this?
There is already sniff for that in Slevomat/CodingStandard: https://github.com/slevomat/coding-standard/blob/master/README.md#slevomatcodingstandardtypehintstypehintdeclaration-
It is high quality package I use for over a year. They also have a branch for 3.0 ready. Also check also sniffs, they're awesome and helps with refactoring to PHP 7.0 and 7.1.

How can I make my open-source PHP app cross-server?

I have a PHP program using MySQL that I will be making open-source, including a simple installer, and I want to make it as easy to install on any server with PHP4 or 5 and MySQL 4 as possible.
I've included an installer to make it user-friendly, but I need to know what are the things I can do to make it most likely to install on every server.
I'll start: I've made sure to use full PHP tags (not short) like this <?php ?> and to make sure all variables are declared prior to using them, like so $nVar = (isset($_POST['nVar']) ? $_POST['nVar'] : NULL);.
What other best practices should be incorporated in a PHP app for the best cross-server compatability?
Just a few hints, from the top of my head
The class { __constructor } is deprecated.
Dont use GLOBAL
Use split and join
Watch out for file magic byte recognition functions
The get_class get_parent_class alphanumerical changed to accept camelcasing - use strtolower on returned value before comparing

Terminology question on "dereferencing"?

In PHP, the following code is valid
$a=array(0);$a[0];
but that one is invalid:
array(0)[0]
What is the terminology corresponding to that behaviour? (has it anything to do with "dereferencing"?)
What is the motivation behind such a behaviour (besides user spite :-P)
I am looking for the general terminology, not necessarily the terminology associated with PHP.
(Other example: in MATLAB, the following is valid:
s = size(M)
s(0)
but that is invalid:
size(M)(0)
In both PHP and MATLAB, adding parenthesis does not help, i.e., (array(0))[0] and (size(M))(0) are both invalid)
That's called Array dereferencing, and will become available in PHP 5.4 (which is currently in alpha)
Note (thanks Gordon) : what you are asking for, with array()1, is not possible even in PHP 5.4 -- but it'll work for functions.
A couple of sources :
RFC - Function Array Dereferencing
Features in PHP trunk: Array dereferencing, when it was unsure whether there would be a PHP 5.4 or a PHP 6
And, last but not least, the (currently last) news on php.net : PHP 5.4 alpha1 released
Quoting that last news :
Here is an incomplete list of changes: - Added: Traits language
construct - Added: Array dereferencing support - Added:
DTrace support - Improved: Improved Zend Engine memory usage and
performance - Moved: ext/sqlite moved to pecl (sqlite3 support is
still built-in)
1.array() is not a function, even if it looks like one -- it's actually what PHP calls a language construct ; and those don't behave like functions.
This is called "array dereferencing" and it will be available for use in PHP5.4.

Categories