Basic detection

Basic detection – parse data source with default options

Detection of a single file

In most case, the basic detection is enough. But sometimes, we will need to adjust accuracy of parser to give the best result. It is possible with $option, the second parameter of each parser method. See parser options list for details.

Suppose we have to detect which PHP version we need to run this script named "math.php"

<?php
$nb 
bcsub(1.23454);
if (
preg_match('/^-/'$nb)) {
    echo 
'minus';
}
?>

We will use this very simple detection script.

<?php
require_once 'PHP/CompatInfo.php';

$source dirname(__FILE__) . DIRECTORY_SEPARATOR 'math.php';

$info = new PHP_CompatInfo();
$info->parseFile($source);
// you may also use unified method:  $info->parseData($source);
?>

Default output used the Array renderer (we will talk about it and other renderers later; don't be afraid if you don't know what is it yet). Here are the raw results we got on standard output :

array (
  'ignored_files' =>
  array (
  ),
  'ignored_functions' =>
  array (
  ),
  'ignored_extensions' =>
  array (
  ),
  'ignored_constants' =>
  array (
  ),
  'max_version' => '',
  'version' => '4.0.0',
  'classes' =>
  array (
  ),
  'extensions' =>
  array (
    0 => 'bcmath',
    1 => 'pcre',
  ),
  'constants' =>
  array (
  ),
  'tokens' =>
  array (
  ),
  'cond_code' =>
  array (
    0 => 0,
  ),
)
    

It means that we need at least PHP 4.0.0 to run the "math.php" script. with two PHP extensions

  • bcmath
  • pcre

loaded.

Detection of a directory

Rather than parsing file after file of an application, you my give the root of your application path as the main directory to parse. Default is recursive parsing: that mean each directory children will be also parsed. And only files with extension

  • php
  • php4
  • inc
  • phtml

will be proceed.

Suppose we have to detect which PHP version we need to run the PEAR::File_Find package release 1.3.0

First begin to download the archive from http://pear.php.net/package/File_Find/download/1.3.0 and extract the full contents to a temporary directory (in our example its '/tmp')

We will use this very simple detection script.

<?php
require_once 'PHP/CompatInfo.php';

$source '/tmp/File_Find-1.3.0';

$info = new PHP_CompatInfo();
$info->parseDir($source);
// you may also use unified method:  $info->parseData($source);
?>

Results displayed:

array (
  'ignored_files' =>
  array (
    0 => '/tmp/File_Find-1.3.0/package.xml',
    1 => '/tmp/File_Find-1.3.0/tests/01glob.phpt',
    2 => '/tmp/File_Find-1.3.0/tests/02maptree.phpt',
    3 => '/tmp/File_Find-1.3.0/tests/03maptreemultiple.phpt',
    4 => '/tmp/File_Find-1.3.0/tests/04search.phpt',
    5 => '/tmp/File_Find-1.3.0/tests/05search_inside.phpt',
    6 => '/tmp/File_Find-1.3.0/tests/06match_shell.phpt',
    7 => '/tmp/File_Find-1.3.0/tests/bug2773.phpt',
  ),
  'ignored_functions' =>
  array (
  ),
  'ignored_extensions' =>
  array (
  ),
  'ignored_constants' =>
  array (
  ),
  'max_version' => '',
  'version' => '4.3.0',
  'classes' =>
  array (
    0 => 'File_Find',
  ),
  'extensions' =>
  array (
    0 => 'pcre',
  ),
  'constants' =>
  array (
    0 => 'FALSE',
    1 => 'NULL',
    2 => 'PHP_OS',
    3 => 'PREG_SPLIT_DELIM_CAPTURE',
    4 => 'PREG_SPLIT_NO_EMPTY',
    5 => 'TRUE',
    6 => '__FILE__',
  ),
  'tokens' =>
  array (
  ),
  'cond_code' =>
  array (
    0 => 4,
  ),
  '/tmp/File_Find-1.3.0/Find.php' =>
  array (
    'ignored_functions' =>
    array (
    ),
    'ignored_extensions' =>
    array (
    ),
    'ignored_constants' =>
    array (
    ),
    'max_version' => '',
    'version' => '4.3.0',
    'classes' =>
    array (
      0 => 'File_Find',
    ),
    'extensions' =>
    array (
      0 => 'pcre',
    ),
    'constants' =>
    array (
      0 => 'FALSE',
      1 => 'NULL',
      2 => 'PREG_SPLIT_DELIM_CAPTURE',
      3 => 'PREG_SPLIT_NO_EMPTY',
      4 => 'TRUE',
    ),
    'tokens' =>
    array (
    ),
    'cond_code' =>
    array (
      0 => 4,
    ),
  ),
  '/tmp/File_Find-1.3.0/tests/setup.php' =>
  array (
    'ignored_functions' =>
    array (
    ),
    'ignored_extensions' =>
    array (
    ),
    'ignored_constants' =>
    array (
    ),
    'max_version' => '',
    'version' => '4.0.0',
    'classes' =>
    array (
    ),
    'extensions' =>
    array (
    ),
    'constants' =>
    array (
      0 => 'PHP_OS',
      1 => '__FILE__',
    ),
    'tokens' =>
    array (
    ),
    'cond_code' =>
    array (
      0 => 0,
    ),
  ),
)
    

means that package PEAR::File_Find 1.3.0 need at least PHP 4.3.0 with extension pcre.

cond_cond offset 0 is set to 4. That means there are conditional code (constant condition) implemented in source code (with php defined function).

If you have a look on source code, you will see that all conditions referred to private package constant FILE_FIND_DEBUG

You may avoid to read the source code to know the constant name, if you specify the debug option when parsing the directory.

<?php
require_once 'PHP/CompatInfo.php';

$source '/tmp/File_Find-1.3.0';

$info = new PHP_CompatInfo();
$info->parseDir($source, array('debug' => true));
?>

And you will see in displayed results, something like :

  'cond_code' =>
  array (
    0 => 4,
    1 =>
    array (
      0 =>
      array (
      ),
      1 =>
      array (
      ),
      2 =>
      array (
        0 => 'FILE_FIND_DEBUG',
      ),
    ),
    

cond_code offset 1 is an array available only when debug mode is set to true. In this array :

  • offset 0, give name of function conditions

  • offset 1, give name of extension conditions

  • offset 2, give name of constant conditions

features and usage patterns (Previous) parse data source with additional options (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.