Perks of Peachpie and Roslyn: Powerful Code Snippets

We mixed in something a little different this week. If you go back to our older blog posts, you may notice that something has changed.

How Peachpie sees the code

Microsoft really did an outstanding job with its compiler architecture on which Peachpie is built. We blogged about performance benefits and advantages that are specific to Peachpie before, but the compiler exceeds just these particular usecases. Our compilation itself generates a plethora of data that we use to generate an efficient form of code. In order to see what is happening under the cover inside the compiler, we prepared a colorful visualization of the code – aka how it is seen by the compiler.

Instead of this:

<?php
class Dog{
    /**
     * @var string
     */
    var $name;
}
class Test{
    /**
     * @param object $x
     */ 
    static function Foo($x)
    {
        for ($i = 0; $i < 100000000; $i++) { $x->name = "Woofie";
        }
    }
}

We now have this:

Code Analysis Explained

The compiler performs several compilation steps as described on Roslyn Wiki. Peachpie, however, goes far beyond that to see through the dynamic PHP code. The analysis itself is a type of magic, based on proprietary know-how, heuristics and possible scenarios that may occur in runtime.

The snippets help us and all developers that decided to build their apps on the Peachpie platform to see how their code is viewed from the compiler’s perspective.

Peachpie snippets quickly reveal possible issues as well as the implied type information and reflect all the diagnostics produced by the compiler.

As a little disclaimer, there are cases that may seem a bit odd, but it can all happen in the dynamically interpreted environment of PHP and the compiler must take this into account.

Powerful Peachpie snippets

The difference is not only that the code is dark; there are several additional features and benefits of these snippets that can be of a significant value. Just hover over variable $x in line 15 in the code above to see what we mean.

There are several features of these new Peachpie snippets.

Error underlining

Just like in your favorite IDE, our snippets underline various issues with different colors according to their severity. In Roslyn terminology it is called diagnostics and we do visualize them in the first place. Errors have a red underlining:

Tooltips

Upon hovering over a variable, class, interface, constant, function parameter, magic constant or function, the snippet will show tooltips with quite a bit of information. Try hovering over the print_r() function in line 10 for example:

The information is taken from assembly metadata, source code, PHPDoc and the corresponding XML documentation.

Warnings/Recommendations

Green underlining is not necessarily a warning, but can also be our recommendation. This kind of diagnostic mostly signalizes that the compiler is aware of something, which will emit a less efficient form of code. In some cases this can be an issue, in others the code can be written exactly like this on purpose:

As you can see, the class `User` is underlined, because it cannot be found in this short code snippet. The snippet does not care about the inclusions, it sees only this piece of code.

And there you go – a practical demonstration of what is possible with Peachpie thanks to Roslyn. We will now continue to add further functionality to the compiler in order to bring it closer to full compatibility with standard PHP. We are also working on getting the new msbuild system to function properly so that you can use Peachpie in VS17. We have some more highly interesting articles planned in the coming weeks, so make sure you follow our progress on GitHub, Twitter or Facebook. If you have any questions, join our community chat on Gitter.

Posted on March 27, 2017, in category Announcement, Information, Snippets, tags: , ,