Welcome Guest, Not a member yet? Register   Sign In
Tracking where method is beeing called
#1

[eluser]Unknown[/eluser]
Hello,

We are having a problem here at work.
We are a developer team and we are working at some project.
Today i need to change a method at some Models.
The problem is,i have no idea where it's beeing used.
So i am afraid to change it, and break some one else code.
My question is, there is any kind of debuging that allow me to track it?
I want to know where this function is beeing used.

Thank you.
#2

[eluser]Sudz[/eluser]
Hi,

You can use
Code:
debug_backtrace()

For example, suppose your function name is test, use below code
Code:
function test( )
{
    $backtrace = debug_backtrace();

    print_r( $backtrace );
}

It will print the details from where the test method is being called.
#3

[eluser]hyperfire[/eluser]
Easy!

1) Enable log on your index.php

2) Load the url helper

3) Put this on your model:

Code:
log_message('info', 'The methox #INSERT METHOD NAME# was called by ' . current_url());

Let the app run for few hours/day. Download the log and search for your info message.
#4

[eluser]Unknown[/eluser]
Tks for the reply.
But is not that,what i am looking for.
I will try give an example.

Code:
function test1() {
   echo "function test1";
}

function test2() {
   echo "function test2";
   $this->test1();
}

function test3() {
    echo "function test3";
}

function test4() {
    echo "function test4";
    $this->test1();
}

I have the function test1 being called at function test2 and function test4.
So i need some code to tell me that.
That is what i need.

Tks once again.
#5

[eluser]Otemu[/eluser]
Hi,

Some things come to mind:

Certain IDE's allow for you to search for functions so maybe investigate that.
Just var_dump(debug_backtrace()); in your function, every time that function is called your know where it coming from
Can you not just do a simple find in all files for the function "test1();"?

Hope that helps





#6

[eluser]TheFuzzy0ne[/eluser]
Debugging is not the solution. Searching files is. By debugging, you can't be sure you've covered every single occurrence of a call to that method. With searching files you can be pretty sure you've covered every avenue. All you need is a good file search app. I recommend [url="http://www.mythicsoft.com/page.aspx?page=download&type=agentransack"]Agent Ransack[/url]. It's just awesome. Smile

As a fail-safe, you can keep the old method name and use it as an alias for the new method name. This is untested, but might help.
Code:
function __call($method, $args)
{
    // Store your old_method => new_method map here.
    $map = array(
        'old_method_name' => 'new_method_name',
    );
    
    // If the requested method has been mapped, log the backtrace and set the new method.
    if (isset($map[$method]))
    {
        file_put_contents(APPPATH . 'logs/backtraces.txt', print_r(debug_backtrace(), TRUE));
        $method = $map[$method];
    }
    
    // If the method doesn't exist, throw an exception.
    if ( ! method_exists($this, $method))
    {
        throw new BadFunctionCallException('Call to undefined method ' . get_class($this) . '::' . $method . '()');
    }

    // Call the method.
    return call_user_func_array(array($this, $method), $args);
}

With a bit of luck, your log file should remain empty, but if it doesn't, you should be able to locate any offending code. Probably not the DRYest example, but since you'll be phasing it out anyway, I don't think it should be an issue. You just need to copy this into each model where you're changing method names, and adjust the map accordingly. If you wanted to, you could put this into your MY_Model class, and move the map out so it becomes a property of your model, but I think that might be overkill, since you won't be using it on all of your models.
#7

[eluser]boltsabre[/eluser]
Quote:The problem is, i have no idea where it’s beeing used.
Well, it sounds like the development team is not documenting things correctly! However, even if you have good documentation all it takes is one person not to update it and it's useless for your particular situation.

Another thing that comes to mind is that you (not you personally, but your development group as a whole) have been making the functions too big and too complex. This is a classic example of code smell/stink!!!

http://en.wikipedia.org/wiki/Code_smell
http://c2.com/cgi/wiki?CodeSmell

A good function should only do one thing and do it good!!! If it's doing more than one thing it should be called "I_am_a_piece_of_code_that_if_gets_changed_may_break_my_app_because_I_am_being_used_for_multiple_purposes_and_no_one_really_knows_anymore_what_my_purpose_is" .

But as per what TheFuzzyOne said, the only real way to track down all uses of it is to do a search of the entire code base.





Theme © iAndrew 2016 - Forum software by © MyBB