Insights after Tasks

Insights after Tasks

I’ve now completed my first four tasks, during which I had the chance to look and work with the wonderland code. I saw a good amount of things that were or at least looked familiar while some things were completely new. Though even there with just looking at and reading the code I was able to build a general idea of what the code does or might do, which is well how it should be.

The first thing you need to do, is figure out weather the code is in the wonderland or myhosttech codebase because that’s dependent on where you create your issue and merge request.

So far it was only easy tasks, just deleting code still leftover or a unneeded part, for example a Handler and it’s dependencies. Now the latest task was a tad bit more complex, since it wasn’t quite to just remove code, well mostly at least. Since I removed part code that was part of switch cases which only had two cases, one I now deleted which makes the switch case unnecessary. So I “had” to or well for the sake of keeping the code as small and non redundant as possible rewrite the code. In this case it went something like this:

original code:

PHP
public function getNotHosttechData($key) // code censored on purpose
{
    $transporterData = [ 
        'phone' => '000 000 00 00', 
        'website' => 'www.example.com'
    ];
    $pcvertriebData = [
        'phone' => '000 000 00 00',
        'website' => 'www.example.com'
    ];
    switch ($this->invoice->layout) {
        case Invoices::LAYOUT_123TRANSPORTER:
            return $transporterData[$key];
            break;
        case Invoices::LAYOUT_HARDWARE:
            return $pcvertriebData[$key];
            break;
    }
}

Then my first fix / rewrite:

PHP
public function getNotHosttechData($key) // code censored on purpose
{
    $transporterData = [
        'phone' => '000 000 00 00',
        'website' => 'www.example.com'
    ];
  
    if ($this->invoice->layout === Invoices::LAYOUT_123TRANSPORTER) {
      return $transporterData[$key];
    }
}

But as it turns out even the if statement was not needed so in the end in just looked like this:

PHP
public function getNotHosttechData($key) // code censored on purpose, makes function and gives $key variable
{
    $transporterData = [ // associative array
        'phone' => '000 000 00 00', // key with assigned value
        'website' => 'www.example.com' // key with assigned value
    ];
  
    return $transporterData[$key]; // returns either value depending on which key is called
}

So as you can see I was able to shorten the code by quite a bit. Now while yes one could’ve done this:

PHP
public function getNotHosttechData($key) // code censored on purpose
{
    $transporterData = [
        'phone' => '000 000 00 00',
        'website' => 'www.example.com'
    ];
    
    switch ($this->invoice->layout) {
        case Invoices::LAYOUT_123TRANSPORTER:
            return $transporterData[$key];
            break;
    }
}

Just quickly look at both of them and one can see that the first option looks much better. A lot less unnecessary code laying around.

Now since I made adjustments to the code I need to test it to see if it still works. In this case I firstly had to find out how to trigger this part of the code, in this case it was part of a Pdf generation so I needed to trigger this Pdf getting generated. Now for that I created a new “Einzelrechnung” then set a position and then created the invoice with the 123 Transporter-Layout since that’s what the requirement was to trigger that specific Pdf creation. After that I downloaded the Pdf and checked if it’s correct.

Understand the code

An important thing to note is that before you delete or especially before you add code that you understand what the code does. Since then you can check if what you’re deleting is actually correct or if there’s maybe another part under a different name or so which also needs / should be deleted. Or if then you add code, how you need to implement the code. Not only the code you want to chance also around the code, what’s above and below and to the sides (other files that are bound via functions, constants, etc.).

Testing

Also after you make a chance in the code make sure to test it. In the Laravel part(myhosttech) you write a test and in the Zend part(wonderland) you test it hands on with the test website usually. So for a test you go to “myhosttech.test” and figure out where this gets done and then make it happen.

Here some useful tools to help locate code instances or parts:

  • “Ctrl + F” – Search the current file for whichever you enter.
  • “Ctrl + Shift + F” – Same as “Ctrl + F” but inside the whole project not just the current file.
  • “Double Shift” (press Shift twice in quick succession) – Search filenames with what you enter.
  • “Ctrl + Click” – Click on a function, class, etc. you will see each instance where that function / class / etc. is called.
    Just try it if it work with what you tried you will notice (for example, mouse will turn from cursor to pointer).

Review Issues mit Manu

MR / Issue Review
Remove old DnsHandler.php and it’s Dependencies
-Gute Issue / MR Beschreibung, vielleicht noch den Pfad zum DNS-Handler selbst ins issue schreiben
-Hätte ich nicht anders gemacht (2 Files gelöscht)

Remove DNS Tool button
-Sehr gute Issue Beschreibung mit Image
-Sonst gut, alles nötige entfernt

Remove PC Vertrieb code
-Issue Beschreibung gut, vielleicht noch reinschreiben, was PC vertrieb ist und macht?
-InvoicePdf.php:520/521 braucht es nur einen Check
-InvoicePdf und StornoPdf: getNotHosttechData($key) ist jetzt nur noch für transporter zuständing könnte man Inline machen.
-application/pdfs/images/pc_vertrieb.svg und application/pdfs/images/logo-pcvertrieb.png könnte man noch löschen
-Sonst gut

Some of these things are parts that I’ve not yet realized that those are important or matter much, so I have taken those points to mind and look for that I will not repeat those mistakes. While other things are things I know but overlooked or purposely ignored since based on things that I’ve observed. For example not deleting the images belonging to pc-vertrieb, those I didn’t delete on purpose since I knew that there are a good amount of old images which are not in use anymore floating around. But this I now know should nonetheless be deleted even if others didn’t.

Now the reason for writing the “StornoPdf:getNotHosttechData($key)” inline is to shorten the code reducing redundance making it faster and might even make it easier to understand. This for example is something I overlooked but I understand on how or well more so why you could and should do that.

5th Task

Now I’ve just completed my fifth task. This issue or well task was a interesting one since this was the first one where I added new code to the codebase. Now the task started out simple, “adjust the VNC password change text”, this was easily done it was inside a string-template where I just had to change the number in the text from 12 to 8. Now after that I went into the test wonderland to see if my changes are working as intended. Now I had a bit of a struggle to figure out how to get there exactly since it needed to be a vServer with certain conditions for the VNC to be active and I couldn’t just create one via the admin page since I didn’t have a option für the OS for example which meant I wasn’t able to create one. Now to fix that I had to manually add an option into the database, that I was able to do with the help of Manu.

Even after I was able to create the needed product the loading of the pages was very and I mean very inconsistent, because sometimes it showed a critical error or just never stopped loading then at times I got a timeout then a zend_session error and each of these option had a chance to appear each page I needed to load which it was like 6 or so before I reached my “destination”, meaning it took quite a moment until I was able to test it.
But after a while I was able to test it but during that I noticed something, that is that the validation is wrong and set for 12 characters still. So then I asked Simon if that is something that I should change as well to which the answer was yes.

So now I looked at the code inside the password-validator to see how one could fix it. Then firstly I just changes the text of the existing code to see if that is truly where the text came from which was the case. So now I had to think of a way to add a check or condition that if it’s loading the VNC password change popup then it needs to show and check for 8 instead of 12 characters. Now the easiest way I could think of was with an if statement, which I then went to try and implement.
First I thought of using DOM selectors to find an id or class to see if it’s loading the VNC password change popup. Now that was a step into the right direction I was just looking at the wrong files and therefor classes / id’s to check for. I tried 2 different variations one with an id the other with a class. Neither worked, so I went to look through the code again to see what else it could be which is when I stumbled over a certain code section which used an if statement with a check if it’s part of the VNC password form. Now when I saw that, I thought since it’s here it should or must work so why not give this a try and so I did and it worked. Here’s what it looks like:

PHP
// before
if (sPW.length < 12) { // checks is the password length is less than 12
    iPercent -= 10 * (12 - sPW.length) // takes away 10% of the bar for each missing letter
    bTooShort = true // sets the boolean from false to true (that the password is too short)
    if (sMessage === '') {// if the message is empty 
        sMessage = 'Mindestlänge 12 Zeichen' // sets the value of "sMessage" to "Mindestlänge 12 Zeichen"
    }
}
// after
if ($('.validatePW').closest('form').attr('id') == 'vnc-passwform') { // searched all elements with class "validatePW" then selects nearest parent "<form>" or if there's multiple the first then select the id and compares it to the given string
    if (sPW.length < 8) { // checks is the password length is less than 8
        iPercent -= 10 * (8 - sPW.length) // takes away 10% of the bar for each missing letter
        bTooShort = true // the same as above, same with the rest
        if (sMessage === '') {
            sMessage = 'Mindestlänge 8 Zeichen' // sets the value of "sMessage" to "Mindestlänge 8 Zeichen"
        }
    }
} else {
    if (sPW.length < 12) {
        iPercent -= 10 * (12 - sPW.length)
        bTooShort = true
        if (sMessage === '') {
            sMessage = 'Mindestlänge 12 Zeichen'
        }
    }
}

As mentioned above the testing took most of the time needed for this task / issue which was rather annoying since it was so inconsistent but after a good few nerves and a few hours I was able to do the final test and finish this task / issue as well.

Sequel I

I have now completed a good few tasks, the whole process got easier to a point where it goes automatically besides a label i forget to adjust in the issue after i did so in the merge request but asides that it’s pretty stuck in the meantime.

Now during these tasks i noticed something. I have a tendency to overlook a tiny detail or do something and then do another thing which contradicts what i just did. Here are a few examples:

Example 1:
Using a variable which in the check above (if its true) won’t exist and then use that inside the check.

PHP
if (is_null($cancellation)) {
    \Sentry\captureMessage('This cancellation: '. $cancellation .', does not exist anymore.');
    return $result;
}

As you can see I made an if statement which will trigger if the variable “$cancellation” is null. But right after i try to give that exact variable in an error message. This will of course not work because how can it output something which will be null (so nothing).
Here’s another Instance of this case:

PHP
Folgende Produkte werden zu den angegebenen Daten gelscht:
<?php if(count($this->products) > 0){ ?>
<?php foreach($this->products as $data){?>
<?php $product = $data["product"]; $date = $data["date"];?>
- Produkt: <?php echo $product->getFullDescription(); ?> zum <?php echo DateHelper::formatDate($date); ?> 
<?php if (is_null($product)) { ?>
Es gibt kein Produkt zu dieser Kndigung.
<?php    \Sentry\captureMessage('Product: '. $product .', does not exist. For this cancellation: '. $this->cancellations->id .', There exists no product. '); ?>
<?php } else { ?>

...

Here once more i added an if statement that triggers if the “$product” is null and the proceed to use it one line further down in an error message.

Example 2:
Set the bracket in the wrong line.

PHP
...// greetings

<?php if(count($this->products) > 0){ ?> // this is the opening bracket
<?php foreach($this->products as $data){?>
<?php $product = $data["product"]; $date = $data["date"];?>
- Produkt: <?php echo $product->getFullDescription(); ?> zum <?php echo DateHelper::formatDate($date); ?> 
<?php if (is_null($product)) { ?>
Es gibt kein Produkt zu dieser Kndigung.
<?php    \Sentry\captureMessage('No product exists for this cancellation:'. $this->cancellations->id); ?>
<?php } else { ?>

- Produkt: <?php echo $product->getFullDescription(); ?> zum <?php echo DateHelper::formatDate($date); ?>
<?php }?>
<?php }?>
<?php if(count($this->domains) > 0){?>
<?php foreach($this->domains as $data){?>
<?php $domain = $data["product"]; $date = $data["date"];?>
- <?php echo $domain->getFullDescription(); ?> zum <?php echo DateHelper::formatDate($date); ?> 
<?php }?>

...

<?php if(...) {?>...<?php } else {?>...<?php }?> ...

<?php foreach($this->domains as $data){?>
<?php $domain = $data["product"]; $date = $data["date"];?>
- <?php echo $domain->getFullDescription(); ?>: <?php echo $domain->authCode;?> 
<?php }?>
<?php }?>

... // regards

<?php } ?> // this is the closing bracket

Here i put the closing bracket of the if function at the start at the bottom of everything. Now i didn’t notice that nor did i think that anything was wrong because when i tested the mail (which this is) everything looked right. But while this would’ve fixed the issue i was given it would have produced at least one new issue which is of course not the idea.
Now when you look closer you can see that there are two main parts to this mail: Products and Domains now in this case if there were no products the domains if there are any wouldn’t have been shown because of where the closing bracket is. So i moved it up just below where the product part of the mail is done, like so:

PHP
... // greetings

<?php if(count($this->products) > 0){ ?> // this is the opening bracket
<?php foreach($this->products as $data){?>
<?php $product = $data["product"]; $date = $data["date"];?>
- Produkt: <?php echo $product->getFullDescription(); ?> zum <?php echo DateHelper::formatDate($date); ?> 
<?php if (is_null($product)) { ?>
Es gibt kein Produkt zu dieser Kndigung.
<?php    \Sentry\captureMessage('No product exists for this cancellation:'. $this->cancellations->id); ?>
<?php } else { ?>

- Produkt: <?php echo $product->getFullDescription(); ?> zum <?php echo DateHelper::formatDate($date); ?>
<?php }?>
<?php }?>
<?php } ?> // this is the closing bracket

<?php if(count($this->domains) > 0){?>
<?php foreach($this->domains as $data){?>
<?php $domain = $data["product"]; $date = $data["date"];?>
- <?php echo $domain->getFullDescription(); ?> zum <?php echo DateHelper::formatDate($date); ?> 
<?php }?>

...

<?php if(...) {?>...<?php } else {?>...<?php }?> ...

<?php foreach($this->domains as $data){?>
<?php $domain = $data["product"]; $date = $data["date"];?>
- <?php echo $domain->getFullDescription(); ?>: <?php echo $domain->authCode;?> 
<?php }?>
<?php }?>

... // regards

Example 3:
Forgot to remove a variable from a Handler.

PHP
protected $_oSambaHandler;

I had to remove a Handler and i did so but in another file there was this little variable hiding. Now i could swear that this variable didn’t appear when i searched for “SambaHandler” with “Ctrl + Shift + F” (search in every file project wide). But there it was now one hint on which i could’ve / should’ve picked up on is that in the funtion where this variable is used it was combined with “$this” meaning that the variable was in this file. Here’s how that looked like:

PHP
public function getSambaHandler()
    {
        if(empty($this->_oSambaHandler)) // here with this "$this->_oSambaHandler"
        {
            $oSSHHandler = $this->getSSHHandler();
            $this->_oSambaHandler = new \...\Samba\Handler($oSSHHandler); // and / or here
        }

        return $this->_oSambaHandler;
    }

So what is to learn from this is to always thoroughly read the code and double check before committing the changes. Something that also might help to avoid such mistakes it to take a quick break or do something else for the mind to focus on because this way instead of just reading it over and over again back to back can make it so you overlook the same part each time. But with the short mental break or divergence of a task it can help refresh the mind and spot things you might’ve overlooked before.

Something else to watch out for is that you make a call to the db (database) the least amount of times possible. Because this is generally what makes the website slow to load. Instead use the models or a variable in which the data from the database is already stored in. Here’s an example:

Instead of this:

PHP
<h3>Userdata</h3>

  <?php $user = $this->order->getUser() ?: $this->order->getUserdata(); ?>
  
	<table>
  <?php if ($this->order->getUser()) : ?>
		  <tr>
			  <td width=263>Kunden-Nummer:</td>
		    <td style="font-family: 'Americane-Bold';"><?php echo $user->id; ?></td>
    </tr>
<?php endif; ?>

Do this:

PHP
<h3>Userdata</h3>
	
  <?php $user = $this->order->getUser() ?: $this->order->getUserdata(); ?>
					
	<table>
  <?php if ($user instanceof User) : ?>
			<tr>
					<td width=263>Kunden-Nummer:</td>
			    <td style="font-family: 'Americane-Bold';"><?php echo $user->id; ?></td>
	    </tr>
  <?php endif; ?>

This checks if the data from the variable “$user” is a instance of the “User Model”. This does the same but you don’t make a call to the database for a second time.

Tags: