views:

142

answers:

6

I have been working on a small php app (400K total). But in the process of building and learning I have a lot of junk files.

I didn't do my job and name them properly, ie demo1.php ect...

Is there a way to do dependency checking? Or is it just delete, refresh and repeat? Then undelete when needed?

A: 

Sounds like you need to start with version control system. If your project is already mucked up then you may need to just bite the bullet and clean it out manually.

Mike B
I'm using subversion, however I still committed my test files. I think I'll have to go at it manually.
shaiss
@shaiss why would you commit test files? You're not using version control correctly if you committed the same file under a different name over and over.
Mike B
not the same file. ie. I created readini and figured out how to read an ini file we had and pull values. Then I created testEmp and did some testing there. Your right, I probably shouldn't have commited them in the first place.
shaiss
+3  A: 

I am in the same situation right now, cleaning up a very big project parts of which are years old.

What I am finding most important to do the job:

  • Version control. I use TortoiseSVN because of its great Windows Explorer integration. I try to check in after each change to avoid much pain and suffering.

  • A very good and convenient search function to make a basic dependency check (Where is $xyz->loadFromOtherSource used?). I use nusphere PhpEd but there is a lot of tools out there with good search functions.

Other than that, there are no reliable refactoring / dependency check tools for PHP that I know of.

Pekka
+4  A: 

There's no magic tool that will do this for you, there's only functionality that will help you implement your own solution.

The function you want is

get_included_files();

This will return an array of any file that's been included so far. Put this at the end of your bootstrap file (or at the end of all your individual files) and you can get a list of every file that's been included or required. This will NOT report on files opened with file_get_contents, fopen, etc. That's why its a good idea to have some kind of wrapper functions/classes that will call these functions for you (allowing you to hook into the actions if need be)

The approach I'd take it to add in code that logs the included files somewhere and then let your app run for a day or two (or exercise all its functionality yourself) This should give you a complete list of files that your project is actually using, allow you to clean up files that don't appear on the list. This logging could be as simple as

file_put_contents('/tmp/files.txt',print_r(get_included_files(), true),FILE_APPEND);
Alan Storm
Sounds promising but a bit over my head. This would take me longer to figure out than manually cleaning out.
shaiss
A: 

That's going to be hard to accomplish - there's no way for an IDE or similar tool to know which PHP files are intended as includes in some way vs those that are supposed to be accessed directly by the end user.

Not to mention that PHP lets you do really dynamic file inclusion such as this

$page = $_GET['page'];
include( 'templates/' . $page . '.php' );

In the end you're just going to need lots of due diligence.

Peter Bailey
A: 

I had a similar problem a few years ago when taking over code from anther developer who left our company. A complete rewrite was not an option as the code was already in production. but the code was unmanageable in its current state.

1) I created a repository of the original source code.

2) I created test code that tested the functionality of his application.

3) I went thou the process of removing, moving, and cleaning up functions until the source code was more manageable. Checking in source code after each successful change.

Steven smethurst
A: 

Hy you could autoload your files. ex. myfile.php the class should also be named myfile

    <?php 
function __autoload($class_name) {
        require_once $class_name . '.php';
    }

    $obj  = new MyClass1();
    $obj2 = new MyClass2(); 
    ?>
streetparade
Sorry, I don't follow
shaiss
That's only going to work for class files that have been included. It's not going to cover anything else.
Alan Storm
So i think cleaning begins first in your head. Thats the place where you should clean first.
streetparade