CoverageXmlParser.php 2.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
<?php
    /*
    *  $Id: CoverageXmlParser.php 14663 2005-03-23 19:27:27Z npac $
    *  
    *  Copyright(c) 2004-2006, SpikeSource Inc. All Rights Reserved.
    *  Licensed under the Open Software License version 2.1
    *  (See http://www.spikesource.com/license.html)
    */
?>
<?php

    require_once dirname(__FILE__) . "/BasicXmlParser.php";

    /** 
     * Special parser for SpikePHPCoverage data parsing 
     * Expect input in following format:
     * <spike-phpcoverage>
     *   <file path="/complete/file/path">
     *     <line line-number="10" frequency="1"/>
     *     <line line-number="12" frequency="2"/>
     *   </file>
     *   <file path="/another/file/path">
     *     ...
     *   </file>
     * </spike-phpcoverage>
     * 
     * @author Nimish Pachapurkar <npac@spikesource.com>
     * @version $Revision: $
     * @package SpikePHPCoverage_Parser
     */
    class CoverageXmlParser extends BasicXmlParser {
        /*{{{ Members */

        protected $_data = array();
        protected $_lastFilePath;

        /*}}}*/
        /*{{{ public function startHandler() */

        public function startHandler($xp, $name, $attrs) {
            switch($name) {
            case "FILE":
                $fileAttributes = $this->handleAttrTag($name, $attrs);
                $this->_lastFilePath = $fileAttributes["PATH"];
45
                if( ! isset($this->_data[$this->_lastFilePath])) {
46 47 48 49 50 51 52
                    $this->_data[$this->_lastFilePath] = array();
                }
                break;

            case "LINE":
                $lineAttributes = $this->handleAttrTag($name, $attrs);
                $lineNumber = (int)$lineAttributes["LINE-NUMBER"];
53
                if( ! isset($this->_data[$this->_lastFilePath][$lineNumber])) {
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
                    $this->_data[$this->_lastFilePath][$lineNumber] = (int)$lineAttributes["FREQUENCY"];
                }
                else {
                    $this->_data[$this->_lastFilePath][$lineNumber] += (int)$lineAttributes["FREQUENCY"];
                }
                break;
            }
        }

        /*}}}*/
        /*{{{ public function getCoverageData() */

        /** 
         * Returns coverage data array from the XML
         * Format:
         * Array
         * (
         *   [/php/src/remote/RemoteCoverageRecorder.php] => Array
         *   (
         *     [220] => 1
         *     [221] => 1
         *   )
         *
         *   [/opt/oss/share/apache2/htdocs/web/sample.php] => Array
         *   (
         *     [16] => 1
         *     [18] => 1
         *   )
         * )
         * 
         * @access public
         */
        public function getCoverageData() {
            return $this->_data;
        }

        /*}}}*/
    }
?>