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

Add variadic parameter $default to ViewInterface::getParameter() #272

Merged
merged 2 commits into from
Sep 29, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Enh #269: Bump PHP version to `^8.1` and refactor code (@vjik)
- Chg #271: Remove deprecated methods `withDefaultExtension()` and `getDefaultExtension()` from `ViewInterface` (@vjik)
- Chg #271: Rename configuration parameter `defaultExtension` to `fallbackExtension` (@vjik)
- Chg #272: Add variadic parameter `$default` to `ViewInterface::getParameter()` (@vjik)

## 10.0.0 June 28, 2024

Expand Down
1 change: 1 addition & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ application when you upgrade the package from one version to another.
- Removed `ViewInterface` methods `withDefaultExtension()` and `getDefaultExtension()`. Use `withFallbackExtension()`
and `getFallbackExtensions()` instead, respectively.
- Rename configuration parameter `defaultExtension` to `fallbackExtension`.
- Added variadic parameter `$default` to `ViewInterface::getParameter()`.

## Upgrade from 9.x

Expand Down
9 changes: 3 additions & 6 deletions src/State/StateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use InvalidArgumentException;

use function array_key_exists;
use function func_get_args;
use function is_array;

/**
Expand Down Expand Up @@ -102,15 +100,14 @@ public function removeParameter(string $id): static
*
* @return mixed The value of the parameter.
*/
public function getParameter(string $id)
public function getParameter(string $id, mixed ...$default): mixed
{
if (isset($this->parameters[$id])) {
return $this->parameters[$id];
}

$args = func_get_args();
if (array_key_exists(1, $args)) {
return $args[1];
if (!empty($default)) {
return reset($default);
}
samdark marked this conversation as resolved.
Show resolved Hide resolved

throw new InvalidArgumentException('Parameter "' . $id . '" not found.');
Expand Down
2 changes: 1 addition & 1 deletion src/ViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function removeParameter(string $id): static;
*
* @return mixed The value of the parameter.
*/
public function getParameter(string $id);
public function getParameter(string $id, mixed ...$default): mixed;

/**
* Checks the existence of a common parameter by ID.
Expand Down
7 changes: 3 additions & 4 deletions src/ViewTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
use function array_merge;
use function array_pop;
use function basename;
use function call_user_func_array;
use function call_user_func;
use function crc32;
use function dechex;
use function dirname;
use function end;
use function func_get_args;
use function is_file;
use function pathinfo;
use function substr;
Expand Down Expand Up @@ -115,7 +114,7 @@
public function withFallbackExtension(string $fallbackExtension, string ...$otherFallbacks): static
{
$new = clone $this;
$new->fallbackExtensions = [$fallbackExtension, ...array_values($otherFallbacks)];

Check warning on line 117 in src/ViewTrait.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayValues": --- Original +++ New @@ @@ public function withFallbackExtension(string $fallbackExtension, string ...$otherFallbacks) : static { $new = clone $this; - $new->fallbackExtensions = [$fallbackExtension, ...array_values($otherFallbacks)]; + $new->fallbackExtensions = [$fallbackExtension, ...$otherFallbacks]; return $new; } /**
return $new;
}

Expand Down Expand Up @@ -304,9 +303,9 @@
*
* @return mixed The value of the parameter.
*/
public function getParameter(string $id)
public function getParameter(string $id, mixed ...$default): mixed
{
return call_user_func_array([$this->state, 'getParameter'], func_get_args());
return call_user_func([$this->state, 'getParameter'], $id, ...$default);
}

/**
Expand Down Expand Up @@ -433,7 +432,7 @@
}

$output = '';
$this->viewFiles[] = [

Check warning on line 435 in src/ViewTrait.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ throw new ViewNotFoundException("The view file \"{$viewFile}\" does not exist."); } $output = ''; - $this->viewFiles[] = ['resolved' => $viewFile, 'requested' => $requestedFile]; + $this->viewFiles[] = ['requested' => $requestedFile]; if ($this->beforeRender($viewFile, $parameters)) { $ext = pathinfo($viewFile, PATHINFO_EXTENSION); $renderer = $this->renderers[$ext] ?? new PhpTemplateRenderer();
'resolved' => $viewFile,
'requested' => $requestedFile,
];
Expand Down
Loading