How to catch php spelling errors with static analyzers? - php

I was made a mistake yesterday and spent hours to fix it. I have method like this
{
if (isset($data['y'])) {
$this->y = $data['y'];
}
if (isset($data['z'])) {
$this->y = $data['z']; // <- error here
}
}
And yes, I assign $this->y two times instead of one y and one z :-(
So question: can any static analyze tools catch such errors? I have PHP Storm and Rector, PHPStan, PHP CS Fixer in my CI toolchain but they missed this error.

This isn't so much an answer, but it's too complicated to put in a comment.
As the comments pointed out, there's simply no way for a robot to figure out that what you wrote isn't what you intended. The easiest solution is to live with human frailty and debug your code.
But that doesn't mean you can't write your code to better express your intent. Consider:
{
$fields = ['x', 'y'];
foreach ($fields as $field) {
if (isset($data[$field]) {
$this->$field = $data[$field];
}
}
}
Now you have expressed in you code that you only want to assign like-named fields.

Related

Selenium PHPUnit - Not able to find a text on page

There is a problem regarding selenium PHPUnit.
PHPUnit version: 3.7.21
OS: Windows7
Selenium Server: selenium-server-standalone-2.48.2
I am getting a very weird situation, that I have a text present on the page, and using assertTextPresent() function on that page only. But it continuously fail.
Earlier I thought, may be it because of page not loaded properly. So I just use pause() for page to load. And now I am able to see the text on page.
I also captured the screenshot, In that screenshot I am also able to see that pattern.
But there is something wrong, that this function is not able to find text in the page.
Any help will be appreciated.
Thanks.
Can you give a more specific example of what steps you're performing and what parameters you're using? Anyway, you could wait for an element to be present on the page like this:
for ($second = 0; ; $second++) {
if ($second >= 30) $this->fail("Element not found");
try {
if ($value == $this->getText("$locator")) break;
} catch (Exception $e) {}
sleep(1);
}
where $value is the text you're looking for and $locator is the path where you expect to find the text. OR (if you don't care about where the text needs to be):
for ($second = 0; ; $second++) {
if ($second >= 30) $this->fail("\n Text not found");
try {
if ($this->isTextPresent("$value")) break;
}
catch (Exception $e) {
}
sleep(1);
}
You can use this code as a workaround
$txt = $this->byXPath("insert-xpath-of-text-here")->text();
$this->assertTrue($txt,"text-to-be-found","some-random-error-message-here");
or
$this->assertTrue($this->byXPath("insert-xpath-of-text-here")->text(),"text-to-be-found","some-random-error-message-here");
I figured that indicating the locator of the text to be found might solve the issue
$this->waitForPageLoad(1000);
You are using this method
assertElementExistsWithText_W();

php - turn-on/off-able function to print debugging messages

I am quite new to programming, when I develop my program I use a simple strategy to debug it: I write the program to print along the debugging messages as it operate the significant statements, for example
function foo1($number)
{
//foo_print("I am function foo1({$number}). <br/>");
//foo_print("I am going to increase 'count' by {$number}. <br/>");
$GLOBALS["count"] = $GLOBALS["count'] + $number;
//foo_print("Now 'count' = {$GLOBALS["count"]}, I finished my task, BYE BYE. <br/>");
}
function isFoo($number)
{
//foo_print("I am function isFoo({$number}). <br/>");
//foo_print("I am checking if the number < 3 or not, if so, it is Foo, if not, it is not Foo. <br/>");
if($number <= 3)
{
//foo_print("Found that number = {$number} <= 3, I return true, BYE BYE. <br/>");
return true;
}
//foo_print("Found that number = {$number} > 3, I return false, BYE BYE. <br/>");
return false;
}
I call them debugging messages but, as you see, they're actually the thoroughly comments describing what does the program do on each line. I just write the function foo_print() to print them out when I am debugging the program. And comment them out in real use.
Instead of inserting and removing the comment sign '//' line by line in and out when switch between real run mode and debugging mode, I have the function foo_print to do the work: It can be set to turn on or off.
define(FOO_PRINT, 1)
function foo_print($message)
{
if(FOO_PRINT) print $message;
// if FOO_PRINT == 0 then do nothing.
}
But I think this method is ineffective, it has to check FOO_PRINT every time before printing a message.
My question is either or both of the following
Can I do something to tell php to ignore my foo_print() function when I don't want to use it?
Perhaps, instead of using foo_print function, I should write the messages in plain comment style using '//' sign and then tell php interpreter to print those comment messages when in debugging mode. Can I do that?
I think, other than debugging ease, this method will be of advantage that it can help me understand the program when I come back to see it in later days. (It very long and complicated for me that I believe I will forget it soon.)
I found it very complicated for me now to use advanced IDEs and debugging tools to develop my program. I believe some of these advanced debugging tools can do something similar to what I want, but I've tried on PHP-eclipse and xdebug for a week and it got me nowhere. thank you very much.
You could define two functions, one of which outputs the debug data and the other one doesn't. Then use a variable name to contain the name of the function you want to call and do your debugging by calling the function in the variable. Like this:
function debug_print($data) {
echo $data;
}
function debug_none($data) {
}
$debug = 'debug_print';
$debug('Testing one'); // This prints out 'Testing one'
$debug = 'debug_none';
$debug('Testing two'); // This doesn't print out anything
If you do this, don't forget to add global $debug to any functions that want to use the function.
EDIT: There is also a more object oriented way to achieve the same result. You could define an interface and write a couple of implementations for it, allowing you to choose which one to use at runtime.
$debugmode = true;
interface Debugger {
public function out($data);
}
class EchoDebugger implements Debugger {
public function out($data) {
echo $data;
}
}
class NullDebugger implements Debugger {
public function out($data) {
// Do nothing
}
}
if($debugmode)
$debugger = new EchoDebugger();
else
$debugger = new NullDebugger();
$debugger->out('This will be output if $debugmode is true');
No bud,
there is no such thing possible, and you have to define a condition every time.
This cannot be done in code of php

Is it possible to get the statements within a method in PHP?

function mainFunction() {
functionA(5, "blah");
functionB("ok", "whatever");
}
How to write a function GetFunctions that returns the functions within mainFunction?
How to call them with the parameters given in mainFunction?
How to call them as follows?
foreach (GetFunctions(mainFunction) as $function) {
print "Calling function $function: ";
call($functionA); // called with parameters(5, "blah")
}
Working in PHP 5.2.8
EDIT: OK, here's a more complete explanation. I tried to keep it simple to make it easy to understand, but apparently that wasn't a good idea.
The goal is to call each assertion within a given static method. I am writing a testing framework. Each assertion returns true or false.
I am calling the methods as follows.
$methods = get_class_methods('LibraryTests');
foreach ($methods as $method) {
if ( StartsWith($method, 'Test') ) {
print "calling: " . $method . ": ";
call_user_func('LibraryTests::' . $method);
}
}
The above code calls each method within the class, but I want to call each assertion individually and track the result (true/false). CallAssertion is supposed to call each assertion (such as TestUnit::AssertEqual(GetFormattedHour(5), "5 PM");). This is the method that I am asking about.
Here is the class:
class LibraryTests extends TestUnit {
static $success = 0;
static $failure = 0;
static $total = 0;
static function CallAssertion($assertion) {
self::$total += 1;
if ($assertion) { self::$success += 1; }
else { self::$failure += 1; }
}
static function TestGetFormattedHour() {
TestUnit::AssertEqual(GetFormattedHour(5), "5 PM");
TestUnit::AssertEqual(GetFormattedHour(16), "4 PM");
}
So, the question is, how to write CallAssertion?
You can't.
Instead, create a class and use reflection to get its methods.
Regardless, you'll want to figure out why this is necessary and see if there is an entirely different approach you can use.
(If this is for debugging purposes, you can use debug_backtrace to inspect but its purpose is not for calling functions as you have described in your question.)
Hmm, what problem are you actually trying to solve. To me it sounds like you're trying to inspect the call stack at runtime. If so, I'd suggest just using debug_backtrace() (src).
I wouldn't suggest using that function in production as much though, as it's a rather heavy hit on your code.
One possibility would be to do a file_get_contents on the PHP file that contains main_function, then go through it to parse out main_function and the functions it calls. Of course, I don't know your situation so that might not work.
You can do this with:
http://php.net/manual/en/function.token-get-all.php
Probably a bad idea, but good luck!

php activerecord save does not work in codeigniter

I use the latest code igniter (2.0.3) and php-active 0.0.1.
All are working fine except save();
Code:
if($_POST)
{
$entry= Customers::find_by_routeid('4');
$entry->routeid=5;
$entry->save();
}
Here's my problem: for some reason that I cannot understand the above code does not work, but if I take the code out of if ($_POST), it works fine.
What I am doing wrong?
EDIT:
Thanks Damien Pirsy $this->input->post() does the trick, but when I uncomment the comments in the code the problems returns.
The code now is:
if($this->input->post())
{
$id = $this->input->post('id');
$oldRoute = $this->input->post('oldRoute');
$newRoute = $this->input->post('newRoute');
$entry= Customers::find_by_routeid($this->input->post('oldRoute'));
$entry->routeid=$this->input->post('newRoute');
$entry->save();
/*
if($oldRoute<$newRoute)
{
for ($i=$newRoute; $i>$oldRoute; $i--)
{
$element = Customers::find_by_routeid($i);
echo $element->routeid -= 1;
$element->save();
}
}
*/
}
The elements new IDs ($element->routeid -= 1;) are echoing right, but I have the same problem as in the beginning and neither of two saves work.
You didn't provide much details or debug info, so I'll just guess: try using the CI's native post handler instead. You should have var_dump()ed the $_POST array, see if isset() or not, also, since you're using it as a condition
if($this->input->post())
{
//...
}
UPDATE:
Since we're talking about Post variables, don't assume they're exactly as you want them. Keep in mind that $this->input->post('field') returns FALSE when the index is not present; that might well brake your if condition.
Assuming you need numbers to do this, you can do a check like
if($this->input->post('newRoute') AND is_numeric($this->input->post('newRoute'))
{
$newRoute = $this->input->post('newRoute');
}
else
{
// give it a default value, or raise an error, for example. If you need this
// variables, and need them to be numbers, you cannot go on in case these
// conditions are not met, right?
}
And the same for $oldRoute.
And yeah, OK, maybe you can write a cleaner code than mine, but you get the picture ;)

Finding PHP dependencies

Are there any tools that can list the names of classes used by a PHP file?
For example, if I ran it on this file:
<?
class Test {
public function __construct(Obj1 $x) {
$y = new Obj2();
$str = "Obj3";
$z = new $str();
}
}
?>
it would report "Obj1" and "Obj2". If it were really smart it might report "Obj3" as well, but that's not essential.
I'm trying to package up some code, and I want some help making sure that I didn't miss any dependencies.
There's something called PHP_Depend, which can graph the number of dependencies, but can't report what they are.
UPDATE: I didn't find a real solution, but I figured out something close enough for my purposes. You can tokenize a file, and search for all T_STRING tokens. This will give you all class names mentioned in the file. It will also give you other things, like function names and constants. But if your class names are easy to distinguish (e.g. they have initial caps), then this shouldn't be a problem.
$contents = file_get_contents($path);
$tokens = token_get_all($contents);
foreach ($tokens as $token) {
if (is_array($token) && $token[0] === T_STRING) {
echo $token[1]."\n";
}
}
There's PHPXref, which is a PHP cross referencing document generator.

Categories