Skip to content

Commit

Permalink
WIP Split move and rename
Browse files Browse the repository at this point in the history
Signed-off-by: ramchale <[email protected]>
  • Loading branch information
ramchale committed Jan 21, 2025
1 parent 5e39a17 commit 06145e0
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 146 deletions.
65 changes: 0 additions & 65 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,6 @@
<code><![CDATA[isOptions]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/File/Rename.php">
<DeprecatedClass>
<code><![CDATA[Filter\AbstractFilter]]></code>
</DeprecatedClass>
<DocblockTypeContradiction>
<code><![CDATA[is_array($options)]]></code>
<code><![CDATA[is_array($options)]]></code>
</DocblockTypeContradiction>
<InvalidReturnStatement>
<code><![CDATA[$file]]></code>
</InvalidReturnStatement>
<MixedArgument>
<code><![CDATA[$file['source']]]></code>
<code><![CDATA[$file['source']]]></code>
<code><![CDATA[$file['target']]]></code>
<code><![CDATA[$file['target']]]></code>
<code><![CDATA[$file['target']]]></code>
<code><![CDATA[$file['target']]]></code>
<code><![CDATA[$file['target']]]></code>
<code><![CDATA[$value]]></code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code><![CDATA[$options]]></code>
</MixedArgumentTypeCoercion>
<MixedArrayAccess>
<code><![CDATA[$file['source']]]></code>
<code><![CDATA[$file['target']]]></code>
<code><![CDATA[$file['target']]]></code>
<code><![CDATA[$file['target']]]></code>
</MixedArrayAccess>
<MixedArrayAssignment>
<code><![CDATA[$uploadData['tmp_name']]]></code>
</MixedArrayAssignment>
<MixedAssignment>
<code><![CDATA[$uploadData['tmp_name']]]></code>
<code><![CDATA[$value]]></code>
<code><![CDATA[$value]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[string]]></code>
</MixedInferredReturnType>
<MixedReturnStatement>
<code><![CDATA[$file['target']]]></code>
</MixedReturnStatement>
<PossiblyUndefinedArrayOffset>
<code><![CDATA[$files['source']]]></code>
<code><![CDATA[$rename['randomize']]]></code>
</PossiblyUndefinedArrayOffset>
<PossiblyUndefinedVariable>
<code><![CDATA[$uploadData]]></code>
<code><![CDATA[$uploadData]]></code>
</PossiblyUndefinedVariable>
<PropertyTypeCoercion>
<code><![CDATA[$this->files]]></code>
</PropertyTypeCoercion>
<RedundantConditionGivenDocblockType>
<code><![CDATA[is_string($file)]]></code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/File/RenameUpload.php">
<DeprecatedClass>
<code><![CDATA[AbstractFilter]]></code>
Expand Down Expand Up @@ -203,12 +144,6 @@
<code><![CDATA[[$value, $expected]]]></code>
</MixedAssignment>
</file>
<file src="test/File/RenameTest.php">
<InvalidArgument>
<code><![CDATA[1234]]></code>
<code><![CDATA[1234]]></code>
</InvalidArgument>
</file>
<file src="test/File/RenameUploadTest.php">
<InvalidArgument>
<code><![CDATA[1234]]></code>
Expand Down
60 changes: 40 additions & 20 deletions src/File/Rename.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,60 @@
use Laminas\Filter\Exception;
use Laminas\Filter\FilterInterface;

use function basename;
use function file_exists;
use function fnmatch;
use function is_dir;
use function is_string;
use function is_writable;
use function pathinfo;
use function realpath;
use function rename;
use function sprintf;
use function strlen;
use function uniqid;
use function unlink;

/**
* @psalm-type Options = array{ target?: string, source?: string, overwrite?: bool, randomize?: bool }
* @psalm-type Options = array{
* match?: non-empty-string,
* target_directory?: non-empty-string,
* rename_to?: string,
* overwrite?: bool,
* randomize?: bool
* }
* @implements FilterInterface<string>
*/
final class Rename implements FilterInterface
{
private string $match;
private string $target;
private string $source;
private string $renameTo;
private bool $overwrite;
private bool $randomize;

/** @param Options $options */
public function __construct(array $options = [])
{
$this->target = $options['target'] ?? '*';
$this->source = $options['source'] ?? '*';
$this->match = $options['match'] ?? '*';
$this->target = $options['target_directory'] ?? '*';
$this->renameTo = $options['rename_to'] ?? '*';
$this->overwrite = $options['overwrite'] ?? false;
$this->randomize = $options['randomize'] ?? false;

if ($this->target !== '*') {
if (! is_dir($this->target)) {
throw new Exception\InvalidArgumentException(sprintf(
'The target directory "%s" does not exist',
$this->target
));
}

if (! is_writable($this->target)) {
throw new Exception\InvalidArgumentException(sprintf(
'The target directory "%s" is not writable',
$this->target
));
}
}
}

/**
Expand All @@ -45,6 +70,10 @@ private function renameFile(string $sourceFilePath): string
{
$file = $this->getFileName($sourceFilePath);

if ($file === $sourceFilePath) {
return $file;
}

if ($this->overwrite && file_exists($file)) {
unlink($file);
}
Expand Down Expand Up @@ -81,7 +110,7 @@ public function filter(mixed $value): mixed
return $value;
}

if ($this->source !== '*' && $this->source !== $value) {
if (! fnmatch($this->match, $value)) {
return $value;
}

Expand All @@ -94,21 +123,12 @@ public function filter(mixed $value): mixed

private function getFileName(string $file): string
{
$target = $this->target;
$fileInfo = pathinfo($file);

if ($this->target === '*') {
$target = $this->source === '*' ? $file : $this->source;
}
$targetName = $this->renameTo === '*' ? $fileInfo['basename'] : $this->renameTo;
$targetDir = $this->target === '*' ? $fileInfo['dirname'] : $this->target;

if (is_dir($target)) {
$name = basename($file);
$last = $target[strlen($target) - 1];
if ($last !== '/' && $last !== '\\') {
$target .= '/';
}

$target .= $name;
}
$target = $targetDir . '/' . $targetName;

if ($this->randomize) {
$info = pathinfo($target);
Expand Down
Loading

0 comments on commit 06145e0

Please sign in to comment.