Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict variables improvements #21

Merged
merged 4 commits into from
May 25, 2024
Merged
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
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
"scripts": {
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"format": "vendor/bin/pint",
"lint": "vendor/bin/phpstan analyse",
"lint": "vendor/bin/pint && vendor/bin/phpstan analyse",
"benchmark": "vendor/bin/phpbench run --report=aggregate --php-config=\"opcache.enable: 1, opcache.enable_cli: 1\"",
"profile": "vendor/bin/phpbench xdebug:profile"
},
Expand Down
1 change: 1 addition & 0 deletions performance/Shopify/PaginateTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function render(RenderContext $context): string
{
return $context->stack(function (RenderContext $context) {
$currentPage = $context->get('current_page');
$currentPage = is_int($currentPage) ? $currentPage : 1;

$collection = $context->get($this->collectionName);
$collection = match (true) {
Expand Down
4 changes: 1 addition & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ parameters:
- tests/Stubs
tmpDir: build/phpstan
treatPhpDocTypesAsCertain: false
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: true
checkPhpDocMissingReturn: true
featureToggles:
disableCheckMissingIterableValueType: false

ignoreErrors:
- identifier: missingType.iterableValue
- '#Method .+ should return .+ but returns mixed#'
14 changes: 7 additions & 7 deletions src/Nodes/VariableLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use Keepsuit\Liquid\Contracts\HasParseTreeVisitorChildren;
use Keepsuit\Liquid\Contracts\IsContextAware;
use Keepsuit\Liquid\Exceptions\SyntaxException;
use Keepsuit\Liquid\Exceptions\UndefinedVariableException;
use Keepsuit\Liquid\Parse\LexerOptions;
use Keepsuit\Liquid\Render\RenderContext;
use Keepsuit\Liquid\Support\MissingValue;
use Keepsuit\Liquid\Support\Str;
use Keepsuit\Liquid\Support\UndefinedVariable;

class VariableLookup implements CanBeEvaluated, HasParseTreeVisitorChildren
{
Expand Down Expand Up @@ -65,7 +65,7 @@ public function toString(): string
return $this->name;
}

return sprintf('%s.%s', $this->name, implode('.', $this->lookups));
return implode('.', [$this->name, ...$this->lookups]);
}

public function __toString(): string
Expand All @@ -85,6 +85,10 @@ public function evaluate(RenderContext $context): mixed
$variables = $context->findVariables($name);

if ($this->lookups === []) {
if ($context->strictVariables && $variables === []) {
return new UndefinedVariable($this->toString());
}

return $variables[0] ?? null;
}

Expand Down Expand Up @@ -117,11 +121,7 @@ public function evaluate(RenderContext $context): mixed
return $object;
}

if ($context->strictVariables) {
throw new UndefinedVariableException($this->toString());
}

return null;
return $context->strictVariables ? new UndefinedVariable($this->toString()) : null;
}

protected function applyFilter(RenderContext $context, mixed $object, string $filter): mixed
Expand Down
8 changes: 0 additions & 8 deletions src/Render/RenderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Keepsuit\Liquid\Exceptions\ResourceLimitException;
use Keepsuit\Liquid\Exceptions\StackLevelException;
use Keepsuit\Liquid\Exceptions\StandardException;
use Keepsuit\Liquid\Exceptions\UndefinedVariableException;
use Keepsuit\Liquid\FileSystems\BlankFileSystem;
use Keepsuit\Liquid\Interrupts\Interrupt;
use Keepsuit\Liquid\Nodes\VariableLookup;
Expand Down Expand Up @@ -168,9 +167,6 @@ public function has(string $key): bool
return $this->get($key) !== null;
}

/**
* @throws UndefinedVariableException
*/
public function findVariables(string $key): array
{
$variables = [];
Expand All @@ -183,10 +179,6 @@ public function findVariables(string $key): array

$variables = array_values(array_filter($variables, fn (mixed $value) => ! $value instanceof MissingValue));

if ($variables === []) {
return $this->strictVariables ? throw new UndefinedVariableException($key) : [];
}

foreach ($variables as $variable) {
if ($variable instanceof IsContextAware) {
$variable->setContext($this);
Expand Down
13 changes: 11 additions & 2 deletions src/Support/FilterRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Keepsuit\Liquid\Contracts\IsContextAware;
use Keepsuit\Liquid\Exceptions\InvalidArgumentException;
use Keepsuit\Liquid\Exceptions\UndefinedFilterException;
use Keepsuit\Liquid\Exceptions\UndefinedVariableException;
use Keepsuit\Liquid\Render\RenderContext;

class FilterRegistry
Expand Down Expand Up @@ -44,14 +45,22 @@ public function register(string $filterClass): static
}

/**
* @throws UndefinedFilterException
* @throws UndefinedFilterException|UndefinedVariableException
*/
public function invoke(RenderContext $context, string $filterName, mixed $value, mixed ...$args): mixed
{
$filter = $this->filters[$filterName] ?? null;

if ($filter !== null) {
return $filter($context, $value, ...$args);
try {
return $filter($context, $value, ...$args);
} catch (\TypeError $e) {
if ($value instanceof UndefinedVariable) {
throw new UndefinedVariableException($value->variableName);
}

throw $e;
}
}

if ($context->strictVariables) {
Expand Down
29 changes: 29 additions & 0 deletions src/Support/UndefinedVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Keepsuit\Liquid\Support;

use Keepsuit\Liquid\Contracts\AsLiquidValue;
use Keepsuit\Liquid\Contracts\CanBeRendered;
use Keepsuit\Liquid\Exceptions\UndefinedVariableException;
use Keepsuit\Liquid\Render\RenderContext;

class UndefinedVariable implements AsLiquidValue, CanBeRendered
{
public function __construct(public readonly string $variableName)
{
}

public function render(RenderContext $context): string
{
if ($context->strictVariables) {
throw new UndefinedVariableException($this->variableName);
}

return '';
}

public function toLiquidValue(): string|int|float|bool|null
{
return null;
}
}
Loading