Skip to content

Commit d545ef3

Browse files
Fix error in deleting code when no filepath exists (#614)
1 parent c1de324 commit d545ef3

File tree

14 files changed

+59
-35
lines changed

14 files changed

+59
-35
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ parameters:
1010
identifier: missingType.generics
1111

1212
-
13-
message: "#^Parameter \\#1 \\$field of method Cake\\\\Datasource\\\\EntityInterface\\:\\:get\\(\\) expects string, array\\<int, string\\>\\|string given\\.$#"
13+
message: '#^Parameter \#1 \$field of method Cake\\Datasource\\EntityInterface\:\:get\(\) expects string, array\<string\>\|string given\.$#'
1414
count: 1
1515
path: src/File/Path/DefaultProcessor.php
1616

src/File/Path/Basepath/DefaultTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,17 @@ public function basepath(): string
5353
if ($value === null) {
5454
throw new LogicException(sprintf(
5555
'Field value for substitution is missing: %s',
56-
$field
56+
$field,
5757
));
5858
} elseif (!is_scalar($value)) {
5959
throw new LogicException(sprintf(
6060
'Field value for substitution must be a integer, float, string or boolean: %s',
61-
$field
61+
$field,
6262
));
6363
} elseif (strlen((string)$value) < 1) {
6464
throw new LogicException(sprintf(
6565
'Field value for substitution must be non-zero in length: %s',
66-
$field
66+
$field,
6767
));
6868
}
6969

@@ -74,7 +74,7 @@ public function basepath(): string
7474
return str_replace(
7575
array_keys($replacements),
7676
array_values($replacements),
77-
$path
77+
$path,
7878
);
7979
}
8080
}

src/File/Path/DefaultProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function __construct(
6363
EntityInterface $entity,
6464
UploadedFileInterface|string $data,
6565
string $field,
66-
array $settings
66+
array $settings,
6767
) {
6868
$this->table = $table;
6969
$this->entity = $entity;

src/File/Path/Filename/DefaultTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function filename(): string
2323
$this->entity,
2424
$this->data,
2525
$this->field,
26-
$this->settings
26+
$this->settings,
2727
);
2828
}
2929

src/File/Path/ProcessorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
EntityInterface $entity,
2424
string|UploadedFileInterface $data,
2525
string $field,
26-
array $settings
26+
array $settings,
2727
);
2828

2929
/**

src/File/Transformer/DefaultTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
protected EntityInterface $entity,
2424
protected UploadedFileInterface $data,
2525
protected string $field,
26-
protected array $settings
26+
protected array $settings,
2727
) {
2828
}
2929

src/File/Transformer/TransformerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
EntityInterface $entity,
2424
UploadedFileInterface $data,
2525
string $field,
26-
array $settings
26+
array $settings,
2727
);
2828

2929
/**

src/File/Writer/DefaultWriter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(
3131
protected EntityInterface $entity,
3232
protected ?UploadedFileInterface $data,
3333
protected string $field,
34-
protected array $settings
34+
protected array $settings,
3535
) {
3636
}
3737

src/File/Writer/WriterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
EntityInterface $entity,
2424
?UploadedFileInterface $data,
2525
string $field,
26-
array $settings
26+
array $settings,
2727
);
2828

2929
/**

src/Model/Behavior/UploadBehavior.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,11 @@ public function afterDelete(EventInterface $event, EntityInterface $entity, Arra
163163
$result = true;
164164

165165
foreach ($this->getConfig(null, []) as $field => $settings) {
166-
if (in_array($field, $this->protectedFieldNames) || Hash::get($settings, 'keepFilesOnDelete', true)) {
166+
if (
167+
in_array($field, $this->protectedFieldNames)
168+
|| Hash::get($settings, 'keepFilesOnDelete', true)
169+
|| $entity->get($field) === null
170+
) {
167171
continue;
168172
}
169173

@@ -206,7 +210,7 @@ public function getPathProcessor(
206210
EntityInterface $entity,
207211
string|UploadedFileInterface $data,
208212
string $field,
209-
array $settings
213+
array $settings,
210214
): ProcessorInterface {
211215
/** @var class-string<\Josegonzalez\Upload\File\Path\ProcessorInterface> $processorClass */
212216
$processorClass = Hash::get($settings, 'pathProcessor', DefaultProcessor::class);
@@ -227,7 +231,7 @@ public function getWriter(
227231
EntityInterface $entity,
228232
?UploadedFileInterface $data,
229233
string $field,
230-
array $settings
234+
array $settings,
231235
): WriterInterface {
232236
/** @var class-string<\Josegonzalez\Upload\File\Writer\WriterInterface> $writerClass */
233237
$writerClass = Hash::get($settings, 'writer', DefaultWriter::class);
@@ -262,7 +266,7 @@ public function constructFiles(
262266
UploadedFileInterface $data,
263267
string $field,
264268
array $settings,
265-
array $pathinfo
269+
array $pathinfo,
266270
): array {
267271
$basepath = $pathinfo['basepath'];
268272
$filename = $pathinfo['filename'];
@@ -284,7 +288,7 @@ public function constructFiles(
284288
} else {
285289
throw new UnexpectedValueException(sprintf(
286290
"'transformer' not set to instance of TransformerInterface: %s",
287-
$transformerClass
291+
$transformerClass,
288292
));
289293
}
290294

0 commit comments

Comments
 (0)