Solved

What the smoke is "$encounter?"

Posted on 2016-07-26
4
68 Views
Last Modified: 2016-07-27
Working on deconstructing a function so I can troubleshoot it. It's been an exhausting couple of weeks, but I can see some light! Here's the most recent puzzle:

protected function printEncounters($starting_y, $break_on_page = true) {
        $data = $this->statement->getData();

        $unrolled_data = $data['unrolled_data'];
        $rolled_data = $data['rolled_data'];

        $num_unrolled = count($unrolled_data);
        $num_rolled = count($rolled_data);
        $this->cursor_y = $starting_y;
        $this->cursor_x = $this->sidemar;
        $margin = .1 * $this->in;
        $table_width = 5.1 * $this->in - $margin;
        $bottom_limit = $break_on_page ? $this->tear_line : (11 * $this->in) - $this->sidemar;

        if ($unrolled_data || $rolled_data) {
            
            //Loop through Unrolled Data
            while ($this->current_index < $num_unrolled + $num_rolled) {

                //Header
                if (($this->current_index === 0 || $this->current_index === $num_unrolled) && $this->broken_tbl === false) {
                    //Lets add the heading in
                    $heading;
                    if($this->activity_heading === false) {
                        $heading = "Activity on your account:";
                        if($num_unrolled * $num_rolled > 0) {
                            if ($this->current_index === $num_unrolled) {
                                $heading = "Older activity:";
                            } else if($this->current_index === 0 ) {
                                $heading = "New activity on your account:";
                            }
                        }
                    } else {
                        $heading = $this->activity_heading;
                    }
                    $this->p->fit_textline($heading, $this->sidemar,$this->cursor_y + 14 * $this->pt, "
                        font=$this->FONT_TITILLIUM
                        fontsize=".(14*$this->pt)."
                        fillcolor=$this->blue
                        ");
                    $this->cursor_y += 14 * $this->pt + $margin;
                }


                    $i = $this->current_index;
                    $encounter;

                    if($i < $num_unrolled) {
                        $encounter = $unrolled_data[$i];
                    } else {
                        $encounter = $rolled_data[$i - $num_unrolled];
                    }

                    if($this->broken_tbl === false) {
                        $tbl = $this->getEncounterTable($encounter);
                    } else {
                        $tbl = $this->broken_tbl;
                    }
                    
                    if ($break_on_page) {
                        if( ($this->drawstatus = $this->drawEncounterTable($tbl, $encounter, $this->cursor_y, $bottom_limit - .5 * $this->in)) != 1) {

                            if($this->drawstatus === 0) $this->broken_tbl = $tbl;
                            else $this->broken_tbl = false;

                            $this->p->fit_textline("Activity continues on following page", $this->sidemar + $this->leftcol_width / 2,  $bottom_limit - .25 * $this->in, "
                                font=$this->FONT_TITILLIUM
                                fillcolor=$this->black
                                fontsize=" . 14 * $this->pt . "
                                position={center center}
                                ");
                            return false;
                        } else {
                            $this->broken_tbl = false;
                        }

                    } else {
                        
                        while(($this->drawstatus = $this->drawEncounterTable($tbl, $encounter, $this->cursor_y, $bottom_limit - .5 * $this->in, ($this->drawstatus === 0 || $this->broken_tbl))) != 1) {
                            //Activity continues blurb
                            if ($this->drawstatus === 0) $this->cursor_y += $this->p->info_table($tbl, "height") + .5 * $this->in;
                            $this->p->fit_textline("Activity continues on following page", $this->sidemar + $this->leftcol_width / 2, $bottom_limit - .25 * $this->in, "
                                font=$this->FONT_TITILLIUM
                                fillcolor=$this->black
                                fontsize=" . 14 * $this->pt . "
                                position={center center}
                                ");
                            $this->nextPage(true);
                            $this->cursor_y = $this->sidemar;
                        }

                        $this->broken_tbl = false;

                    }    

                $this->cursor_y += $this->p->info_table($tbl, "height") + .3 * $this->in;
                $this->current_index++;
            }

        }

        $bar_height = .5 * $this->in;
        $height_mod = $this->STATEMENT_TYPE === 2 ? 8 * $this->pt : 0;
        $bar_height += $height_mod;

        if ($this->cursor_y + $bar_height > $bottom_limit) {
            $this->p->fit_textline("Activity continues on following page", $this->sidemar + $this->leftcol_width / 2, $bottom_limit - .25 * $this->in, "
                font=$this->FONT_TITILLIUM
                fillcolor=$this->black
                fontsize=" . 14 * $this->pt . "
                position={center center}
                ");

            //Return
            if($break_on_page) {
                return false;
            } else {

                $this->nextPage(true);
                $this->cursor_y = $this->sidemar;
                $this->cursor_x = $this->sidemar + $margin;
                $result = $this->p->fit_table($tbl,$this->cursor_x, $bottom_limit - .5 * $this->in, $this->cursor_x + $this->leftcol_width, $this->cursor_y, "blind=true header=1");

            }
        }

        if($this->leftToPayLine($break_on_page) === false) return false;

        // END LEFT TO PAY

        return true;

    }

Open in new window


Look at line 45-46. I get "$I," but I've never seen anything like what you see on the next line where you apparently have an undeclared variable. There are no errors, so that's obviously not it. But what is it?

Later on in the code, it asks for this:

if (isset($encounter['txns'])) {

How the smoke did that come about?

I can tell you that the initial body of data that this page is processing is a multi-dimensional array and there is a value in there referenced as $results['txns'], so there are some breadcrumbs that I can deduce. But I don't recognize what's happening as far as the sudden presence of "$encounters." What is that?
0
Comment
Question by:brucegust
  • 2
4 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 41729759
It's nothing. It means nothing.  It is a "no-op" line.  It is not a variable declaration.  It's just a programming mistake as far as I can tell.  Without an assignment statement, PHP will not put $encounter into the symbol table.

You can set up the SSCCE for things like this and use var_dump() to see what the variables contain.  Example:
https://iconoun.com/demo/temp_brucegust.php
<?php // demo/temp_brucegust.php
/**
 * https://www.experts-exchange.com/questions/28959632/What-the-smoke-is-encounter.html
 *
 * http://php.net/manual/en/function.var-dump.php
 * http://php.net/manual/en/function.error-reporting.php
 */

// WITH FULL ERROR REPORTING -- ALWAYS RECOMMENDED
error_reporting(E_ALL);
$encounter;
var_dump($encounter);

Open in new window

Outputs:
Notice: Undefined variable: encounter in /home/iconoun/public_html/demo/temp_brucegust.php on line 12
NULL

Open in new window

Key point to notice: No message at all about anything on line 11.

In the original code snippet, it looks like the value in $encounter is assigned by conditional statements around lines 48-52.  So look upstream from there to find the variable that was used to set $encounter['txns'].
0
 
LVL 43

Assisted Solution

by:zephyr_hex (Megan)
zephyr_hex (Megan) earned 200 total points
ID: 41729763
PHP is not a strongly typed language, which means variables aren't tied to a data type.  They can change (albeit changing the data type on a variable is poor coding practice).

So, at line 46, $encounter is just a variable without a value.  We don't know what it is.  A few lines down, it is assigned a value, depending on the result of the comparison $i < $num_unrolled.

Side note:  I just want to point out that this is a bug waiting to happen here:

if($i < $num_unrolled) {
	$encounter = $unrolled_data[$i];
} else {
	$encounter = $rolled_data[$i - $num_unrolled];
}

Open in new window


$rolled_data is accessed at an index that we assume exists, but you should always protect that assumption by verifying the index exists or using an access method that guarantees you aren't trying to access an undefined index.  $num_unrolled is defined on line 7 as:

$num_unrolled = count($unrolled_data);

Open in new window


So we know that, barring the scenario where $num_unrolled is assigned a new value, $unrolled_data[$i] will always exist because $i < $num_unrolled.  We don't have that guarantee with $rolled_data[$i - $num_unrolled]

ok, end side note.

$encounter now has a value.  But we don't know what it is because we don't know what $unrolled_data and $rolled_data are.  We know they are some kind of array... but what if they are arrays of arrays?  If so, then $encounter is being assigned an array.

You can test to see what they are by using var_dump.

Just after lines 4 & 5, var_dump $unrolled_data and $rolled_data.  And after line 52, var_dump $encounter.
1
 
LVL 55

Assisted Solution

by:Julian Hansen
Julian Hansen earned 50 total points
ID: 41729765
Looks like it might have been an attempt to definie $encounter - which as Ray points out - it is not doing.

I am guessing the original programmer created the if statement where $encounter was being set in both if and else parts and then went back with the intention of defining $encounter and then forgot to put in the = bit

OR
There was a declaration for $encounter and after updating the code he removed the assignment because context had changed but did not update it to a new assignment leaving behind a waste of space and processing time.
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 41729769
+1 for @zephyr_hex "Side note."  

This is some of the worst PHP code I have ever seen.  The data design is incompetent, too.  I hope you're getting paid a lot to work on this project.  To me, it feels like scrubbing the sidewalk with a toothbrush, something we do not do, for the obvious reasons.

The right thing here is to (at least) refactor the code.  A more practical approach might be to redesign the application and bring it into this millennium!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question