IMAP email debugging

November 21, 2013

Isn’t it a problem when you have to debug a problem with emails in an existing project? You never know if they are sent and to whom? I just found out that python has an awesome IMAP debugger built in! Just set your project to use these email settings:

IMAP host: localhost
IMAP port: 1025
IMAP user: {leave empty}
IMAP password: {leave empty}

Then you run this in your terminal:

python -m smtpd -n -c DebuggingServer localhost:1025

You’ll now see every mail that’s sent from your application displayed in your terminal!

That’s it… Have fun debugging

Advertisements

PSR coding standards

October 26, 2013

I think cross-project, language wide coding standards like PSR are great. It makes reading or writing code from other projects easy. And since PSR is so widely adopted in the PHP programming scene, it’s a really good coding standard to adopt. When studying it, though, I came across two things that I really dislike in their standard. They both apply to PSR-2.

The eternal Spaces vs. Tabs debate

Indents are a really personal thing. Some people find 4 spaces a good indentation width, others prefer 2 spaces, even others prefer 8. That’s why tabs are so awesome, every good editor allows you to set your own tab width, so that your indentation is perfect for you in your editor, and perfect for somebody else in his or her editor of choice. Now, i’m really talking about indentation, not alignment. Allignment SHOULD be done using spaces. Things will still align, even if the tab with is changed. This is perfect in every case.

An example. . are spaces, and thus fixed width spacing. - stands for one space width inside a tab in a certain editor.

One tab is 2 spaces wide

/**
.*.This.is.an.example.class
.*/
class.ClassName {
--/**
--.*.The.fooBar.method,.takes.two.arguments
--.*
--.*.@param.string..............$argument1.......The.first.argument
--.*.@param.string[optional]....$argument2.......The.second.argument
--.*/
--public.function.fooBar($argument1,.$argument2.=.null).{
----if.($argument1.===.$argument2).{
------//.Return.Foo
----}
----else.{
------//.Return.Bar
----}
--}
}

One tab is 4 spaces wide

/**
.*.This.is.an.example.class
.*/
class.ClassName.{
----/**
----.*.The.fooBar.method,.takes.two.arguments
----.*
----.*.@param.string..............$argument1.......The.first.argument
----.*.@param.string[optional]....$argument2.......The.second.argument
----.*/
----public.function.fooBar($argument1,.$argument2.=.null).{
--------if.($argument1.===.$argument2).{
------------//.Return.Foo
--------}
--------else.{
------------//.Return.Bar
--------}
----}
}

As you can see in these two examples, things keep aligning perfectly, everyone gets to use their own prefered indentation width, *and* your git history is as clean as using only spaces. When used consistently this method has all the upsides of the PSR-2 “only spaces” rule, and none of the downsides.

Curly brackets

PSR is not really consistent in this case. Control structures like if, elseif, switch, for and while must have their opening brackets on the same line, while functions, methods and classes must have their opening brackets on the next line.
I think it would be more consistent if control structures, functions, methods and classes would all have the same notation with brackets on the same line, like in this example:

<?php
namespace Vendor\Package;

class ClassName {
    public function fooBar($argument1, $argument2 = null) {
        if ($argument1 === $argument2) {
            // Return Foo
        }
        else {
            // Return Bar
        }
    }
}

Also notice in the above example that every closing bracket is on its own line, unlike the PSR-2 standard way of putting the if closing bracket on the same line as the else statement, like this:

if ($argument1 === $argument2) {
    // Return Foo
} else {
    // Return Bar
}

I think this is inconsistent, and doesn’t help for readability.

That’s all…

Ubuntu Quick Work Setup

January 4, 2013

Today i installed a dual boot Ubuntu setup on my computer at work. I used the Wubi installer with Ubuntu 12.04 LTS. Here’s what i did to get started pretty quick:

1. Update and upgrade:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

2. Install some software

  • sudo apt-get install git tig chromium-browser apache2 php5 php5-mysql php5-sqlite php5-gd php5-curl php5-xdebug php5-memcached php5-imagick php5-intl phpunit mysql-server vim ffmpeg curl inkscape vlc tree
  • I manually installed Sublime Text 2, a launcher, and some add-ons for it

3. Configure some software

  • Firefox and chrome: tabs, startpage, sync
  • Thunderbird + Exchange setup: my previous post
  • git: global settings (user, email, colors)
  • apache:
    • sudo a2enmod rewrite to enable mod_rewrite
    • sudo a2enmod vhost_alias to enable VirtualHosts
    • Added VirtualDocumentRoot phrase to /etc/apache2/sites-available/default
  • I also set up dropbox to brainlessly sync between my work’s windows and ubuntu setups

That’s about it. Took me less time than i thought it would.

Regards
Toon

I always wonder what would be the perfect development setup for me. Graphical? Command line? IDE? Text editor? VCS? Automation? Now it’s time to sum some things up for myself. (To make sure we speak the same language: i write php, javascript, html, css, and sh. Sometimes a little bit of python.)

I hate IDE’s. They are so bloathed with stuff you don’t need, 99% of the time. But they can be pretty useful… certainly when you have to work with big projects (or other people’s projects), it can help to have a good IDE at hand. Most of the time though, they’re in my way, they create stupid project files, they slow down my laptop and they cost money if you want a decent one. So that’s why i usually let NetBeans sleep, and wake Sublime Text or Vim instead.

Now to compensate for a few things text editors miss, compared to IDE’s. For version control i’m using Git. It’s fantastic. I use vagrant shared folders to have my code right on the development environment while developing.

For debugging, i usually use the php-cli tool in the terminal or Xdebug in the browser. To cope with a lot of ouput, i’ve colorized my prompt line, so that i can find it easily between two var_dumps.

Almost all the above relies on the power of the command line, and applies to both my Trisquel GNU/Linux and Mac OSX setups. I quite like it. On the contrary, everything above is unavailable at the place where i work. No git, no handy command line debugging (cygwin fails to install), no rsync… only a gedit install without plugins. Read more about that here.

That’s about it! 🙂
Toon

Lately, i was writing a class in php, and i wanted to test it from the command line. In the same file i wrote the class in, i put this:

$test = new Test();

that’s the default way to do this in php. What’s not so interesting about this, is that when you want to use the class in another project and you include the file, the class will automatically be called, because that piece of code is in there. So you’ll have to edit your file again.

That’s not what i wanted.

What i want is to make the class file detect if it’s being included/required or not, and act in a correct manner: if the file is not included by another file the class should be called automatically, when it is included though, it should not, so that the project that includes the class can do whatever they want with it. And i want all of this without ever having to change the code in the class file.

The following code does just that:

//initiate the class if we're not included by another php file
if (basename(__FILE__) == basename($_SERVER['PHP_SELF'])) {
$test = new Test();
//you can put other code here, e.g:
$test->monkey();
}

it compares the basename of the __FILE__ constant, with the basename of the $_SERVER[‘PHP_SELF’] environment variable. __FILE__ always returns the name of the real file in which it is called, while PHP_SELF returns the path of the file that was directly called by the user (so if it’s called in a file that’s being included, it returns the path of the parent file). If both are the same, the file is not included or required by another file, so the class should be called.

That’s all 🙂

I recently started working as php developer for a small/medium-sized media company. Everything is really fine there, except for one thing. They give their developers computers with Windows installed on them. I’ll try and document here what programs i installed to survive this hell. You’ll notice i like to keep things simple (and i really really like to use Free Software).

– First thing I did was to completely update the Operating System. That way i don’t have to worry about outdated browsers, uncompatible dll’s etcetera. It’s Windows Vista, so i just did Windows Update and that was it.

– Next step: a decent browser. I installed Firefox 4 because i really enjoy using it on Mac OSX and GNU/Linux. I usually use a lot of browser add-ons. More about that later. When Firefox was installed, i naturally made it my standard browser, and i removed all desktop shortcuts and such for Explorer.

– You can’t Code without an Editor. Notepad is not an option. So i went for Gedit, from the GNOME project. It’s a really neat and simple editor, it has code-highlighting, and a few other handy options for programming. Also i activated the gedit-plugin ‘File Browser Pane’, to have a filebrowser inside of the editor, like many IDE’s have. On Linux i’d also activate the Terminal pane, but that’s not possible on the Windows version of Gedit. I also installed my own Gedit theme, which is a tweaked version of the default Oblivion theme.

– next up is an FTP program. At the company they use SFTP so i was looking for a decent FTP program. Where many people would’ve chosen FileZilla, i choose FireFTP. It’s a Firefox browser add-on, and works great. It has all features i need. And it runs in my browser, so the integration with it is fantastic (upload->view on the web == WIN!)

– So far, so good. Communication in the office happens by e-mail, for which they have a Microsoft Exchange server. I downloaded Thunderbird, and set it up to work with it. Great.

– I needed an SSH client, so i installed FireSSH, another great Firefox browser add-on which provides everything i need from SSH.

– For Quick Scripting: the GreaseMonkey browser add-on for Firefox.

– And lastly (too bad) i needed a PDF reader and Flash Player and Quicktime, and that’s why i installed Adobe Reader X, Adobe Flash Player and Quicktime.

– time to get rid of the ugly Windows Vista desktop background.

I’ll survive like this, but i’d so much rather have a Mac or a GNU/Linux box to work on 🙂

regards
Toon

ASCII Stars

February 26, 2010

A few days ago i created a nice ASCII art php script. I was taking a break from work and i was looking around on the internet for nice ascii art. I saw a wegpage where someone let a script create stars of a certain magnitude. I made a command line script that does the same thing for fun.

for input 1, it gives you this:

asciistars1

for input 2, it gives you this:

etc… it’s quite fun 🙂

you find the script here

to run it, remove the .txt from the end of the filename, open a terminal, and then type this:

php Desktop/stars.php 3

ofcourse, change ‘Desktop’ to the dir where you put it. instead of the 3 type any number below 10

you will need php-cli installed to run this. (installed on Mac OSX by default)