Skip to content

Commit

Permalink
Refactor RealPath
Browse files Browse the repository at this point in the history
Signed-off-by: ramchale <[email protected]>
  • Loading branch information
ramchale committed Nov 11, 2024
1 parent 55ef969 commit eadfd3a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 104 deletions.
9 changes: 9 additions & 0 deletions docs/book/v3/migration/v2-to-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
6 changes: 3 additions & 3 deletions docs/book/v3/standard-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
25 changes: 1 addition & 24 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</PossiblyInvalidPropertyAssignmentValue>
<PossiblyUnusedMethod>
<code><![CDATA[applyFilterOnlyToStringableValuesAndStringableArrayValues]]></code>
<code><![CDATA[isOptions]]></code>
</PossiblyUnusedMethod>
<RedundantConditionGivenDocblockType>
<code><![CDATA[is_object($options)]]></code>
Expand Down Expand Up @@ -536,27 +537,6 @@
<code><![CDATA[Module]]></code>
</UnusedClass>
</file>
<file src="src/RealPath.php">
<DeprecatedClass>
<code><![CDATA[AbstractFilter]]></code>
</DeprecatedClass>
<InvalidPropertyAssignmentValue>
<code><![CDATA[$this->options]]></code>
<code><![CDATA[[
'exists' => true,
]]]></code>
</InvalidPropertyAssignmentValue>
<PossiblyInvalidArgument>
<code><![CDATA[$existsOrOptions]]></code>
<code><![CDATA[$existsOrOptions]]></code>
</PossiblyInvalidArgument>
<RedundantCastGivenDocblockType>
<code><![CDATA[(bool) $flag]]></code>
</RedundantCastGivenDocblockType>
<RedundantConditionGivenDocblockType>
<code><![CDATA[$existsOrOptions !== null]]></code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/StripTags.php">
<DeprecatedClass>
<code><![CDATA[AbstractFilter]]></code>
Expand Down Expand Up @@ -828,9 +808,6 @@
</PossiblyUnusedMethod>
</file>
<file src="test/RealPathTest.php">
<InvalidArgument>
<code><![CDATA[['unknown']]]></code>
</InvalidArgument>
<PossiblyUnusedMethod>
<code><![CDATA[returnUnfilteredDataProvider]]></code>
</PossiblyUnusedMethod>
Expand Down
68 changes: 13 additions & 55 deletions src/RealPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Laminas\Filter;

use Laminas\Stdlib\ErrorHandler;
use Traversable;

use function array_pop;
use function explode;
Expand All @@ -24,74 +23,28 @@

/**
* @psalm-type Options = array{
* exists: bool,
* ...
* exists?: bool,
* }
* @template TOptions of Options
* @extends AbstractFilter<TOptions>
* @implements FilterInterface<string|array<array-key, string|mixed>>
*/
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)) {
return $value;
}
$path = (string) $value;

if ($this->options['exists']) {
if ($this->pathMustExist) {
return realpath($path);
}

Expand Down Expand Up @@ -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);
}
}
32 changes: 10 additions & 22 deletions test/RealPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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));
Expand Down

0 comments on commit eadfd3a

Please sign in to comment.