diff --git a/docs/book/v3/migration/v2-to-v3.md b/docs/book/v3/migration/v2-to-v3.md index 802eebde..5bfa45ef 100644 --- a/docs/book/v3/migration/v2-to-v3.md +++ b/docs/book/v3/migration/v2-to-v3.md @@ -131,6 +131,15 @@ Additionally, `$options['pattern']` _must_ be provided at construction time or a Exceptions for invalid or empty patterns are now thrown during construct rather than when the filter is invoked. +#### `RealPath` + +The following methods have been removed: + +- `setExists` +- `getExists` + +The constructor now only accepts an associative array of [documented options](../standard-filters.md#realPath). + #### `SeparatorToCamelCase` The constructor now only accepts an associative array of [documented options](../word.md#separatorToCamelCase). diff --git a/docs/book/v3/standard-filters.md b/docs/book/v3/standard-filters.md index df554c3b..7ac982c4 100644 --- a/docs/book/v3/standard-filters.md +++ b/docs/book/v3/standard-filters.md @@ -1156,11 +1156,11 @@ $filtered = $filter->filter($path); ### Non-Existing Paths Sometimes it is useful to get paths to files that do n0t exist; e.g., when you -want to get the real path for a path you want to create. You can then either -provide a `FALSE` `exists` value at initiation, or use `setExists()` to set it. +want to get the real path for a path you want to create. You can then +provide a `FALSE` `exists` option at initiation. ```php -$filter = new Laminas\Filter\RealPath(false); +$filter = new Laminas\Filter\RealPath(['exists' => false]); $path = '/www/var/path/../../non/existing/path'; $filtered = $filter->filter($path); diff --git a/psalm-baseline.xml b/psalm-baseline.xml index b0a966b6..c7c3b6f0 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -24,6 +24,7 @@ + @@ -536,27 +537,6 @@ - - - - - - options]]> - true, - ]]]> - - - - - - - - - - - - @@ -828,9 +808,6 @@ - - - diff --git a/src/RealPath.php b/src/RealPath.php index dff15b3c..94a8f328 100644 --- a/src/RealPath.php +++ b/src/RealPath.php @@ -5,7 +5,6 @@ namespace Laminas\Filter; use Laminas\Stdlib\ErrorHandler; -use Traversable; use function array_pop; use function explode; @@ -24,66 +23,20 @@ /** * @psalm-type Options = array{ - * exists: bool, - * ... + * exists?: bool, * } - * @template TOptions of Options - * @extends AbstractFilter + * @implements FilterInterface> */ -final class RealPath extends AbstractFilter +final class RealPath implements FilterInterface { - /** @var TOptions $options */ - protected $options = [ - 'exists' => true, - ]; + private readonly bool $pathMustExist; - /** - * @param bool|Traversable|Options $existsOrOptions Options to set - */ - public function __construct($existsOrOptions = true) + /** @param Options $options */ + public function __construct(array $options = []) { - if ($existsOrOptions !== null) { - if (! static::isOptions($existsOrOptions)) { - $this->setExists($existsOrOptions); - } else { - $this->setOptions($existsOrOptions); - } - } - } - - /** - * Sets if the path has to exist - * TRUE when the path must exist - * FALSE when not existing paths can be given - * - * @param bool $flag Path must exist - * @return self - */ - public function setExists($flag = true) - { - $this->options['exists'] = (bool) $flag; - return $this; + $this->pathMustExist = $options['exists'] ?? true; } - /** - * Returns true if the filtered path must exist - * - * @return bool - */ - public function getExists() - { - return $this->options['exists']; - } - - /** - * Defined by Laminas\Filter\FilterInterface - * - * Returns realpath($value) - * - * If the value provided is non-scalar, the value will remain unfiltered - * - * @psalm-return ($value is string ? string : mixed) - */ public function filter(mixed $value): mixed { if (! is_string($value)) { @@ -91,7 +44,7 @@ public function filter(mixed $value): mixed } $path = (string) $value; - if ($this->options['exists']) { + if ($this->pathMustExist) { return realpath($path); } @@ -132,4 +85,9 @@ public function filter(mixed $value): mixed return $drive . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $stack); } + + public function __invoke(mixed $value): mixed + { + return $this->filter($value); + } } diff --git a/test/RealPathTest.php b/test/RealPathTest.php index a5a2d5ea..5488b036 100644 --- a/test/RealPathTest.php +++ b/test/RealPathTest.php @@ -16,20 +16,17 @@ class RealPathTest extends TestCase { - private RealPathFilter $filter; - - public function setUp(): void - { - $this->filter = new RealPathFilter(); - } - /** * Ensures expected behavior for existing file */ public function testFileExists(): void { + $filter = new RealPathFilter(); + $filename = __DIR__ . '/_files/file.1'; - $result = $this->filter->filter($filename); + $result = $filter->filter($filename); + + self::assertIsString($result); self::assertStringContainsString($filename, $result); } @@ -38,28 +35,19 @@ public function testFileExists(): void */ public function testFileNonexistent(): void { + $filter = new RealPathFilter(); + $path = '/path/to/nonexistent'; if (str_contains(PHP_OS, 'BSD')) { - self::assertSame($path, $this->filter->filter($path)); + self::assertSame($path, $filter->filter($path)); } else { - self::assertSame(false, $this->filter->filter($path)); + self::assertSame(false, $filter->filter($path)); } } - public function testGetAndSetExistsParameter(): void - { - self::assertTrue($this->filter->getExists()); - $this->filter->setExists(false); - self::assertFalse($this->filter->getExists()); - - $this->filter->setExists(['unknown']); - self::assertTrue($this->filter->getExists()); - } - public function testNonExistentPath(): void { - $filter = $this->filter; - $filter->setExists(false); + $filter = new RealPathFilter(['exists' => false]); $path = __DIR__ . DIRECTORY_SEPARATOR . '_files'; self::assertSame($path, $filter($path));