Skip to content
This repository
tree: b4eb282066
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 124 lines (104 sloc) 3.208 kb
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 45 46 47 48 49 50 51 52 53 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
open Common

open Ast_php

module Ast = Ast_php
module V = Visitor_php

(* timing:
* 9h30: start
* 9h42: finished
* 9h47: have the sexp to better printout
* 9h55: add static_scalar info about default parameter value
*
* Porting of Justin Bishop function stats:
* https://gist.github.com/0726a693b5fdd767f085
*)

type function_stat = {
  line: int;
  parameters: (string * static_scalar option) list;

  function_calls: string list;
  method_calls: string list;
  instantiations: string list;
}
 (* with tarzan *)
type file_stat = (string * function_stat) list
 (* with tarzan *)


(* automatically generated by ocamltarzan *)
let sexp_of_function_stat x =
  raise Todo
  
let sexp_of_file_stat v =
  Conv.sexp_of_list
    (fun (v1, v2) ->
       let v1 = Conv.sexp_of_string v1
       and v2 = sexp_of_function_stat v2
       in Sexp.List [ v1; v2 ])
    v
(* end auto generated *)


let s_of_file_stat x =
  Sexp.to_string_hum (sexp_of_file_stat x)


let navigator_extract_functions file =

  let (ast2, _stat) = Parse_php.parse file in
  let ast = Parse_php.program_of_program2 ast2 in

  let stats =
    ast +> Common.map_filter (fun top ->
      match top with
      | FuncDef def ->
          
          let funcs = ref [] in
          let methods = ref [] in
          let instances = ref [] in

          let hooks = { V.default_visitor with
            V.kexpr = (fun (k,_) x ->
              match Ast.untype x with
              | New (_, classref, _)
              | AssignNew (_, _, _, _, classref, _) ->
                  (match classref with
                  | ClassNameRefStatic name ->
                      Common.push2 (Ast.name name) instances;
                  | ClassNameRefDynamic (var, objs) ->
                      (* TODO ? *)
                      ()
                  );
                  k x
              | _ -> k x
            );
            V.klvalue = (fun (k,_) x ->
              match Ast.untype x with
              | FunCallSimple (name, args) ->
                  Common.push2 (Ast.name name) funcs;
                  k x
              | MethodCallSimple (var, tok, name, args) ->
                  Common.push2 (Ast.name name) methods;
                  k x
              | _ -> k x
            );
          }
          in
          let visitor = V.mk_visitor hooks in
          visitor.V.vtop top;

          let stat =
            (* {
line = Ast.line_of_info (def.f_tok);
parameters =
def.f_params +> Ast.unparen +> List.map (fun param ->
Ast.dname param.p_name,
param.p_default +> Common.fmap (fun (tok, static_scalar) ->
static_scalar
)
);
function_calls = !funcs;
method_calls = !methods;
instantiations = !instances;
}*)
            raise Todo
          in
          Some (Ast.name def.f_name, stat)

      | ClassDef _ | InterfaceDef _
      | StmtList _
      | Halt _
      | NotParsedCorrectly _ | FinalDef _ ->
          (* TODO ? *)
          None
    )
  in
  pr2 (s_of_file_stat stats);
  ()

let main =
  navigator_extract_functions Sys.argv.(1)
Something went wrong with that request. Please try again.