Software to document code? - php

I need to document and create a list of variables in my code as part of my A Level computing coursework, however my PHP script is over 6,000 lines in total.
Does anyone know of any software that will display a list of variables within my code? I really don't feel like going through all of my code and hand-picking out about a thousand variables :(
Thanks in advance.

PHP's tokenizer allows you to parse the script and could be used to pick out all the variables defined/used

In PowerShell this would be quite trivial:
Select-String '\$[\w_]+' foo.php -AllMatches |
Select-Object -ExpandProperty Matches |
Select-Object -Unique -ExpandProperty Value
or shorter:
sls -a '\$[\w_]+' foo.php|%{$_.Matches}|select -u -exp Value
Adapt accordingly for Perl, sed, or whatever you like to use. The basic idea would be the same.

Depending on how detailed you need to be, Xdebug might be useful here.
You should also look into the Reflection classes: http://php.net/manual/en/book.reflection.php Writing something up using Reflection would give you control over the output.

$ grep -oh "\$[a-zA-Z_]*" file.php | sort | uniq
or
$ grep -R -oh "\$[a-zA-Z_]*" src_dir | sort | uniq

Related

Loop from specific offer in XML file

Is there a way to loop through an XML file from a specific element / node.
For instance if I want to start from <offer id="a2a9d7a3a520de69e8e06f3e53df1c49"> in this XML feed: http://pastebin.com/n5myzcz1
Is is then possible to load all offers after that ID?
I was going to suggest XPath but since you mention that the file is 1GB, you should use a streaming parser such as XMLReader (which I now read that you are already using).
You are bound to have a linear search because you don't know where the element you want is so you have to go through all of them until you find it.
EDIT
Just an esoteric idea could be using shell_exec to grep the file to find the line where the ID is and then cutting the file from that line using sed or equivalent.
EDIT 2
Ahhhh, this was fun!!
$line = intval(shell_exec('grep -n a2a9d7a3a520de69e8e06f3e53df1c49 orders.xml | cut -d : -f1'));
$totalLines = intval(shell_exec('wc -l orders.xml'));
echo shell_exec("sed -n '".$line.",".$totalLines."p' orders.xml");
Maybe you can just put 9999999999 instead of caculating the total lines. Not saying you should of course ;) ;)
I don't know if this is faster than just going through the file with XMLReader but I guess it's up to you to decide if it's worth it.
I hope this can give you further ideas to solve your problem.

PHP exec very slow processing simple 3-pipe grep

I've read here and cannot really understand how to speed up my simple exec() which basically looks like this:
zcat access_log.201312011745.gz | grep 'id=6' | grep 'id2=10' | head -n10
I've added ini_set('memory_limit', 256); to the top of the PHP document, but the script still takes about 1 minute to run (contrasted with about near instant completion in Penguinet). What can I do to improve it?
I would try some of the following:
Change your exec to just run somethig simple, like
echo Hello
and see if it still takes so long - if it does, the problem is in the process creation and exec()ing area.
If that runs quickly, try changing the exec to something like:
zcat access_log.201312011745.gz > /dev/null
to see if it is the "zcat" slowing you down
Think about replacing the greps with a "sed" that quits (using "q") as soon as it finds what you are looking for rather than continuing all the way to end of file - since it seems (by your "head") you are only interested in the first few, not all occurrences of your strings. For example, you seem to be looking for lines that contain "id=6" and also "id2=10", so if you used "sed" like below, it may be faster because "sed" will print it and stop immediately the moment it finds a line with "id=6" followed by "id2=10"
zcat access_log.201312011745.gz | sed -n '/id=2.*id2=10/p;q'
The "-n" says "don't print, in general" and then it looks for "id=2" followed by any characters then "id2=10". If it finds that, it prints the line and the "q" makes it quit immediately without looking through to end of file. Note that I am assuming "id=2" comes before "id2=10" on the line. If that is not true, the "sed" will need additional work.

php exec suggestions/alternatives

Can anyone give me some pointers with regard PHP command execution and best practice?
Im currently trying to parse some netbackup data, but i am running into issues related to the massive amount of data the system call is returning. In order to cut down the amount of data im retreiving I'm doing something like this:
$awk_command = "awk -F, '{print $1\",\"$2\",\"$3\",\"$4\",\"$5\",\"$6\",\"$7\",\"$9\",\"$11\",\"$26\",\"$32\",\"$33\",\"$34\",\"$35\",\"$36\",\"$37\",\"$38\",\"$39\",\"$40}'";
exec("sudo /usr/openv/netbackup/bin/admincmd/bpdbjobs -report -M $master_name -all_columns | $awk_command", $get_backups, $null);
foreach ($get_backups as $backup_detail)
{
process_the_data();
write_data_to_db();
}
Im using awk to limit the amount of data be received. Without it i end up receiving nearly ~150mb of data, and with it, i get a much more manageable ~800k of data.
You don't need to tell me that the awk shit is nasty - i know that already... But in the interests of bettering myself (and my code) can anyone suggest an alternative?
I was thinking of something like proc_open but really not sure if that is going to provide any benefits.
Use exec to write the data to a file instead of reading it whole into your script.
exec("sudo /usr/openv/netbackup/bin/admincmd/bpdbjobs -report -M $master_name -all_columns | $awk_command > /tmp/output.data");
Then use any memory efficient method to read the file in parts.
Have a look here:
Least memory intensive way to read a file in PHP

Parsing SNMP responses with PHP

I'm currently successfully reading out several properties on our switches over SNMP with php. Now i'm looking at making the resulting output of snmpget and snmpwalk actually usefull for the consumers of our API's.
Problem is that the responses look like this: INTEGER: up(1) and INTEGER: 10103 ...
Is there any convention/standard on how to parse this response format or is the response vendor specific for each device we are trying to read?
Is there by any chance already a PHP library, function or extension that can cast these responses in php native variables or at least something usefull that we can work with?
UPDATE:
I've found out a few new things namely that there are indeed several libraries in php that can parse binary ASN.1 strings which basically are BER encoded strings if i'm right. Problem is that i can't seem to find a way to get the binary data from the devices with php ...
You can simply use this function at the beginning of your script :
snmp_set_quick_print(TRUE);
It will returns only the value you are searching for, without the leading "INTEGER" or so ;)
Hope this helps !
I'm not sure about your particular PHP methods, but the difference between your two INTEGER examples is likely to be whether your system has an SNMP MIB corresponding to the OID (e.g. to determine that 1 means "up").
If you only want the integers, you should be able to pass a parameter to your get or walk command. For example, net-snmp's snmpget or snmpwalk commands will take -Oe to remove symbolic labels. From the manpage:
$ snmpget -c public -v 1 localhost ipForwarding.0
IP-MIB::ipForwarding.0 = INTEGER: forwarding(1)
$ snmpget -c public -v 1 -Oe localhost ipForwarding.0
IP-MIB::ipForwarding.0 = INTEGER: 1
If you are parsing net-snmp output, I recommend reading the snmpcmd man page as it has a lot of output options that will interest you especially the display of other types such as timeticks and strings.
If you do want to retrieve SNMP in PHP you could look at how Cacti does it.

how to count lines of code in a single file

I'd like to count the number of lines of code in individual files.
sloccount sums up the entire directory.
Is there a program that will return the LOC for a single PHP file?
According to the documentation, just use the --details option.
Try this:
find /path/of/your/project | xargs wc -l "{}" \;

Categories