Skip to content

Commit

Permalink
Refactor RenameUpload filter to implement FlterInterface
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Thole <[email protected]>
  • Loading branch information
marcelthole committed Jan 9, 2025
1 parent 2477249 commit 7396466
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 447 deletions.
12 changes: 7 additions & 5 deletions docs/book/v3/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ The following set of options are supported:
> It is generally a better idea to supply an internal filename to avoid
> security risks.
`RenameUpload` does not support an array of options like the`Rename` filter.
`RenameUpload` does not support an array of options like the `Rename` filter.
When filtering HTML5 file uploads with the `multiple` attribute set, all files
will be filtered with the same option settings.

Expand All @@ -224,13 +224,15 @@ $files = $request->getFiles();
// i.e. $files['my-upload']['tmp_name'] === '/tmp/php5Wx0aJ'
// i.e. $files['my-upload']['name'] === 'myfile.txt'

// 'target' option is assumed if param is a string
$filter = new \Laminas\Filter\File\RenameUpload('./data/uploads/');
$filter = new \Laminas\Filter\File\RenameUpload(['target' => './data/uploads/']);
echo $filter->filter($files['my-upload']);
// File has been moved to './data/uploads/php5Wx0aJ'

// ... or retain the uploaded file name
$filter->setUseUploadName(true);
$filter = new \Laminas\Filter\File\RenameUpload([
'target' => './data/uploads/',
'use_upload_name' => true,
]);
echo $filter->filter($files['my-upload']);
// File has been moved to './data/uploads/myfile.txt'
```
Expand All @@ -244,7 +246,7 @@ $request = new Request();
$files = $request->getFiles();
// i.e. $files['my-upload']['tmp_name'] === '/tmp/php5Wx0aJ'

$filter = new \Laminas\Filter\File\RenameUpload('./data/uploads/newfile.txt');
$filter = new \Laminas\Filter\File\RenameUpload(['target' => './data/uploads/newfile.txt']);
echo $filter->filter($files['my-upload']);
// File has been renamed to './data/uploads/newfile.txt'
```
Expand Down
22 changes: 22 additions & 0 deletions docs/book/v3/migration/v2-to-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,28 @@ So, to check if the path exists, ensure a validator (such as `Laminas\Validator\
Windows support has been dropped.
Which in some cases may now need a custom filter to handle Windows specific issues.


#### `RenameUpload`

The following methods have been removed:

- `setStreamFactory`

Check failure on line 255 in docs/book/v3/migration/v2-to-v3.md

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Documentation Linting [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integra...

Multiple consecutive blank lines [Expected: 1; Actual: 2]
- `getStreamFactory`
- `setTarget`
- `getTarget`
- `setUploadFileFactory`
- `getUploadFileFactory`
- `setUseUploadName`
- `getUseUploadName`
- `setUseUploadExtension`
- `getUseUploadExtension`
- `setOverwrite`
- `getOverwrite`
- `setRandomize`
- `getRandomize`

The constructor now only accepts an associative array of [documented options](../file.md#renameupload).

#### `SeparatorToCamelCase`

The constructor now only accepts an associative array of [documented options](../word.md#separatortocamelcase).
Expand Down
118 changes: 5 additions & 113 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<code><![CDATA[applyFilterOnlyToStringableValuesAndStringableArrayValues]]></code>
<code><![CDATA[getOptions]]></code>
<code><![CDATA[isOptions]]></code>
<code><![CDATA[setOptions]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/Dir.php">
Expand Down Expand Up @@ -80,105 +81,6 @@
<code><![CDATA[is_string($file)]]></code>
</RedundantConditionGivenDocblockType>
</file>
<file src="src/File/RenameUpload.php">
<DeprecatedClass>
<code><![CDATA[AbstractFilter]]></code>
</DeprecatedClass>
<DocblockTypeContradiction>
<code><![CDATA[$target === null]]></code>
<code><![CDATA[is_string($target)]]></code>
</DocblockTypeContradiction>
<InvalidNullableReturnType>
<code><![CDATA[string]]></code>
</InvalidNullableReturnType>
<InvalidPropertyAssignmentValue>
<code><![CDATA[$this->options]]></code>
<code><![CDATA[$this->options]]></code>
<code><![CDATA[$this->options]]></code>
<code><![CDATA[$this->options]]></code>
<code><![CDATA[$this->options]]></code>
<code><![CDATA[$this->options]]></code>
<code><![CDATA[$this->options]]></code>
<code><![CDATA[[
'target' => null,
'use_upload_name' => false,
'use_upload_extension' => false,
'overwrite' => false,
'randomize' => false,
'stream_factory' => null,
'upload_file_factory' => null,
]]]></code>
</InvalidPropertyAssignmentValue>
<MixedArgument>
<code><![CDATA[$clientFilename]]></code>
<code><![CDATA[$sourceFile]]></code>
<code><![CDATA[$sourceFile]]></code>
<code><![CDATA[$sourceFile]]></code>
<code><![CDATA[$sourceFile]]></code>
<code><![CDATA[$sourceFile]]></code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code><![CDATA[$value]]></code>
</MixedArgumentTypeCoercion>
<MixedArrayOffset>
<code><![CDATA[$this->alreadyFiltered[$sourceFile]]]></code>
<code><![CDATA[$this->alreadyFiltered[$sourceFile]]]></code>
<code><![CDATA[$this->alreadyFiltered[$sourceFile]]]></code>
</MixedArrayOffset>
<MixedAssignment>
<code><![CDATA[$clientFilename]]></code>
<code><![CDATA[$sourceFile]]></code>
<code><![CDATA[$sourceFile]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code><![CDATA[UploadedFileInterface]]></code>
<code><![CDATA[array<string, string>]]></code>
<code><![CDATA[string]]></code>
</MixedInferredReturnType>
<MixedReturnStatement>
<code><![CDATA[$this->alreadyFiltered[$alreadyFilteredKey]]]></code>
<code><![CDATA[$this->alreadyFiltered[$fileName]]]></code>
<code><![CDATA[$this->alreadyFiltered[$sourceFile]]]></code>
</MixedReturnStatement>
<MixedReturnTypeCoercion>
<code><![CDATA[$fileData]]></code>
<code><![CDATA[$this->alreadyFiltered[$sourceFile]]]></code>
</MixedReturnTypeCoercion>
<MoreSpecificImplementedParamType>
<code><![CDATA[$value]]></code>
</MoreSpecificImplementedParamType>
<NullableReturnStatement>
<code><![CDATA[$this->options['target']]]></code>
</NullableReturnStatement>
<PossiblyInvalidArgument>
<code><![CDATA[$sourceFile]]></code>
<code><![CDATA[$sourceFile]]></code>
</PossiblyInvalidArgument>
<PossiblyInvalidCast>
<code><![CDATA[$sourceFile]]></code>
<code><![CDATA[$sourceFile]]></code>
</PossiblyInvalidCast>
<PossiblyNullArgument>
<code><![CDATA[$clientFileName]]></code>
<code><![CDATA[$clientFileName]]></code>
<code><![CDATA[$clientFileName]]></code>
</PossiblyNullArgument>
<PossiblyUnusedMethod>
<code><![CDATA[setOverwrite]]></code>
<code><![CDATA[setRandomize]]></code>
<code><![CDATA[setUseUploadExtension]]></code>
<code><![CDATA[setUseUploadName]]></code>
</PossiblyUnusedMethod>
<PossiblyUnusedReturnValue>
<code><![CDATA[bool]]></code>
</PossiblyUnusedReturnValue>
<RedundantCastGivenDocblockType>
<code><![CDATA[(bool) $flag]]></code>
<code><![CDATA[(bool) $flag]]></code>
<code><![CDATA[(bool) $flag]]></code>
<code><![CDATA[(bool) $flag]]></code>
</RedundantCastGivenDocblockType>
</file>
<file src="src/Module.php">
<UnusedClass>
<code><![CDATA[Module]]></code>
Expand Down Expand Up @@ -214,20 +116,10 @@
<code><![CDATA[1234]]></code>
</InvalidArgument>
</file>
<file src="test/File/RenameUploadTest.php">
<InvalidArgument>
<code><![CDATA[1234]]></code>
</InvalidArgument>
<MixedArgument>
<code><![CDATA[$filter($this->sourceFile)]]></code>
<code><![CDATA[$filter($this->sourceFile)]]></code>
<code><![CDATA[$filter($this->sourceFile)]]></code>
<code><![CDATA[$filter($this->sourceFile)]]></code>
<code><![CDATA[$firstResult]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$firstResult]]></code>
</MixedAssignment>
<file src="test/File/RenameUploadMock.php">
<MethodSignatureMismatch>
<code><![CDATA[protected function moveUploadedFile(string $sourceFile, string $targetFile): void]]></code>
</MethodSignatureMismatch>
</file>
<file src="test/FilterPluginManagerCompatibilityTest.php">
<DeprecatedMethod>
Expand Down
Loading

0 comments on commit 7396466

Please sign in to comment.