I'm currently working with an massive PHP codebase, witch is completely undocumented.
Here's the deal. All of the PHP code is included every page call. They build a simple include all kinda function that scan's the directory and includes all PHP files it can find. It works recursively for all the subfolders.
I would like to know what functions are used in what files and from which files those functions came.
I need to know this in order to switch to normale includes. Also it gives me more information about not used functions and files (since it a really old and big codebase, there is a lot of legacy in it).
It there a tool or something for PHP that can check this kind of stuff in a codebase?
NetBeans gives me pretty good visibility, for the spot inquiry. I'm interested to see if you get something with more value as a straight "audit" or inventory tool.
It's an old question so you seem to have found a solution. There's a php function: get_defined_functions(), get_defined_vars(), get_defined_constants(). You can use them in a page and you know the functions/vars/constants defined for use but not being used.
Related
I'm currently working on a school project that requires building a php application. I've started off by writing most of the functions the application will be needing however i'm not comfortable with the fact that i have to go through all my code to find and change the strings that get printed to the user, being that i make very frequent changes.
I was wondering if there was a better approach? Kind of the way android handles multi-language applications by having all the strings in one file. I understand that the main reason for this is for translation and since my system only has one language, i don't really require this.
So would it be wrong (I.e: it would somehow bring to further complications) to use the same approach?
I was thinking of a class with a bunch of static variables which could be referenced from the other functions.
What about what Mario is suggesting? I understand that this is not my case since i don't need multi-language support but being that the case would a db still be a bad idea?
Thanks
For translations, it depend on size of file, if it will be small one - sure. Can be all in one file. But if project will grow. Better is group strings and put in different files.
It will help only to find and edit it. Doesn’t matter for speed or error handling.
Different thing is separate php & html. But this also depend on project. In some it's OK. I think best is to have separate template global layout from php generated only output. In PHP you will any way put some html tags.
Functions, best - should be group in class. One class - one file.
I want to intercept CMS function and log it's arguments every time it is called. Namely I want to get all calls to t(). Problem is, in CMS core there are 1875 calls to t(), so it is not feasible to edit code that calls it. On the other hand, I can include my code snippet before first function call.
I have seen How to intercept PHP function call in my script?, but it's accepted answer assumes user can replace direct calls with calls to his new function. That's not my case. Linked article is about intercepting methods, so it seems not applicable, too.
Some of the other Drupal functions, like variable_set(), use global arrays, so I was able to plug into them by replacing global array with an object. Sadly, t() only uses global $language, and this variable is not an array searched against function's argument, so this approach is not possible and I need a native PHP solution.
Is there any way to intercept actual function calls, the way they are written? For now I actually hacked t(), but I really, really want to avoid it. Sadly, all solutions I found on the net seems to cope with interacting with own code, not with the code written by others in framework.
Last but not least: I can install additional software on dev machine. It's Ubuntu Server. So if any PHP plugin can be used, I'd like to know that. I would prefer solution that does not require any, but I can live with it.
This question is related to, but not a duplicate of, How to make strings from templates translatable on all pages they appear? on Drupal Answers. Now I'm trying to find a way to hack function _l10n_client_page_strings() of Localization Client module. But I'm sure actual function call interception would be interesting for many people.
Why I need it?
To answer this comment: Well, I believe it's generally interesting PHP issue, but my questions has also a practical purpose.
To translate website, one would traditionally:
Export translatable strings
Translate *.po file without seeing where and how string is used, possibly introducing errors (usually person translating site is not the one who manages it in day-to-day basis, so translator does not know the context)
Import file
Test translation, note errors and things that does not make sense
Go to 2.
With Localization Client procedure should look like that:
See page with untranslated / badly translated strings
Fix translations directly
Simpler, faster, less error-prone. Only problem with this module is that it does not collect strings actually used on page, but uses queries that "will not work reliably" (their own comment), so:
it lists a lot of strings, some of them are not really used on page, just happens to be in a module used in page generation
and worse, it misses some strings.
That's why I was trying to patch _l10n_client_page_strings() function with something that works slower, all right, but also is doing it's job directly. And I know some calls in template files may happen too late, but I'm willing to cross that bridge when I'll reach it. For now I need a reliable way to actually know these calls without hacking anything but the module I'm trying to force to work the way it should.
I have inherited a very messy project. There are at least 3 versions that I can tell in it.
Is there a utility that can trace the PHP code from the main index.php so that I can figure out what isn't being used and what is, or am I stuck doing a manual cleanup?
Thanks
*Update*
I don't think I've been clear about what I'm looking for, that or I'm not understanding how the products mentioned work. What I'm looking for is something that can run on a folder (directory) and step through the project and give me a report of which files are actually referenced or used (in the case of images, CSS, etc).
This project has several thousand files and it's a very small project. I'm trying to clean it up and when I do a "search in files" in my IDE I get 3 or 4 references and can't easily tell which one is the right one.
Hope that makes it a little clearer.
Cross referencing software really lets you explore which functions are used for what.
PHPXref is quite good..
For example Yoast used it to cross reference the Wordpress PHP code. Take a look at the Wordpress example of how powerful it is.
For example, start by browsing the WP trunk. Click on some of the file names on the left and observe how the required files are listed, along with defined classes and methods, etc., etc.
There are several utilities that can do this, what first comes mind is Zend Studio's built in Optimizer that will run through your files and issue notices on a per file basis, including unused variables, warnings, etc. Alternatively, you can run your program in E_STRICT and PHP will notify you of some of your issues.
Be very careful of such cleanup tools, especially in PHP or Javascript. They work reasonably well in languages like Java, but any language that allows Eval() can trip an automated tool up, sometimes in devilishly clever ways, depending on how clever the original code developer thought they were.
You need the inclued extension. You can generate include graphs using GraphViz, see below for example code.
There are some useful examples on PHP.net: http://www.php.net/manual/en/inclued.examples-implementation.php
You might want to check xdebug's code coverage, possibly as an auto_append. However, itś rather limited and it would require you to have either 100% test-cases (which I doubt as you say the project is a mess), or the tenacity to go through every possible action on the site, and even then you'll have to apply good judgement whether you can remove a portion of code because it isn't used, or leave it there because a certain condition just hasn't been met yet in your cases. On a side note: stepping through the code with xdebug's remote debugger has really helped me in the past to quickly get the different mechanisms & flows in unknown projects.
I would try opening the whole project in NetBeans PHP, its a great tool which we use for huge projects. You can easily see warnings and notifications and also follow usage of functions/classes easily. Try it!
I would recommend against automatic cleanups and the likes. Even if the code seems to work afterwards, I wouldnt sleep very well at night...
I'm trying to find the best pragmatic approach to import functions on the fly... let me explain.
Say I have a directory called functions which has these files:
array_select.func.php
stat_mediam.func.php
stat_mean.func.php
.....
I would like to: load each individual file (which has a function defined inside) and use it just like an internal php function.. such as array_pop(), array_shift(), etc.
Once I stumbled on a tutorial (which I can't find again now) that compiled user defined functions as part of a PHP installation.. Although that's not a very good solution because on shared/reseller hosting you can't recompile the PHP installation.
I don't want to have conflicts with future versions of PHP / other extensions, i.e. if a function named X by me, is suddenly part of the internal php functions (even though it might not have the same functionality per se) I don't want PHP to throw a fatal error because of this and fail miserably.
So the best method that I can think of is to check if a function is defined, using function_exists(), if so throw a notice so that it's easy to track in the log files, otherwise define the function. However that will probably translate to having a lot of include/require statement in other files where I need such a function, which I don't really like. Or possibly, read the directory and loop over each *.func.php file and include_once. Though I find this a bit ugly.
The question is, have you ever stumbled upon some source code which handled such a case? How was it implemented? Did you ever do something similar? I need as much ideas as possible! :)
One way you could pull something like this off is to put those functions into classes and then set up an __autoload function. If you are against wrapping the functions in classes than this solution probably won't apply to you. Personally I like it because it allows me to namespace my functions and share private methods between them.
First you set up your autoload function similar to this. You'll want to adjust the naming convention to fit your own style, and probably introduce some error handling, but this is just to get the basic idea across.
function __autoload($class_name){
require_once(strtolower("library/$class_name.class.php"));
}
Then anywhere in your code regardless of scope you can do something like this.
arrayFunctions::doStuff($myArray);
PHP will automatically try to include "library/arrayFunctions.class.php" and look for a method called "doStuff" in the arrayFunctions class.
I have issues with this idea. Hitting the file system to include a single function is very expensive in the terms of it lowering your max possible requests per second.
It's generally much better to load/parse five functions in a single file (static class?) and only use two of them (one stat call) rather than load two files for two functions (two stat calls).
Which obviously becomes even worse when you need all five functions.
To automatically load stuff when need, put your functions in classes and use autoloading.
For the name conflict, use namespaces (if you have PHP 5.3).
My PHP code is split between many files, and often I find myself using code like this this:
require_once( "$preIP/functions.php" );
The problem is that I have to keep using this a lot, and sometimes these statements are becoming redundant.
What kind of system do yo use/would recommend for keeping track of all the inter-dependencies in all the PHP files?
Might want to take a look at this:
autoloading
I've used it several times and it it magically worked so I never looked into it that deep.
One thing you could try is logging all the "require_once" calls to the browser, with something like FirePHP - if you don't want to change all your function calls from require_once to another, you could override the original function using the core code, and include there a call to the FB::log so you see every time a file is included, and where it was included from.
Using eclipse, you can dig right into the source code for PHP, and see what all the functions do, if I recall correctly. There must be a copy online somewhere, but a quick search didnt find it - hope someone comments with a link!