Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "./vendor/phpbench/phpbench/phpbench.schema.json",
"runner.bootstrap": "vendor/autoload.php",
"runner.path": "tests/Benchmark"
}
18 changes: 11 additions & 7 deletions src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -1674,14 +1674,15 @@
*/
public static function renderTagAttributes(array $attributes): string
{
if (count($attributes) > 1) {

Check warning on line 1677 in src/Html.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "GreaterThan": @@ @@ */ public static function renderTagAttributes(array $attributes): string { - if (count($attributes) > 1) { + if (count($attributes) >= 1) { $ordered = []; foreach (self::ATTRIBUTE_ORDER as $name) { if (isset($attributes[$name])) {
$sorted = [];
$ordered = [];
foreach (self::ATTRIBUTE_ORDER as $name) {
if (isset($attributes[$name])) {
$sorted[$name] = $attributes[$name];
$ordered[$name] = $attributes[$name];
unset($attributes[$name]);
}
}
$attributes = array_merge($sorted, $attributes);
$attributes = $ordered + $attributes;
}

$html = '';
Expand All @@ -1698,11 +1699,11 @@
/** @psalm-var array<array-key, scalar[]|string|Stringable|null> $value */
foreach ($value as $n => $v) {
if (!isset($v)) {
continue;

Check warning on line 1702 in src/Html.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "Continue_": @@ @@ /** @psalm-var array<array-key, scalar[]|string|Stringable|null> $value */ foreach ($value as $n => $v) { if (!isset($v)) { - continue; + break; } $fullName = "$name-$n"; if (in_array($fullName, self::ATTRIBUTES_WITH_CONCATENATED_VALUES, true)) {
}
$fullName = "$name-$n";
if (in_array($fullName, self::ATTRIBUTES_WITH_CONCATENATED_VALUES, true)) {
$html .= self::renderAttribute(

Check warning on line 1706 in src/Html.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "Assignment": @@ @@ } $fullName = "$name-$n"; if (in_array($fullName, self::ATTRIBUTES_WITH_CONCATENATED_VALUES, true)) { - $html .= self::renderAttribute( + $html = self::renderAttribute( $fullName, self::encodeAttribute( is_array($v) ? implode(' ', $v) : $v,
$fullName,
self::encodeAttribute(
is_array($v) ? implode(' ', $v) : $v,
Expand Down Expand Up @@ -1925,13 +1926,16 @@
*/
public static function cssStyleFromArray(array $style): ?string
{
$result = '';
if (empty($style)) {
return null;
}

$pairs = [];
foreach ($style as $name => $value) {
$result .= "$name: $value; ";
$pairs[] = "$name: $value;";
}

// Return null if empty to avoid rendering the "style" attribute.
return $result === '' ? null : rtrim($result);
return implode(' ', $pairs);
}

/**
Expand Down
85 changes: 85 additions & 0 deletions tests/Benchmark/HtmlBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Html\Tests\Benchmark;

use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use Yiisoft\Html\Html;

/**
* @BeforeMethods({"setUp"})
*/
class HtmlBench
{
private array $attributes;
private string $content;

public function setUp(): void
{
$this->attributes = [
'id' => 'test-id',
'class' => 'test-class another-class',
'data-test' => 'value',
'aria-label' => 'Test Label',
'style' => 'color: red; background: blue;',
];
$this->content = 'Test content with <special> characters & symbols';
}

/**
* @Revs(1000)
* @Iterations(10)
*/
public function benchDivCreation(): void
{
Html::div($this->content, $this->attributes)->render();
}

/**
* @Revs(1000)
* @Iterations(10)
*/
public function benchFormCreation(): void
{
Html::form('/submit', 'POST', $this->attributes)->render();
}

/**
* @Revs(1000)
* @Iterations(10)
*/
public function benchInputCreation(): void
{
Html::input('text', 'test_field', 'test_value', $this->attributes)->render();
}

/**
* @Revs(1000)
* @Iterations(10)
*/
public function benchAttributeRendering(): void
{
Html::renderTagAttributes($this->attributes);
}

/**
* @Revs(1000)
* @Iterations(10)
*/
public function benchEncoding(): void
{
Html::encode($this->content);
}

/**
* @Revs(1000)
* @Iterations(10)
*/
public function benchAttributeEncoding(): void
{
Html::encodeAttribute($this->content);
}
}
Loading