cvs: peardoc /en/package/php/php-compatinfo condition.xml

From: Date: Mon, 28 Jul 2008 20:32:54 +0000
Subject: cvs: peardoc /en/package/php/php-compatinfo condition.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-11029@lists.php.net to get a copy of this message
farell Mon Jul 28 20:32:54 2008 UTC Added files: /peardoc/en/package/php/php-compatinfo condition.xml Log: initial commit of chapter related to Conditional Code Analysis

http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-compatinfo/condition.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-compatinfo/condition.xml +++ peardoc/en/package/php/php-compatinfo/condition.xml <!-- $Revision: 1.1 $ --> <refentry id="package.php.php-compatinfo.condition"> <refnamediv> <refname>Conditional Code Analysis</refname> <refpurpose>improve accuracy detection with conditional code</refpurpose> </refnamediv> <refsect1 id="package.php.php-compatinfo.condition.category"> <title>Categories</title> <para> As briefly introduced at end of <link linkend="package.php.php-compatinfo.advanced">advanced detection</link> chapter, we will learn now that there are 3 categories of conditional code that could give wrong result, if there are not catched properly. </para> <para> These categories are : <itemizedlist> <listitem> <para> <emphasis role="bold">function</emphasis> (<literal>cond_code = 1</literal>) </para> <para> A quick example: <programlisting role="php"> <![CDATA[ <?php if (function_exists('debug_backtrace')) { $backtrace = debug_backtrace(); } else { $backtrace = false; } ?> ]]> </programlisting> </para> </listitem> <listitem> <para> <emphasis role="bold">extension</emphasis> (<literal>cond_code = 2</literal>) </para> <para> A quick example: <programlisting role="php"> <![CDATA[ <?php if (extension_loaded('sqlite') == false) { $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : ''; dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX); echo 'SQLite version : ' . sqlite_libversion(); } ?> ]]> </programlisting> </para> </listitem> <listitem> <para> <emphasis role="bold">constant</emphasis> (<literal>cond_code = 4</literal>) </para> <para> A quick example: <programlisting role="php"> <![CDATA[ <?php if (defined('DATE_RSS') { $date_format = DATE_RSS; } else { $date_format = 'D, j M Y H:i:s O'; } // will display something like "Sat, 26 Jul 2008 16:56:24 +0200" echo date($date_format, mktime(0, 0, 0, 7, 26, 2008)); ?> ]]> </programlisting> </para> </listitem> </itemizedlist> </para> </refsect1> <refsect1 id="package.php.php-compatinfo.condition.web-catch"> <title>How to catch them with web interface</title> <para> At the beginning of first version that catch conditional code, there were only 3 options: <literal>ignore_functions</literal>, <literal>ignore_extensions</literal> and <literal>ignore_constants</literal>. </para> <para> Incovenient with these options, is that you should know the source code to parse, and identify whose functions, extensions or constants to avoid. </para> <para> Version 1.7.0 of API has introduced the ability to add name patterns to identify all or part of functions, extensions, constants to ignore from parsing. You should use now these options: <literal>ignore_functions_match</literal>, <literal>ignore_extensions_match</literal> or <literal>ignore_constants_match</literal>. </para> <para> Let's take a look with an example, how it's easy to catch whatever you want to exclude from parsing. We will take again example of PEAR::HTML_CSS 1.5.1 package already seen in <link linkend="package.php.php-compatinfo.advanced.dir"> advanced directory detection </link>. </para> <para> <programlisting role="php"> <![CDATA[ <?php require_once 'PHP/CompatInfo.php'; $datasource = '/tmp/HTML_CSS-1.5.1'; $options = array( 'ignore_dirs' => array('examples', 'tests'), 'ignore_functions_match' => array('function_exists', array('/.*/')), 'ignore_extensions_match' => array('extension_loaded', array('/.*/')), 'ignore_constants_match' => array('defined', array('/.*/')), ); $pci = new PHP_CompatInfo(); $pci->parseData($datasource, $options); ?> ]]> </programlisting> </para> <para> Here we catch all standard conditional code (<literal>function_exists</literal>, <literal>extension_loaded</literal>, <literal>defined</literal>) what match all names (regular expression given by <literal>array('/.*/')</literal>). </para> <para> <tip> <para> To catch what ever function you want, use <literal>preg_match</literal> rather than <literal>function_exists</literal>. </para> <para> It's also true for <literal>extension_loaded</literal> and <literal>defined</literal> </para> <para> With <emphasis>preg_match</emphasis> you are really free to ignore a single function, a group set or all functions, only by giving the good name pattern. </para> </tip> Example to ignore all functions prefixed by <emphasis>xdebug_</emphasis> : <programlisting role="php"> <![CDATA[ <?php require_once 'PHP/CompatInfo.php'; $datasource = '/tmp/HTML_CSS-1.5.1'; $options = array( 'ignore_functions_match' => array('preg_match', array('/^xdebug_/')), ); $pci = new PHP_CompatInfo(); $pci->parseData($datasource, $options); ?> ]]> </programlisting> </para> </refsect1> <refsect1 id="package.php.php-compatinfo.condition.cli-catch"> <title>How to catch them with CLI</title> <para> If you use the command-line parser with <command>pci</command> script, the solution is a bit different. </para> <para> To catch cond_code = 1 (function), you must run the command <cmdsynopsis> <command>pci</command> <arg>-inm <replaceable>functions-match.txt</replaceable></arg> </cmdsynopsis> where <filename>functions-match.txt</filename> is a text file, that identify on each line a new condition. </para> <para> Each blank line or beginning with <literal>;</literal> will be skipped (proceed as comment line like in php.ini) </para> <para> If first non blank character is an equal sign (=), then you can catch what ever function you want with a preg_match condition (see xdebug example in previous section (web interface) </para> <para> Example of text file contents <screen> ;=^xdebug_ ;=alias$..* ;file_put_contents </screen> </para> <para> <caution> <para> Do not confuse a regular expression beginning with equal sign (=), and the same line without =. </para> <para> In first case you will catch all functions that match the name pattern given found in all source code, while second case try to catch only matches found with <literal>if function_exists('')</literal> condition. </para> <para> It's also true for extensions and constants, we will see them now. </para> </caution> </para> <para> To catch cond_code = 2 (extension), you must run the command <cmdsynopsis> <command>pci</command> <arg>-iem <replaceable>extensions-match.txt</replaceable></arg> </cmdsynopsis> where <filename>extensions-match.txt</filename> is a text file, that identify on each line a new condition. </para> <para> Each blank line or beginning with <literal>;</literal> will be skipped (proceed as comment line like in php.ini) </para> <para> If first non blank character is an equal sign (=), then you can catch what ever extension you want with a preg_match condition. </para> <para> Example of text file contents <screen> ;=xdebug ;sqlite =gd ;=sapi_apache </screen> </para> <para> To catch cond_code = 4 (constant), you must run the command <cmdsynopsis> <command>pci</command> <arg>-icm <replaceable>constants-match.txt</replaceable></arg> </cmdsynopsis> where <filename>constants-match.txt</filename> is a text file, that identify on each line a new condition. </para> <para> Each blank line or beginning with <literal>;</literal> will be skipped (proceed as comment line like in php.ini) </para> <para> If first non blank character is an equal sign (=), then you can catch what ever constant you want with a preg_match condition. </para> <para> Example of text file contents <screen> =PHP_EOL =DATE_RSS ;FILE_FIND_VERSION </screen> </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->
« previous php.pear.doc (#11029) next »