Skip to content

Commit

Permalink
Merge pull request #3 from keepsuit/refactoring
Browse files Browse the repository at this point in the history
Reduced Template state
  • Loading branch information
cappuc authored Sep 1, 2023
2 parents 2af6ab5 + 89565f9 commit 437f138
Show file tree
Hide file tree
Showing 57 changed files with 523 additions and 315 deletions.
4 changes: 4 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ errors:
include: "Argument error in tag 'include' - Illegal template name"
disabled:
tag: "%{tag} usage is not allowed in this context"
stack:
nesting_too_deep: "Nesting too deep"
runtime:
partial_not_loaded: "The partial '%{partial}' has not be loaded during parsing"
5 changes: 3 additions & 2 deletions performance/Shopify/CommentFormTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Keepsuit\Liquid\Performance\Shopify;

use Keepsuit\Liquid\Exceptions\SyntaxException;
use Keepsuit\Liquid\Parse\ParseContext;
use Keepsuit\Liquid\Parse\Regex;
use Keepsuit\Liquid\Parse\Tokenizer;
use Keepsuit\Liquid\Render\Context;
Expand All @@ -21,9 +22,9 @@ public static function tagName(): string
return 'form';
}

public function parse(Tokenizer $tokenizer): static
public function parse(ParseContext $parseContext, Tokenizer $tokenizer): static
{
parent::parse($tokenizer);
parent::parse($parseContext, $tokenizer);

if (preg_match(self::Syntax, $this->markup, $matches)) {
$this->variableName = $matches[1];
Expand Down
13 changes: 9 additions & 4 deletions performance/Shopify/CustomFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,20 @@ public function linkToTag(string|int|float $label, string $tag): string
public function highlightActiveTag(string|int|float $tag, string $cssClass = 'active'): string
{
$currentTags = $this->context->get('current_tags') ?? [];
assert(is_array($currentTags));

if (in_array($tag, $currentTags)) {
return sprintf('<span class="%s">%s</span>', $cssClass, $tag);
}

return $tag;
return (string) $tag;
}

public function linkToAddTag(string|int|float $label, string $tag): string
{
$tags = array_unique([...($this->context->get('current_tags') ?? []), $tag]);
$currentTags = $this->context->get('current_tags') ?? [];
assert(is_array($currentTags));
$tags = array_unique([...$currentTags, $tag]);

return sprintf(
'<a title="Show tag %s" href="/collections/%s/%s">%s</a>',
Expand All @@ -87,7 +90,9 @@ public function linkToAddTag(string|int|float $label, string $tag): string

public function linkToRemoveTag(string|int|float $label, string $tag): string
{
$tags = array_filter($this->context->get('current_tags') ?? [], fn ($t) => $t !== $tag);
$currentTags = $this->context->get('current_tags') ?? [];
assert(is_array($currentTags));
$tags = array_filter($currentTags, fn ($t) => $t !== $tag);

return sprintf(
'<a title="Show tag %s" href="/collections/%s/%s">%s</a>',
Expand Down Expand Up @@ -210,7 +215,7 @@ protected function toHandle(string $input): string
$result = $input;
$result = strtolower($result);
$result = str_replace(['\'', '"', '()', '[]'], '', $result);
$result = preg_replace('/\W+/', '-', $result);
$result = preg_replace('/\W+/', '-', $result) ?? '';

return trim($result, '-');
}
Expand Down
7 changes: 4 additions & 3 deletions performance/Shopify/PaginateTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Keepsuit\Liquid\Exceptions\InvalidArgumentException;
use Keepsuit\Liquid\Exceptions\SyntaxException;
use Keepsuit\Liquid\Nodes\Range;
use Keepsuit\Liquid\Parse\ParseContext;
use Keepsuit\Liquid\Parse\Regex;
use Keepsuit\Liquid\Parse\Tokenizer;
use Keepsuit\Liquid\Render\Context;
Expand All @@ -25,9 +26,9 @@ public static function tagName(): string
return 'paginate';
}

public function parse(Tokenizer $tokenizer): static
public function parse(ParseContext $parseContext, Tokenizer $tokenizer): static
{
parent::parse($tokenizer);
parent::parse($parseContext, $tokenizer);

if (preg_match(self::Syntax, $this->markup, $matches)) {
$this->collectionName = $matches[1];
Expand All @@ -39,7 +40,7 @@ public function parse(Tokenizer $tokenizer): static
$this->attributes = ['window_size' => 3];
preg_match_all(sprintf('/%s/', Regex::TagAttributes), $this->markup, $attributeMatches, PREG_SET_ORDER);
foreach ($attributeMatches as $matches) {
$this->attributes[$matches[1]] = $this->parseExpression($matches[2]);
$this->attributes[$matches[1]] = $this->parseExpression($parseContext, $matches[2]);
}

return $this;
Expand Down
4 changes: 2 additions & 2 deletions performance/ThemeRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public function __construct(
public function compile(): void
{
foreach ($this->tests as $test) {
$this->templateFactory->parse($test->liquid);
$this->templateFactory->parseString($test->liquid);
if ($test->layoutLiquid !== null) {
$this->templateFactory->parse($test->layoutLiquid);
$this->templateFactory->parseString($test->layoutLiquid);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions performance/ThemeTestTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function pageTemplate(): string

public function compile(): CompiledThemeTestTemplate
{
$template = $this->factory->parse($this->liquid);
$layout = $this->layoutLiquid !== null ? $this->factory->parse($this->layoutLiquid) : null;
$template = $this->factory->parseString($this->liquid);
$layout = $this->layoutLiquid !== null ? $this->factory->parseString($this->layoutLiquid) : null;

return new CompiledThemeTestTemplate(
factory: $this->factory,
Expand Down
35 changes: 35 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#3 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
count: 3
path: performance/Shopify/CustomFilters.php

-
message: "#^Cannot access offset 'articles' on mixed\\.$#"
count: 1
path: performance/Shopify/Database.php

-
message: "#^Parameter \\#1 \\$array of static method Keepsuit\\\\Liquid\\\\Support\\\\Arr\\:\\:first\\(\\) expects array, mixed given\\.$#"
count: 1
path: performance/Shopify/Database.php

-
message: "#^Comparison operation \"\\>\" between 0 and 0 is always false\\.$#"
count: 2
path: performance/benchmark.php

-
message: "#^Strict comparison using \\!\\=\\= between null and null will always evaluate to false\\.$#"
count: 1
path: src/Profiler/Profiler.php

-
message: "#^Access to protected property Spatie\\\\Invade\\\\Invader\\<Keepsuit\\\\Liquid\\\\Render\\\\Context\\>\\:\\:\\$scopes\\.$#"
count: 2
path: tests/Stubs/ContextDrop.php

-
message: "#^Method Keepsuit\\\\Liquid\\\\Tests\\\\Stubs\\\\ContextDrop\\:\\:loopPos\\(\\) should return int\\|null but returns mixed\\.$#"
count: 1
path: tests/Stubs/ContextDrop.php

-
message: "#^Parameter \\#3 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
count: 1
path: tests/Stubs/TestDrop.php
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ parameters:
level: 9
paths:
- src
- performance
- tests/Stubs
tmpDir: build/phpstan
treatPhpDocTypesAsCertain: false
checkMissingIterableValueType: false
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/LiquidFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface LiquidFileSystem
{
public function readTemplateFile(string $templatePath): string;
public function readTemplateFile(string $templateName): string;
}
4 changes: 0 additions & 4 deletions src/Exceptions/StackLevelException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@

class StackLevelException extends LiquidException
{
public function __construct(string $message = 'Nesting too deep', int $code = 0, int $severity = 1, ?string $filename = __FILE__, ?int $line = __LINE__, \Throwable $previous = null)
{
parent::__construct($message, $code, $severity, $filename, $line, $previous);
}
}
6 changes: 3 additions & 3 deletions src/Exceptions/TagDisabledException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Keepsuit\Liquid\Exceptions;

use Keepsuit\Liquid\Parse\ParseContext;
use Keepsuit\Liquid\Support\I18n;

class TagDisabledException extends LiquidException
{
public function __construct(string $tagName, ParseContext $parseContext)
public function __construct(string $tagName, I18n $locale)
{
parent::__construct($parseContext->locale->translate('errors.disabled.tag', ['tag' => $tagName]));
parent::__construct($locale->translate('errors.disabled.tag', ['tag' => $tagName]));
}
}
2 changes: 1 addition & 1 deletion src/FileSystems/BlankFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class BlankFileSystem implements LiquidFileSystem
{
public function readTemplateFile(string $templatePath): string
public function readTemplateFile(string $templateName): string
{
throw new FileSystemException('This liquid context does not allow includes.');
}
Expand Down
4 changes: 2 additions & 2 deletions src/FileSystems/LocalFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public function __construct(
) {
}

public function readTemplateFile(string $templatePath): string
public function readTemplateFile(string $templateName): string
{
$fullPath = $this->fullPath($templatePath);
$fullPath = $this->fullPath($templateName);

$content = file_get_contents($fullPath);

Expand Down
10 changes: 4 additions & 6 deletions src/Nodes/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
class Document implements CanBeRendered
{
public function __construct(
protected readonly ParseContext $parseContext,
protected BlockBodySection $body,
) {
}

public static function parse(Tokenizer $tokenizer, ParseContext $parseContext): Document
public static function parse(ParseContext $parseContext, Tokenizer $tokenizer): Document
{
try {
$bodySections = BlockParser::forDocument()->parse($tokenizer, $parseContext);
Expand All @@ -27,13 +26,12 @@ public static function parse(Tokenizer $tokenizer, ParseContext $parseContext):
$exception = SyntaxException::unexpectedOuterTag($parseContext, $exception->tagName ?? '');
}

$exception->lineNumber = $parseContext->lineNumber;

throw $exception;
$parseContext->handleError($exception);
} catch (\Throwable $exception) {
$parseContext->handleError($exception);
}

return new Document(
parseContext: $parseContext,
body: $bodySections[0] ?? new BlockBodySection()
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Parse/BlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ protected function parseForDocument(Tokenizer $tokenizer, ParseContext $parseCon
$tagClass = $parseContext->tagRegistry->get($tagName) ?? null;

if ($tagClass !== null) {
$tag = (new $tagClass($markup, $parseContext));
$tag->parse($tokenizer);
$tag = (new $tagClass($markup, $parseContext->lineNumber));
$tag->parse($parseContext, $tokenizer);
$section->pushNode($tag);

continue;
Expand Down Expand Up @@ -184,7 +184,8 @@ protected function parseForLiquidTag(Tokenizer $tokenizer, ParseContext $parseCo
$tagClass = $parseContext->tagRegistry->get($tagName) ?? null;

if ($tagClass !== null) {
$tag = (new $tagClass($markup, $parseContext))->parse($tokenizer);
$tag = (new $tagClass($markup, $parseContext->lineNumber));
$tag->parse($parseContext, $tokenizer);
$section->pushNode($tag);

continue;
Expand Down
Loading

0 comments on commit 437f138

Please sign in to comment.