Open Menu

Creating Custom Result Renderers#^ TOP

If you don't know what result renderers are, go ahead and read this article first. We will be continuing on from the previous article, let's go ahead and create the renderer.

1. Create the folder and class

mkdir src/ResultRenderer

touch src/ResultRenderer/CodingStandardFailureRenderer.php

2. Write the renderer class

<?php

namespace PhpSchool\SimpleMath\ResultRenderer;

use PhpSchool\PhpWorkshop\ResultRenderer\ResultRendererInterface;
use PhpSchool\PhpWorkshop\ResultRenderer\ResultsRenderer;
use PhpSchool\SimpleMath\Result\CodingStandardFailure;

class CodingStandardFailureRenderer implements ResultRendererInterface
{
    /**
     * @var CodingStandardFailure
     */
    private $failure;

    /**
     * @param CodingStandardFailure $failure
     */
    public function __construct(CodingStandardFailure $failure)
    {
        $this->failure = $failure;
    }

    /**
     * @param ResultsRenderer $renderer
     * @return string
     */
    public function render(ResultsRenderer $renderer)
    {
        $header = sprintf(
            'Coding Standard violations were found using the standard: "%s"',
            $this->failure->getCodingStandard()
        );
        $output = [
            sprintf('  %s', $renderer->style($header, ['bold', 'underline', 'yellow'])),
        ];

        foreach ($this->failure->getErrors() as $error) {
            $output[] = '   * ' . $renderer->style($error, 'red');
        }
        $output[] = '';

        return implode("\n", $output);
    }
}

This is really simple: the render(ResultsRenderer $renderer) just returns a string representation of the result, we style the results a little in a bullet pointed list, highlighting them red. We also add a title which describes the coding standard used.

That's basically it - we just need to register the renderer with the application.

3. Register the renderer

Now you need to tell the application about your new result. Open up app/bootstrap.php and after the application object is created you just call addResult with the result class name and the result renderer class name. Your final app/bootstrap.php file should look something like:

<?php

ini_set('display_errors', 1);
date_default_timezone_set('Europe/London');
switch (true) {
    case (file_exists(__DIR__ . '/../vendor/autoload.php')):
        // Installed standalone
        require __DIR__ . '/../vendor/autoload.php';
        break;
    case (file_exists(__DIR__ . '/../../../autoload.php')):
        // Installed as a Composer dependency
        require __DIR__ . '/../../../autoload.php';
        break;
    case (file_exists('vendor/autoload.php')):
        // As a Composer dependency, relative to CWD
        require 'vendor/autoload.php';
        break;
    default:
        throw new RuntimeException('Unable to locate Composer autoloader; please run "composer install".');
}

use PhpSchool\PhpWorkshop\Application;
use PhpSchool\SimpleMath\Check\Psr2Check;
use PhpSchool\SimpleMath\Exercise\Mean;
use PhpSchool\SimpleMath\Result\CodingStandardFailure;
use PhpSchool\SimpleMath\ResultRenderer\CodingStandardFailureRenderer;

$app = new Application('Simple Math', __DIR__ . '/config.php');

$app->addExercise(Mean::class);
$app->addCheck(Psr2Check::class);
$app->addResult(CodingStandardFailure::class, CodingStandardFailureRenderer::class);

$art = <<<ART
  ∞ ÷ ∑ ×

 PHP SCHOOL
SIMPLE MATH
ART;

$app->setLogo($art);
$app->setFgColour('red');
$app->setBgColour('black');

return $app;

Try it out!#^ TOP

Run the workshop and select the Mean Average exercise. Verifying a solution which does not pass the PSR2 coding standard will yield the following output:

You can see the finished, working code on the custom-result branch of the tutorial repository.