for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Helick\Blocks\Traits;
use Exception;
use Helick\Blocks\Exceptions\BlockException;
trait Renderable
{
/**
* Captures the output that is generated when a template is included.
* This property is static to prevent object scope resolution.
*
* @param string $template
* @param array $data
* @return string
* @throws Exception
*/
protected static function capture(string $template, array $data): string
extract($data, EXTR_SKIP);
ob_start();
try {
include $template;
} catch (Exception $e) {
ob_end_clean();
throw $e;
}
return ob_get_clean();
* Render the block.
* [!!] Global variables with the same key name as local variables will be
* overwritten by the local variable.
* @param array $fields
* @param array $attributes
* @param array $blocks
* @return void
public function render(array $fields, array $attributes, array $blocks): void
$globalVariables = compact('fields', 'attributes', 'blocks');
$localVariables = $this->with($fields);
with()
If this is a false-positive, you can also ignore this issue in your code via the ignore-call annotation
ignore-call
/** @scrutinizer ignore-call */
$data = array_merge($globalVariables, $localVariables);
echo static::capture($this->template(), $data);
* Get the block's template.
protected function template(): string
if (empty($this->template)) {
throw BlockException::forEmptyTemplate();
$template = locate_template($this->template);
locate_template
$template = /** @scrutinizer ignore-call */ locate_template($this->template);
if ($template === '') {
throw BlockException::forNotFoundTemplate($this->template);
return $template;