Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /data/e/7/e76990b9-3372-4d88-8089-fce36e16744a/webperfection.net/sub/rady-nosu/wp-content/plugins/seo-ultimate/modules/class.su-module.php on line 1195
php7 anonymous classes

December 9, 2015 | In: Programovanie

php7 Anonymous Classes

Anonymous Classes

It is exactly what it says, but when can it be useful?

Ever found your self refactoring a pile of shit like the following code?

public function getEmail()
{
    // Now that we have the email we will save it as an object
    // on this class
    $emailObj = new \stdClass();
    $Results = new \stdClass();
    $Results->ID = $emailFromDb->id;
    $Results->HTMLBody = ...;
    $Results->TextBody = ...;
    ... more shit ...
    $emailObj->Results = $Results;

    return $emailObj;
}

// and here we go
public function getTemplateInformation($templateId)
{
    $emailObj = $this->getEmail($templateId);
    
    $data = (array)$emailObj;
    foreach ($data as $property => $value) {
        // do some black magic
    }
    
    $result = [
        "senderAddress" => "test1@test1.com",
        "subject" => '',
        "startDateTime" => '',
        "senderName" => "",
        "label" => isset($this->email->Results->Subject) ? $this->email->Results->Subject : "",
        "isModel" => 1,
    ];

    return (object)$result;
}

and then wherever you look you see casting it back to array?

$data = (array)$email;
foreach ($data as $property => $value) {
...

and then somewhere else

        $emailData = (object)[
            'emailId' => $emailId,
            'folderId' => $folderId,
            'emailWrapper' => $emailParts['container']
        ];

and some more

$result = (array)$result;
foreach ($result as $property => $value) {
...

and some more

    public function getTemplateInformation($templateId)
    {
        return (object)[
            \Service\EditorAbstract::EMAIL_TYPE => null,
            \Service\EditorAbstract::EMAIL_MASTER_ID => $this->templateMapper->getMasterForTemplate($templateId)
        ];
    }

Finding your self doing something like that? Then stop it! Seriously!

You know by now that you want to replace it with some model or real object, but you want to do it step by step and not brake anything.
Now how to do it? The answer is refactoring. I found anonymous classes super useful when replacing crap like that. As first step you can define anonymous class to confirm that everything still works as expected (if you have unit tests, if not – heaven help you!). And start replacing all the crap with your new object:


public function getTemplateInformation($templateId)
{
    $result = [
        "senderAddress" => "test1@test1.com",
        "subject" => '',
        "startDateTime" => '',
        "senderName" => "",
        "isModel" => 1,
    ];

    return new class($result) extends \Model\AbstractModel
    {
        function __construct($result)
        {
            // make the black magic here
        }
    };
}

And what you get is the nice model ready to be tested and you are ready for next steps.

more about php7:

if you need to install PHP7 check out this article

php7 null coalesce operator ??

php7 class return type

Share This:

Comments are closed.