Skip to content

Commit

Permalink
Refactor Rename
Browse files Browse the repository at this point in the history
Signed-off-by: ramchale <[email protected]>
  • Loading branch information
ramchale committed Jan 8, 2025
1 parent 8c66c54 commit ef89bc3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 94 deletions.
57 changes: 15 additions & 42 deletions src/File/Rename.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace Laminas\Filter\File;

use Laminas\Filter;
use Laminas\Filter\Exception;
use Laminas\Stdlib\ArrayUtils;
use Traversable;
use Laminas\Filter\FilterInterface;

use function basename;
use function count;
Expand All @@ -28,14 +26,11 @@
use const DIRECTORY_SEPARATOR;

/**
* @psalm-type Options = array{
* file?: array{source?: string, target?: string, overwrite?: bool, randomize?: bool},
* ...
* }
* @psalm-type FileConfig = array{target: string, source?: string, overwrite?: bool, randomize?: bool}
* @psalm-type Options = array{ file?: FileConfig }
* @template TOptions of Options
* @template-extends Filter\AbstractFilter<TOptions>
*/
final class Rename extends Filter\AbstractFilter
final class Rename implements FilterInterface

Check failure on line 33 in src/File/Rename.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MissingTemplateParam

src/File/Rename.php:33:31: MissingTemplateParam: Laminas\Filter\File\Rename has missing template params when extending Laminas\Filter\FilterInterface, expecting 1 (see https://psalm.dev/182)
{
/**
* Internal array of array(source, target, overwrite)
Expand All @@ -52,22 +47,15 @@ final class Rename extends Filter\AbstractFilter
* 'overwrite' => Shall existing files be overwritten ?
* 'randomize' => Shall target files have a random postfix attached?
*
* @param string|array|Traversable $options Target file or directory to be renamed
* @param Options $options Target file or directory to be renamed
* @throws Exception\InvalidArgumentException
*/
public function __construct($options = [])
public function __construct(array $options = [])
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
} elseif (is_string($options)) {
$options = ['target' => $options];
} elseif (! is_array($options)) {
throw new Exception\InvalidArgumentException(
'Invalid options argument provided to filter'
);
}
$this->files = [];
$this->addFile($options);

Check failure on line 56 in src/File/Rename.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

InvalidArgument

src/File/Rename.php:56:24: InvalidArgument: Argument 1 of Laminas\Filter\File\Rename::addFile expects array{overwrite?: bool, randomize?: bool, source?: string, target: string}|string, but array{file?: array{overwrite?: bool, randomize?: bool, source?: string, target: string}} with additional array shape fields (file) was provided (see https://psalm.dev/004)

$this->setFile($options);
return $this;

Check failure on line 58 in src/File/Rename.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

InvalidReturnStatement

src/File/Rename.php:58:16: InvalidReturnStatement: No return values are expected for Laminas\Filter\File\Rename::__construct (see https://psalm.dev/128)
}

/**
Expand All @@ -80,26 +68,6 @@ public function getFile()
return $this->files;
}

/**
* Sets a new file or directory as target, deleting existing ones
*
* Array accepts the following keys:
* 'source' => Source filename or directory which will be renamed
* 'target' => Target filename or directory, the new name of the sourcefile
* 'overwrite' => Shall existing files be overwritten?
* 'randomize' => Shall target files have a random postfix attached?
*
* @param string|array{source?: string, target?: string, overwrite?: bool, randomize?: bool} $options
* @return self
*/
public function setFile($options)
{
$this->files = [];
$this->addFile($options);

return $this;
}

/**
* Adds a new file or directory as target to the existing ones
*
Expand All @@ -109,7 +77,7 @@ public function setFile($options)
* 'overwrite' => Shall existing files be overwritten?
* 'randomize' => Shall target files have a random postfix attached?
*
* @param string|array{source?: string, target?: string, overwrite?: bool, randomize?: bool} $options $options
* @param string|FileConfig $options
* @return Rename
* @throws Exception\InvalidArgumentException
*/
Expand Down Expand Up @@ -359,4 +327,9 @@ protected function _getFileName($file)

return $rename;
}

public function __invoke(mixed $value): mixed
{
return $this->filter($value);
}
}
56 changes: 4 additions & 52 deletions test/File/RenameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,28 +87,6 @@ public static function returnValidFilterInputProvider(): array
$newDirFile = self::getTempSubDirectory() . '/test_file.txt';

return [
'Configured with target file path and filtering file path' => [
'options' => $newFile,
'input' => $oldFile,
'expectedGetFileResult' => [
'source' => '*',
'target' => $newFile,
'overwrite' => false,
'randomize' => false,
],
'expectedFilterResult' => $newFile,
],
'Configured with target file path and filtering file path array' => [
'options' => $newFile,
'input' => ['tmp_name' => $oldFile],
'expectedGetFileResult' => [
'source' => '*',
'target' => $newFile,
'overwrite' => false,
'randomize' => false,
],
'expectedFilterResult' => ['tmp_name' => $newFile],
],
'Configured with array' => [
'options' => ['source' => $oldFile, 'target' => $newFile],
'input' => $oldFile,
Expand Down Expand Up @@ -159,17 +137,6 @@ public static function returnValidFilterInputProvider(): array
],
'expectedFilterResult' => $newFile,
],
'Configured with target directory and filtering file path' => [
'options' => $newDir,
'input' => $oldFile,
'expectedGetFileResult' => [
'source' => '*',
'target' => $newDir,
'overwrite' => false,
'randomize' => false,
],
'expectedFilterResult' => $newDirFile,
],
'Configured with array and filtering file path' => [
'options' => ['source' => $oldFile, 'target' => $newDir],
'input' => $oldFile,
Expand Down Expand Up @@ -213,7 +180,7 @@ public static function returnInvalidFilterInputProvider(): array

return [
'Source file non-existent' => [
'options' => $newFile,
'options' => ['target' => $newFile],
'input' => 'non-existent-file.txt',
'expectedGetFileResult' => [
'source' => '*',
Expand Down Expand Up @@ -458,8 +425,8 @@ public function testAddFileWithString(): void
$oldFile = self::getTempPath() . '/' . self::TEST_FILE_NAME;
$newFile = self::getTempPath() . '/new_file.xml';

$filter = new FileRename($oldFile);
$filter->addFile($newFile);
$filter = new FileRename(['target' => $oldFile]);
$filter->addFile(['target' => $newFile]);

self::assertSame(
[
Expand All @@ -481,21 +448,6 @@ public function testAddFileWithString(): void
}
}

public function testAddFileWithInvalidOption(): void
{
$filter = new FileRename('invalid');
$this->expectException(Exception\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid options');
$filter->addFile(1234);
}

public function testInvalidConstruction(): void
{
$this->expectException(Exception\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid options');
new FileRename(1234);
}

/** @return list<array{0: mixed}> */
public static function returnUnfilteredDataProvider(): array
{
Expand All @@ -519,7 +471,7 @@ public function testReturnUnfiltered(mixed $input): void
{
self::createSourceFile();

$filter = new FileRename(self::getTempPath() . '/new_file.xml');
$filter = new FileRename(['target' => self::getTempPath() . '/new_file.xml']);

self::assertSame($input, $filter($input));
}
Expand Down

0 comments on commit ef89bc3

Please sign in to comment.