<?php
/********************************************************************************************
** Autor : Andrés DarÃo Gutiérrez Poveda. *
** Fecha : Septiembre 13 de 2004. *
** Versión : 0.1.0. *
** Empresa : Atila Servicios S.A. *
** *
*********************************************************************************************
** Proposito: *
** *
** This class is meant to parse a .php archive to see the use of each variable and point *
** out if there's any possible error. Note that this just a posibility, you have to check*
** your code to know if it is really an error or it is meant to be that way. *
** *
*********************************************************************************************
** Version 1.0.0 - *
** Cambios para la versión 1.0.0 *
** *
** - *
********************************************************************************************/
class Parser{
/*******************************************************************************************
** ATTRIBUTES **
*******************************************************************************************/
/** Path and name of the script to parse. */var $archivo;
/** Array of colors. */var $colores;
/** Array of the variables use. */var $variables;
/*******************************************************************************************
** CONSTANTS **
*******************************************************************************************/
var $prohibidos = " \"'+-*/[]()!;.,=<>";
/*******************************************************************************************
** METHODS **
*******************************************************************************************/
/*****************************************************************************
** NAME: **
** **
** SET and GET for the archivo attribute. **
** **
******************************************************************************
** OBJETIVE: **
** **
** Methods to set and get the value of the attribute. **
** **
******************************************************************************
** PARAMS: **
** **
** - value: value to set. **
*****************************************************************************/
function get_archivo(){
return $this->archivo;
}
function set_archivo($value){
$this->archivo = $value;
}
/*****************************************************************************
** NAME: **
** **
** SET and GET for the colores attribute. **
** **
******************************************************************************
** OBJETIVE: **
** **
** Methods to set and get the value of the attribute. **
** **
******************************************************************************
** PARAMS: **
** **
** - top: Color for the headers of the report. **
** - left: Color for the left panel of the table. **
** - font: Color for the font of the header of the table. **
*****************************************************************************/
function set_colores($top,$left,$font){
$this->colores['top'] = $top;
$this->colores['left'] = $left;
$this->colores['font'] = $font;
$this->colores['algo'] = '#FF9955';
}
/*****************************************************************************
** NAME: **
** **
** parse () **
** **
******************************************************************************
** Objective: **
** **
** This function parse the .php archive. **
** **
******************************************************************************
** PARAMS: **
** **
*****************************************************************************/
function parse(){
$empiece = false;
$variable = "";
if($handler = fopen($this->archivo,"r")){
while(($char = fgetc($handler)) !== false){
if($char == "$")
$empiece = true;
if($empiece){
$pos = strpos($this->prohibidos,$char);
if($pos === false)
$variable .= $char;
else{
if($variable == '$this'){
$datos = $this->obtener_atributo($handler);
$variable = $datos['variable'];
$pos = $datos['pos'];
}
if((!empty($variable)) && ($variable != '$')){
if(($operacion = $this->obtener_operacion($handler,$pos)) != -1)
$this->reportar($variable,$operacion);
}
$empiece = false;
$variable = "";
}
}
}
fclose($handler);
}else
echo "No pude!";
}
/*****************************************************************************
** NAME: **
** **
** parse () **
** **
******************************************************************************
** Objective: **
** **
** This function parse the .php archive. **
** **
******************************************************************************
** PARAMS: **
** **
*****************************************************************************/
function obtener_atributo($file){
$char = fgetc($file);
$pos = strpos($this->prohibidos,$char);
if($this->prohibidos[$pos] == '>'){
$seguir = true;
$datos['variable'] = "$";
while(($seguir) && (($char = fgetc($file)) !== false)){
$pos = strpos($this->prohibidos,$char);
if($pos === false)
$datos['variable'] = $datos['variable'].$char;
else if($this->prohibidos[$pos] == '('){
$seguir = false;
$datos['variable'] = "";
}else{
$seguir = false;
$datos['pos'] = $pos;
}
}
}else{
$datos['variable'] = "";
$datos['pos'] = $pos;
}
return $datos;
}
/*****************************************************************************
** NAME: **
** **
** reportar ($variable,$operacion) **
** **
******************************************************************************
** Objective: **
** **
** This function keep count of the variable and operation of each. **
** **
******************************************************************************
** PARAMS: **
** **
** - variable: Name of the variable. **
** - operacion: Code of the operation. **
*****************************************************************************/
function reportar($variable,$operacion){
$this->variables[$variable]['contador'] = $this->variables[$variable]['contador'] + 1;
$this->variables[$variable]['nombre'] = $variable;
$this->variables[$variable][$operacion] = $this->variables[$variable][$operacion] + 1;
}
/*****************************************************************************
** NAME: **
** **
** ver_reporte () **
** **
******************************************************************************
** Objective: **
** **
** This function creates the HTML report to show. **
** **
******************************************************************************
** PARAMS: **
** **
*****************************************************************************/
function ver_reporte(){
usort($this->variables,"asc");
$reporte = "<style>.grid {font-family: Tahoma, Arial, Helvetica, sans-serif; font-size: 11px; color: #000000}</style>";
$reporte .= "<table border=1 cellspacing=1 bordercolor=".$this->colores['top']." class='grid'>";
$reporte .= "<tr align=center bgcolor=".$this->colores['top'].">";
$reporte .= "<td><font color=".$this->colores['font']."><strong>Name</strong></font></td>";
$reporte .= "<td><font color=".$this->colores['font']."><strong>Counter</strong></font></td>";
$reporte .= "<td><font color=".$this->colores['font']."><strong>Compare</strong></font></td>";
$reporte .= "<td><font color=".$this->colores['font']."><strong>Assign</strong></font></td>";
$reporte .= "<td><font color=".$this->colores['font']."><strong>Concat</strong></font></td>";
$reporte .= "<td><font color=".$this->colores['font']."><strong>SubItem</strong></font></td>";
$reporte .= "<td><font color=".$this->colores['font']."><strong>Operate</strong></font></td>";
$reporte .= "<td><font color=".$this->colores['font']."><strong>Object</strong></font></td>";
$reporte .= "<td><font color=".$this->colores['font']."><strong>Param</strong></font></td>";
$reporte .= "<td><font color=".$this->colores['font']."><strong>Array</strong></font></td>";
$reporte .= "<td bgcolor=".$this->colores['algo']."><font color=".$this->colores['font']."><strong>Type</strong></font></td>";
$reporte .= "<td bgcolor=".$this->colores['algo']."><font color=".$this->colores['font']."><strong>Text</strong></font></td></tr>";
foreach($this->variables as $variable){
$error = false;
$comportamiento = '';
$reporte .= "<tr><td bgcolor=".$this->colores['left'].">".$variable['nombre']."</td><td align=center>".$variable['contador']."</td>";
$contadortotal += $variable['contador'];
for($i=0;$i<8;$i++){
if($variable[$i] > 0){
$color = '';
$contadori[$i] = $contadori[$i] + $variable[$i];
$resultado = $variable['contador'] - $variable[$i];
$comportamiento = $this->obtener_comportamiento($resultado,$i);
if($comportamiento['type'] != 'Normal'){
$comportamientos = "<td align=center bgcolor=".$this->colores['algo'].">".$comportamiento['type']."</td>";
$comportamientos .= "<td align=center bgcolor=".$this->colores['algo'].">".$comportamiento['text']."</td>";
$color = $this->colores['algo'];
$error = true;
}else if(!$error)
$comportamientos = "<td align=center>".$comportamiento['type']."</td><td align=center>".$comportamiento['text']."</td>";
}else
$color = $this->colores['left'];
$reporte .= "<td align=center bgcolor=".$color.">".$variable[$i]."</td>";
}
$reporte .= $comportamientos;
$reporte .= "</tr>";
}
$reporte .= "<tr>";
$reporte .= "<td>Total</td><td align=center>".$contadortotal."</td>";
for($i=0;$i<8;$i++)
$reporte .= "<td align=center>".$contadori[$i]."</td>";
$reporte .= "</tr>";
$reporte .= "</table>";
return $reporte;
}
/*****************************************************************************
** NAME: **
** **
** obtener_comportamiento ($resultado,$i) **
** **
******************************************************************************
** Objective: **
** **
** This function set the behavior of the variable and assign type and text.**
** **
******************************************************************************
** PARAMS: **
** **
** - resultado: Result of each posibility. **
** - type: Code of the operation. **
*****************************************************************************/
function obtener_comportamiento($resultado,$type){
if($resultado < 0)
$comportamiento = "ERROR: Internal counter.";
else if($resultado == 0){
switch($type){
case 0: /** Compare Statements */
$comportamiento['type'] = "ERROR";
$comportamiento['text'] = "Used in compare statement but never assigned.";
break;
case 1: /** Assign Statements */
$comportamiento['type'] = "ERROR";
$comportamiento['text'] = "Assigned value and never used.";
break;
case 2: /** Concatenation Statements */
$comportamiento['type'] = "WARNING";
$comportamiento['text'] = "Used in concatenation statement but never assigned.";
break;
case 3: /** SubItem */
$comportamiento['type'] = "ERROR";
$comportamiento['text'] = "Used as subitem in array but never assigned.";
break;
case 4: /** Operate Statements */
$comportamiento['type'] = "WARNING";
$comportamiento['text'] = "Used in operate statement but never assigned.";
break;
case 5: /** Object */
$comportamiento['type'] = "ERROR";
$comportamiento['text'] = "Used as an object but never created.";
break;
case 6: /** Param */
$comportamiento['type'] = "WARNING";
$comportamiento['text'] = "Used as a param but never assigned.";
break;
case 7: /** Array */
$comportamiento['type'] = "WARNING";
$comportamiento['text'] = "Used as an array but never used.";
break;
case 8: /** Being assigned */
$comportamiento['type'] = "WARNING";
$comportamiento['text'] = "Its value is beign assign but never set.";
break;
default:
$comportamiento['type'] = "ERROR";
$comportamiento['text'] = "Unknown error.";
break;
}
}else{
$comportamiento['type'] = "Normal";
$comportamiento['text'] = "Good use.";
}
return $comportamiento;
}
/*****************************************************************************
** NAME: **
** **
** obtener_operacion ($file,$pos) **
** **
******************************************************************************
** Objective: **
** **
** This function look for the operation in the .php archive of the variable**
** **
******************************************************************************
** PARAMS: **
** **
** - file: File handler. **
** - pos: Position of the 'prohibidos'. **
*****************************************************************************/
function obtener_operacion($file,$pos){
switch($this->prohibidos[$pos]){
case ' ':
$char = fgetc($file);
$pos = strpos($this->prohibidos,$char);
$tipo = $this->obtener_operacion($file,$pos);
break;
case '=':
$char = fgetc($file);
if($char == '=')
$tipo = 0;
else
$tipo = 1;
break;
case '<':
$tipo = 0;
break;
case '>':
$tipo = 0;
break;
case '!':
$tipo = 0;
break;
case '.':
$tipo = 2;
break;
case ']':
$tipo = 3;
break;
case '+':
$tipo = 4;
break;
case '-':
$char = fgetc($file);
if($char != '>')
$tipo = 4;
else
$tipo = 5;
break;
case '*':
$tipo = 4;
break;
case '/':
$tipo = 4;
break;
case ')':
$tipo = 6;
break;
case ',':
$tipo = 6;
break;
case '[':
$tipo = 7;
break;
case ';':
$tipo = 6;
break;
default:
$tipo = -1;
break;
}
return $tipo;
}
}
?>