ralphNOSPAM@pri memail.com wrote:[color=blue]
> Is there a PHP script that can find unused variables? I'd like to
> 'clean up' my scripts.
>
> Thanks...
>[/color]
There must be out-of the box scripts out there, try Google to trace them
(e.g. by searching for php+checkstyle) .
Meanwhile, I have been playing around a bit and created the following
(probably useless) script, which uses the simple criteria "if the var is
mentioned once, it must be unused".
On Tue, 11 Oct 2005 18:45:23 +0200, Janwillem Borleffs
<jw@jwscripts.c om> wrote:
[color=blue]
>ralphNOSPAM@pr imemail.com wrote:[color=green]
>> Is there a PHP script that can find unused variables? I'd like to
>> 'clean up' my scripts.
>>
>> Thanks...
>>[/color]
>
>There must be out-of the box scripts out there, try Google to trace them
>(e.g. by searching for php+checkstyle) .
>
>Meanwhile, I have been playing around a bit and created the following
>(probably useless) script, which uses the simple criteria "if the var is
>mentioned once, it must be unused".
>
>The source:
>
>http://playground.jwscripts.com/double-vars.phps
>
>
>Enjoy :-)
>
>JW[/color]
ralphNOSPAM@pri memail.com wrote:
[color=blue]
> Is there a PHP script that can find unused variables? I'd like to
> 'clean up' my scripts.[/color]
$ cat test.php
<?
$a = 1;
class A{}
function f($b, $c)
{ $d = $b; }
?>
$ phplint --print-context text.php
function f($b, $c)
\_ HERE
6: notice: undefined type for argument `$b'. I will assume `mixed'. Hint:
either use an explicit type (example: `/*.int.*/ $b') or assign a default
value (example: `$b=123').
function f($b, $c)
\_ HERE
6: notice: undefined type for argument `$c'. I will assume `mixed'. Hint:
either use an explicit type (example: `/*.int.*/ $c') or assign a default
value (example: `$c=123').
7: notice: variable `$d' assigned but never used
6: notice: variable `$c' assigned but never used
6: notice: guessed signature of the function `f' as void(mixed, mixed)
2: notice: variable `$a' assigned but never used
6: notice: unused function `f'
4: notice: unused class `A'
Overall test results: 0 errors, 0 warnings.
ralphNOSPAM@pri memail.com wrote:[color=blue]
> I ran your script but it didn't display anything!
>[/color]
Then, there where probably no unused vars detected (as in all vars where
found more than once).
Be aware that the script is *very* basic and certainly not failproof and
has a lot of limitations. As an example, the following script will pass
while it shouldn't:
Umberto Salsi wrote:
[color=blue]
> ralphNOSPAM@pri memail.com wrote:
>[color=green]
>> Is there a PHP script that can find unused variables? I'd like to
>> 'clean up' my scripts.[/color]
>
> $ phplint --print-context text.php[/color]
phplint is an interesting tool but it was originally designed for the
express purpose of making PHP behave as if it were not dynamically typed -
so you're likely to have problems seeing the wood for the trees unless you
use PHP to run your Java/C++ programs ;) Although I'll concede that PHP
might benefit from something like Perl's strict mode.
IIRC the Zend IDE will do this and can also track code coverage - which is
likely to be much more beneficial.
Colin McKinnon <colin.thisisno tmysurname@ntlw orld.deletemeun lessURaBot.com> wrote:
[color=blue]
> phplint is an interesting tool but it was originally designed for the
> express purpose of making PHP behave as if it were not dynamically typed -
> so you're likely to have problems seeing the wood for the trees unless you
> use PHP to run your Java/C++ programs ;) Although I'll concede that PHP
> might benefit from something like Perl's strict mode.
>
> IIRC the Zend IDE will do this and can also track code coverage - which is
> likely to be much more beneficial.[/color]
IMHO, Perl (with or without the "strict" option) is the stone age of the
safe programming, and I will not consider it here. I never used the Zend
IDE, but I intend to try it.
The aim of the PHPLint project is not to "turn PHP into Java", but
instead to provide a development and debugging tool without boring the
programmer with lots of useless code, so preserving the easiness of
the language with minimal impact on the habits of the PHP programmers.
Most of the existing code can be validated effectively with PHPLint,
possibly with only few changes. For example, converting the phpmailer
class (http://phpmailer.sourceforge.net/) from "plain PHP source" to
"PHPLint-compliant source" took me about one hour: most of the methods
were used before being declared (PHPLint is a single-pass parser) so I
needed to move them upward; for some of the methods PHPLint was unable to
detect the type of the returned value, and some formal arguments needed
to be formally declared, like in this example:
/**
* Adds a "Cc" address. Note: this function works
* with the SMTP mailer on win32, not with the "mail"
* mailer.
* @param string $address <-- REDUNDANT
* @param string $name <-- REDUNDANT
* @return void <-- REDUNDANT
*/
function AddCC(/*. string .*/ $address, $name = "") {
$cur = count($this->cc);
$this->cc[$cur][0] = trim($address);
$this->cc[$cur][1] = $name;
}
Note that only the type of the argument $address required to be
specified, since PHPLint was unable to guess it by itself: this is
the only change made. The types of $name (string) and of the local
variable $cur (int) are guessed successfully, so they do not require
to be declared formally. Also the type of the returned value (void) is
detected automatically by PHPLint. Note too that the declarations of the
PHPDocumentator (@param, @return) are now redundant (I hope to complete
the work either integrating PHPLint inside PHPDocumentator or vice-versa).
Doing that, I found a little bug: phpmailer::SMTP Debug is boolean,
where SMTP::do_debug is int, and the two values are mixed inside
phpmailer::Smtp Connect(). In this case, PHP convert the TRUE value into
int(1) so the program runs without errors, but probably this isn't what
the programmer intended to do.
All the work done on this class is not merely the validation of its source:
now I can to validate all the other programs that require it.
PHPLint and phpdocumentor (Was Re: Finding Unused Variables)
Umberto Salsi wrote:[color=blue]
> Colin McKinnon <colin.thisisno tmysurname@ntlw orld.deletemeun lessURaBot.com> wrote:[/color]
<snip>[color=blue]
> /**
> * Adds a "Cc" address. Note: this function works
> * with the SMTP mailer on win32, not with the "mail"
> * mailer.
> * @param string $address <-- REDUNDANT
> * @param string $name <-- REDUNDANT
> * @return void <-- REDUNDANT
> */[/color]
<snip>[color=blue]
> to be declared formally. Also the type of the returned value (void) is
> detected automatically by PHPLint. Note too that the declarations of the
> PHPDocumentator (@param, @return) are now redundant[/color]
<snip>
As said earlier, your PHPLint is great. But, I don't think, the
declaration you mentioned will turn it redudnant--as usually the
practice is to provide description for the variable like:
@param string $address Email address of the recipient
Comment