How Do You Clean up a PHP Project?
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?
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);
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.
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.
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.
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.
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();
?>
精彩评论