From d7b1661b9d7b4c9549796989e69c6ece5ff3fb8e Mon Sep 17 00:00:00 2001 From: Leonardo de Andrade Date: Tue, 1 Apr 2025 17:26:05 -0300 Subject: [PATCH 01/12] Allowing no file in the PathProcessor --- src/Model/Behavior/UploadBehavior.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 0a11c06..ad0c4ce 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -197,20 +197,24 @@ public function afterDelete(EventInterface $event, EntityInterface $entity, Arra * for a given file upload * * @param \Cake\Datasource\EntityInterface $entity an entity - * @param \Psr\Http\Message\UploadedFileInterface|string $data the data being submitted for a save or the filename + * @param \Psr\Http\Message\UploadedFileInterface|string|null $data the data being submitted for a save or the filename * @param string $field the field for which data will be saved * @param array $settings the settings for the current field * @return \Josegonzalez\Upload\File\Path\ProcessorInterface */ public function getPathProcessor( EntityInterface $entity, - string|UploadedFileInterface $data, + string|UploadedFileInterface|null $data, string $field, array $settings ): ProcessorInterface { /** @var class-string<\Josegonzalez\Upload\File\Path\ProcessorInterface> $processorClass */ $processorClass = Hash::get($settings, 'pathProcessor', DefaultProcessor::class); + if ($data === null) { + $data = ""; + } + return new $processorClass($this->_table, $entity, $data, $field, $settings); } From 28dbc8705b5602d344591012e2ff6db26d1a1023 Mon Sep 17 00:00:00 2001 From: Leonardo de Andrade Date: Tue, 1 Apr 2025 17:46:40 -0300 Subject: [PATCH 02/12] Adding a comma to the last parameter --- src/Model/Behavior/UploadBehavior.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index ad0c4ce..8e4f1d4 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -206,7 +206,7 @@ public function getPathProcessor( EntityInterface $entity, string|UploadedFileInterface|null $data, string $field, - array $settings + array $settings, ): ProcessorInterface { /** @var class-string<\Josegonzalez\Upload\File\Path\ProcessorInterface> $processorClass */ $processorClass = Hash::get($settings, 'pathProcessor', DefaultProcessor::class); From f82049d8bda8c4150e54ffa927a836c79d1b0b49 Mon Sep 17 00:00:00 2001 From: Leonardo de Andrade Date: Tue, 1 Apr 2025 19:10:52 -0300 Subject: [PATCH 03/12] Adding unit test --- .../Model/Behavior/UploadBehaviorTest.php | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 14b4845..165e903 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -687,6 +687,49 @@ public function testAfterDeleteWithProtectedFieldName() $this->assertTrue($behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject())); } + public function testAfterDeleteWithNoFile() + { + $dir = '/some/path/'; + + $methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']); + $behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior') + ->onlyMethods($methods) + ->setConstructorArgs([$this->table, $this->settings]) + ->getMock(); + $behavior->setConfig($this->configOk); + + $this->entity->expects($this->once()) + ->method('has') + ->with('dir') + ->will($this->returnValue(false)); + + $this->entity->expects($this->exactly(2)) + ->method('get') + ->with('field') + ->will($this->returnValue(null)); + + $behavior->expects($this->once()) + ->method('getPathProcessor') + ->with($this->entity, null, 'field', $this->configOk['field']) + ->willReturn($this->processor); + + $this->processor->expects($this->once()) + ->method('basepath') + ->willReturn($dir); + + $behavior->expects($this->once()) + ->method('getWriter') + ->with($this->entity, null, 'field', $this->configOk['field']) + ->willReturn($this->writer); + + $this->writer->expects($this->once()) + ->method('delete') + ->with([$dir]) + ->willReturn([false]); + + $behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject()); + } + public function testGetWriter() { $processor = $this->behavior->getWriter($this->entity, new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'), 'field', []); From ffd0f862e10ea4b62301043b5d9ceb98df9d54b5 Mon Sep 17 00:00:00 2001 From: Leonardo de Andrade Date: Wed, 2 Apr 2025 08:22:32 -0300 Subject: [PATCH 04/12] Updating the unit test --- .../Model/Behavior/UploadBehaviorTest.php | 49 +++---------------- 1 file changed, 6 insertions(+), 43 deletions(-) diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 165e903..33a1367 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -687,49 +687,6 @@ public function testAfterDeleteWithProtectedFieldName() $this->assertTrue($behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject())); } - public function testAfterDeleteWithNoFile() - { - $dir = '/some/path/'; - - $methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']); - $behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior') - ->onlyMethods($methods) - ->setConstructorArgs([$this->table, $this->settings]) - ->getMock(); - $behavior->setConfig($this->configOk); - - $this->entity->expects($this->once()) - ->method('has') - ->with('dir') - ->will($this->returnValue(false)); - - $this->entity->expects($this->exactly(2)) - ->method('get') - ->with('field') - ->will($this->returnValue(null)); - - $behavior->expects($this->once()) - ->method('getPathProcessor') - ->with($this->entity, null, 'field', $this->configOk['field']) - ->willReturn($this->processor); - - $this->processor->expects($this->once()) - ->method('basepath') - ->willReturn($dir); - - $behavior->expects($this->once()) - ->method('getWriter') - ->with($this->entity, null, 'field', $this->configOk['field']) - ->willReturn($this->writer); - - $this->writer->expects($this->once()) - ->method('delete') - ->with([$dir]) - ->willReturn([false]); - - $behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject()); - } - public function testGetWriter() { $processor = $this->behavior->getWriter($this->entity, new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'), 'field', []); @@ -826,6 +783,12 @@ public function testGetPathProcessor() $this->assertInstanceOf('Josegonzalez\Upload\File\Path\ProcessorInterface', $processor); } + public function testGetPathProcessorWithNoFile() + { + $processor = $this->behavior->getPathProcessor($this->entity, null, 'field', []); + $this->assertInstanceOf('Josegonzalez\Upload\File\Path\ProcessorInterface', $processor); + } + public function testNameCallback() { $table = $this->getTableLocator()->get('Files'); From 7eca58e3afbaa4266f27af1b8038c5fd15c8632b Mon Sep 17 00:00:00 2001 From: Leonardo de Andrade Date: Wed, 2 Apr 2025 09:52:45 -0300 Subject: [PATCH 05/12] Validating the field value in afterDelete --- src/Model/Behavior/UploadBehavior.php | 2 +- .../Model/Behavior/UploadBehaviorTest.php | 32 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 8e4f1d4..1f3526e 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -163,7 +163,7 @@ public function afterDelete(EventInterface $event, EntityInterface $entity, Arra $result = true; foreach ($this->getConfig(null, []) as $field => $settings) { - if (in_array($field, $this->protectedFieldNames) || Hash::get($settings, 'keepFilesOnDelete', true)) { + if (in_array($field, $this->protectedFieldNames) || Hash::get($settings, 'keepFilesOnDelete', true) || $entity->get($field) === null) { continue; } diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 33a1367..4bb4dfd 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -524,7 +524,7 @@ public function testAfterDeleteUsesPathProcessorToDetectPathToTheFile() ->with('dir') ->will($this->returnValue(false)); - $this->entity->expects($this->exactly(2)) + $this->entity->expects($this->exactly(3)) ->method('get') ->with('field') ->will($this->returnValue($field)); @@ -601,7 +601,7 @@ public function testAfterDeleteNoDeleteCallback() $this->configOk['field']['deleteCallback'] = null; $behavior->setConfig($this->configOk); - $this->entity->expects($this->exactly(2)) + $this->entity->expects($this->exactly(3)) ->method('get') ->with('field') ->will($this->returnValue($field)); @@ -642,7 +642,7 @@ public function testAfterDeleteUsesDeleteCallback() }; $behavior->setConfig($this->configOk); - $this->entity->expects($this->exactly(4)) + $this->entity->expects($this->exactly(5)) ->method('get') ->with('field') ->will($this->returnValue($field)); @@ -687,6 +687,26 @@ public function testAfterDeleteWithProtectedFieldName() $this->assertTrue($behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject())); } + public function testAfterDeleteWithNullableFileField() + { + $methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']); + $behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior') + ->onlyMethods($methods) + ->setConstructorArgs([$this->table, $this->settings]) + ->getMock(); + $behavior->setConfig($this->configOk); + + $this->entity->expects($this->once()) + ->method('get') + ->with('field') + ->will($this->returnValue(null)); + + $behavior->expects($this->never()) + ->method('getPathProcessor'); + + $behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject()); + } + public function testGetWriter() { $processor = $this->behavior->getWriter($this->entity, new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'), 'field', []); @@ -783,12 +803,6 @@ public function testGetPathProcessor() $this->assertInstanceOf('Josegonzalez\Upload\File\Path\ProcessorInterface', $processor); } - public function testGetPathProcessorWithNoFile() - { - $processor = $this->behavior->getPathProcessor($this->entity, null, 'field', []); - $this->assertInstanceOf('Josegonzalez\Upload\File\Path\ProcessorInterface', $processor); - } - public function testNameCallback() { $table = $this->getTableLocator()->get('Files'); From 997a796f7edaa8a3b457a9a967d2a0148cc4201a Mon Sep 17 00:00:00 2001 From: Leonardo de Andrade Date: Wed, 2 Apr 2025 09:55:36 -0300 Subject: [PATCH 06/12] Reverting the adjustments to the getPathProcessor method --- src/Model/Behavior/UploadBehavior.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 1f3526e..f935508 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -197,24 +197,20 @@ public function afterDelete(EventInterface $event, EntityInterface $entity, Arra * for a given file upload * * @param \Cake\Datasource\EntityInterface $entity an entity - * @param \Psr\Http\Message\UploadedFileInterface|string|null $data the data being submitted for a save or the filename + * @param \Psr\Http\Message\UploadedFileInterface|string $data the data being submitted for a save or the filename * @param string $field the field for which data will be saved * @param array $settings the settings for the current field * @return \Josegonzalez\Upload\File\Path\ProcessorInterface */ public function getPathProcessor( EntityInterface $entity, - string|UploadedFileInterface|null $data, + string|UploadedFileInterface $data, string $field, - array $settings, + array $settings ): ProcessorInterface { /** @var class-string<\Josegonzalez\Upload\File\Path\ProcessorInterface> $processorClass */ $processorClass = Hash::get($settings, 'pathProcessor', DefaultProcessor::class); - if ($data === null) { - $data = ""; - } - return new $processorClass($this->_table, $entity, $data, $field, $settings); } From 1605c35953d0d8fd336d826b34ef0b5b0c5b1144 Mon Sep 17 00:00:00 2001 From: Leonardo de Andrade Date: Mon, 14 Apr 2025 10:02:28 -0300 Subject: [PATCH 07/12] Adding the trailing commas --- src/File/Path/Basepath/DefaultTrait.php | 8 ++++---- src/File/Path/DefaultProcessor.php | 2 +- src/File/Path/Filename/DefaultTrait.php | 2 +- src/File/Path/ProcessorInterface.php | 2 +- src/File/Transformer/DefaultTransformer.php | 2 +- src/File/Writer/DefaultWriter.php | 2 +- src/File/Writer/WriterInterface.php | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/File/Path/Basepath/DefaultTrait.php b/src/File/Path/Basepath/DefaultTrait.php index 99e82dd..5022852 100644 --- a/src/File/Path/Basepath/DefaultTrait.php +++ b/src/File/Path/Basepath/DefaultTrait.php @@ -53,17 +53,17 @@ public function basepath(): string if ($value === null) { throw new LogicException(sprintf( 'Field value for substitution is missing: %s', - $field + $field, )); } elseif (!is_scalar($value)) { throw new LogicException(sprintf( 'Field value for substitution must be a integer, float, string or boolean: %s', - $field + $field, )); } elseif (strlen((string)$value) < 1) { throw new LogicException(sprintf( 'Field value for substitution must be non-zero in length: %s', - $field + $field, )); } @@ -74,7 +74,7 @@ public function basepath(): string return str_replace( array_keys($replacements), array_values($replacements), - $path + $path, ); } } diff --git a/src/File/Path/DefaultProcessor.php b/src/File/Path/DefaultProcessor.php index 7694438..ef0b31a 100644 --- a/src/File/Path/DefaultProcessor.php +++ b/src/File/Path/DefaultProcessor.php @@ -63,7 +63,7 @@ public function __construct( EntityInterface $entity, UploadedFileInterface|string $data, string $field, - array $settings + array $settings, ) { $this->table = $table; $this->entity = $entity; diff --git a/src/File/Path/Filename/DefaultTrait.php b/src/File/Path/Filename/DefaultTrait.php index e7dd9c4..76ba83b 100644 --- a/src/File/Path/Filename/DefaultTrait.php +++ b/src/File/Path/Filename/DefaultTrait.php @@ -23,7 +23,7 @@ public function filename(): string $this->entity, $this->data, $this->field, - $this->settings + $this->settings, ); } diff --git a/src/File/Path/ProcessorInterface.php b/src/File/Path/ProcessorInterface.php index 4eb3e70..21fcdf4 100644 --- a/src/File/Path/ProcessorInterface.php +++ b/src/File/Path/ProcessorInterface.php @@ -23,7 +23,7 @@ public function __construct( EntityInterface $entity, string|UploadedFileInterface $data, string $field, - array $settings + array $settings, ); /** diff --git a/src/File/Transformer/DefaultTransformer.php b/src/File/Transformer/DefaultTransformer.php index 09c48fd..8943975 100644 --- a/src/File/Transformer/DefaultTransformer.php +++ b/src/File/Transformer/DefaultTransformer.php @@ -23,7 +23,7 @@ public function __construct( protected EntityInterface $entity, protected UploadedFileInterface $data, protected string $field, - protected array $settings + protected array $settings, ) { } diff --git a/src/File/Writer/DefaultWriter.php b/src/File/Writer/DefaultWriter.php index 18ad409..6bba13c 100644 --- a/src/File/Writer/DefaultWriter.php +++ b/src/File/Writer/DefaultWriter.php @@ -31,7 +31,7 @@ public function __construct( protected EntityInterface $entity, protected ?UploadedFileInterface $data, protected string $field, - protected array $settings + protected array $settings, ) { } diff --git a/src/File/Writer/WriterInterface.php b/src/File/Writer/WriterInterface.php index 6407109..fbca548 100644 --- a/src/File/Writer/WriterInterface.php +++ b/src/File/Writer/WriterInterface.php @@ -23,7 +23,7 @@ public function __construct( EntityInterface $entity, ?UploadedFileInterface $data, string $field, - array $settings + array $settings, ); /** From 3e978e44822d707367b058223e802534ec69253a Mon Sep 17 00:00:00 2001 From: Leonardo de Andrade Date: Mon, 14 Apr 2025 10:07:24 -0300 Subject: [PATCH 08/12] Adding more trailing commas --- src/File/Transformer/TransformerInterface.php | 2 +- src/Model/Behavior/UploadBehavior.php | 8 ++++---- tests/Stub/ChildBehavior.php | 2 +- .../TestCase/File/Transformer/DefaultTransformerTest.php | 2 +- tests/TestCase/File/Writer/DefaultWriterTest.php | 2 +- tests/TestCase/Model/Behavior/UploadBehaviorTest.php | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/File/Transformer/TransformerInterface.php b/src/File/Transformer/TransformerInterface.php index 442531c..49ad21c 100644 --- a/src/File/Transformer/TransformerInterface.php +++ b/src/File/Transformer/TransformerInterface.php @@ -23,7 +23,7 @@ public function __construct( EntityInterface $entity, UploadedFileInterface $data, string $field, - array $settings + array $settings, ); /** diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index f935508..8c48e68 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -206,7 +206,7 @@ public function getPathProcessor( EntityInterface $entity, string|UploadedFileInterface $data, string $field, - array $settings + array $settings, ): ProcessorInterface { /** @var class-string<\Josegonzalez\Upload\File\Path\ProcessorInterface> $processorClass */ $processorClass = Hash::get($settings, 'pathProcessor', DefaultProcessor::class); @@ -227,7 +227,7 @@ public function getWriter( EntityInterface $entity, ?UploadedFileInterface $data, string $field, - array $settings + array $settings, ): WriterInterface { /** @var class-string<\Josegonzalez\Upload\File\Writer\WriterInterface> $writerClass */ $writerClass = Hash::get($settings, 'writer', DefaultWriter::class); @@ -262,7 +262,7 @@ public function constructFiles( UploadedFileInterface $data, string $field, array $settings, - array $pathinfo + array $pathinfo, ): array { $basepath = $pathinfo['basepath']; $filename = $pathinfo['filename']; @@ -284,7 +284,7 @@ public function constructFiles( } else { throw new UnexpectedValueException(sprintf( "'transformer' not set to instance of TransformerInterface: %s", - $transformerClass + $transformerClass, )); } diff --git a/tests/Stub/ChildBehavior.php b/tests/Stub/ChildBehavior.php index 03ba4b3..fd19447 100644 --- a/tests/Stub/ChildBehavior.php +++ b/tests/Stub/ChildBehavior.php @@ -16,7 +16,7 @@ public function constructFiles( UploadedFileInterface $data, string $field, array $settings, - array $pathinfo + array $pathinfo, ): array { $files = parent::constructFiles($entity, $data, $field, $settings, $pathinfo); $this->constructedFiles = $files; diff --git a/tests/TestCase/File/Transformer/DefaultTransformerTest.php b/tests/TestCase/File/Transformer/DefaultTransformerTest.php index 4e6e5d8..a877a1b 100644 --- a/tests/TestCase/File/Transformer/DefaultTransformerTest.php +++ b/tests/TestCase/File/Transformer/DefaultTransformerTest.php @@ -29,7 +29,7 @@ public function testTransform() { $this->assertEquals( [$this->uploadedFile->getStream()->getMetadata('uri') => 'foo.txt'], - $this->transformer->transform('foo.txt') + $this->transformer->transform('foo.txt'), ); } } diff --git a/tests/TestCase/File/Writer/DefaultWriterTest.php b/tests/TestCase/File/Writer/DefaultWriterTest.php index 8ce52fb..83c0e67 100644 --- a/tests/TestCase/File/Writer/DefaultWriterTest.php +++ b/tests/TestCase/File/Writer/DefaultWriterTest.php @@ -40,7 +40,7 @@ public function setUp(): void $this->entity, $this->data, $this->field, - $this->settings + $this->settings, ); $this->vfs = Vfs::setup('tmp'); diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 4bb4dfd..2d1e9ae 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -30,7 +30,7 @@ public function setUp(): void 1, UPLOAD_ERR_OK, 'derp', - 'text/plain' + 'text/plain', ), ]; @@ -45,7 +45,7 @@ public function setUp(): void fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_NO_FILE, - 'derp' + 'derp', ), ]; $this->configError = [ From 361bf17e03c325363cd7cdbea437fd87e5676e4b Mon Sep 17 00:00:00 2001 From: Leonardo de Andrade Date: Mon, 14 Apr 2025 10:13:54 -0300 Subject: [PATCH 09/12] Adding more trailing commas --- .../Model/Behavior/UploadBehaviorTest.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 2d1e9ae..12fc5a2 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -284,13 +284,13 @@ public function testBeforeMarshalDataAsArray() ->will($this->returnValue($this->settings)); $data = new ArrayObject( - $this->transformUploadedFilesToArray($this->dataOk) + $this->transformUploadedFilesToArray($this->dataOk), ); $behavior->beforeMarshal(new Event('fake.event'), $data, new ArrayObject()); $this->assertEquals(new ArrayObject($this->transformUploadedFilesToArray($this->dataOk)), $data); $data = new ArrayObject( - $this->transformUploadedFilesToArray($this->dataError) + $this->transformUploadedFilesToArray($this->dataError), ); $behavior->beforeMarshal(new Event('fake.event'), $data, new ArrayObject()); $this->assertEquals(new ArrayObject([]), $data); @@ -720,7 +720,7 @@ public function testConstructFiles() new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'), 'field', [], - ['basepath' => 'path', 'filename' => 'file.txt'] + ['basepath' => 'path', 'filename' => 'file.txt'], ); $this->assertEquals(['php://temp' => 'path/file.txt'], $files); @@ -729,7 +729,7 @@ public function testConstructFiles() new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'), 'field', [], - ['basepath' => 'some/path', 'filename' => 'file.txt'] + ['basepath' => 'some/path', 'filename' => 'file.txt'], ); $this->assertEquals(['php://temp' => 'some/path/file.txt'], $files); } @@ -741,7 +741,7 @@ public function testConstructFilesWithBasePathEndingDS() new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'), 'field', [], - ['basepath' => 'path/', 'filename' => 'file.txt'] + ['basepath' => 'path/', 'filename' => 'file.txt'], ); $this->assertEquals(['php://temp' => 'path/file.txt'], $files); @@ -750,7 +750,7 @@ public function testConstructFilesWithBasePathEndingDS() new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'), 'field', [], - ['basepath' => 'some/path/', 'filename' => 'file.txt'] + ['basepath' => 'some/path/', 'filename' => 'file.txt'], ); $this->assertEquals(['php://temp' => 'some/path/file.txt'], $files); } @@ -765,7 +765,7 @@ public function testConstructFilesWithCallable() new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'), 'field', ['transformer' => $callable], - ['basepath' => 'some/path', 'filename' => 'file.txt'] + ['basepath' => 'some/path', 'filename' => 'file.txt'], ); $this->assertEquals(['php://temp' => 'some/path/file.text'], $files); } @@ -780,7 +780,7 @@ public function testConstructFilesWithCallableAndBasePathEndingDS() new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK), 'field', ['transformer' => $callable], - ['basepath' => 'some/path', 'filename' => 'file.txt'] + ['basepath' => 'some/path', 'filename' => 'file.txt'], ); $this->assertEquals(['php://temp' => 'some/path/file.text'], $files); } @@ -793,7 +793,7 @@ public function testConstructFilesException() new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'), 'field', ['transformer' => 'UnexpectedValueException'], - ['basepath' => 'path', 'filename' => 'file.txt'] + ['basepath' => 'path', 'filename' => 'file.txt'], ); } @@ -841,7 +841,7 @@ function (UploadedFileInterface $file) { 'size' => $file->getSize(), ]; }, - $data + $data, ); } } From 8acf0eb0aff397803c18dc2928919f3400f1e463 Mon Sep 17 00:00:00 2001 From: Leonardo de Andrade Date: Mon, 14 Apr 2025 10:30:17 -0300 Subject: [PATCH 10/12] Resolving the type error --- src/Model/Behavior/UploadBehavior.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 8c48e68..6142e7b 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -163,7 +163,11 @@ public function afterDelete(EventInterface $event, EntityInterface $entity, Arra $result = true; foreach ($this->getConfig(null, []) as $field => $settings) { - if (in_array($field, $this->protectedFieldNames) || Hash::get($settings, 'keepFilesOnDelete', true) || $entity->get($field) === null) { + if ( + in_array($field, $this->protectedFieldNames) + || Hash::get($settings, 'keepFilesOnDelete', true) + || $entity->get((string)$field) === null + ) { continue; } From 8a105e12bbc59dbee5bd0444a3b317dd5a253b39 Mon Sep 17 00:00:00 2001 From: Leonardo de Andrade Date: Mon, 14 Apr 2025 10:36:09 -0300 Subject: [PATCH 11/12] Resolving other type errors --- src/Model/Behavior/UploadBehavior.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 6142e7b..6363c0d 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -113,14 +113,14 @@ public function beforeSave(EventInterface $event, EntityInterface $entity, Array continue; } - $data = $entity->get($field); + $data = $entity->get((string)$field); if (!$data instanceof UploadedFileInterface) { continue; } - if ($entity->get($field)->getError() !== UPLOAD_ERR_OK) { + if ($entity->get((string)$field)->getError() !== UPLOAD_ERR_OK) { if (Hash::get($settings, 'restoreValueOnFailure', true)) { - $entity->set($field, $entity->getOriginal($field)); + $entity->set($field, $entity->getOriginal((string)$field)); $entity->setDirty($field, false); } continue; @@ -175,14 +175,14 @@ public function afterDelete(EventInterface $event, EntityInterface $entity, Arra if ($entity->has($dirField)) { $path = $entity->get($dirField); } else { - $path = $this->getPathProcessor($entity, $entity->get($field), $field, $settings)->basepath(); + $path = $this->getPathProcessor($entity, $entity->get((string)$field), $field, $settings)->basepath(); } $callback = Hash::get($settings, 'deleteCallback'); if ($callback && is_callable($callback)) { $files = $callback($path, $entity, $field, $settings); } else { - $files = [$path . $entity->get($field)]; + $files = [$path . $entity->get((string)$field)]; } $writer = $this->getWriter($entity, null, $field, $settings); From 9a4983326d2b7f7acf274908b22e99fffa27fa2f Mon Sep 17 00:00:00 2001 From: Leonardo de Andrade Date: Mon, 14 Apr 2025 14:47:56 -0300 Subject: [PATCH 12/12] Adjusting the phpstan.neon configuration --- phpstan.neon | 2 +- src/Model/Behavior/UploadBehavior.php | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 441a164..6a71375 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -10,7 +10,7 @@ parameters: identifier: missingType.generics - - message: "#^Parameter \\#1 \\$field of method Cake\\\\Datasource\\\\EntityInterface\\:\\:get\\(\\) expects string, array\\\\|string given\\.$#" + message: '#^Parameter \#1 \$field of method Cake\\Datasource\\EntityInterface\:\:get\(\) expects string, array\\|string given\.$#' count: 1 path: src/File/Path/DefaultProcessor.php diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 6363c0d..a6f6e1f 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -113,14 +113,14 @@ public function beforeSave(EventInterface $event, EntityInterface $entity, Array continue; } - $data = $entity->get((string)$field); + $data = $entity->get($field); if (!$data instanceof UploadedFileInterface) { continue; } - if ($entity->get((string)$field)->getError() !== UPLOAD_ERR_OK) { + if ($entity->get($field)->getError() !== UPLOAD_ERR_OK) { if (Hash::get($settings, 'restoreValueOnFailure', true)) { - $entity->set($field, $entity->getOriginal((string)$field)); + $entity->set($field, $entity->getOriginal($field)); $entity->setDirty($field, false); } continue; @@ -166,7 +166,7 @@ public function afterDelete(EventInterface $event, EntityInterface $entity, Arra if ( in_array($field, $this->protectedFieldNames) || Hash::get($settings, 'keepFilesOnDelete', true) - || $entity->get((string)$field) === null + || $entity->get($field) === null ) { continue; } @@ -175,14 +175,14 @@ public function afterDelete(EventInterface $event, EntityInterface $entity, Arra if ($entity->has($dirField)) { $path = $entity->get($dirField); } else { - $path = $this->getPathProcessor($entity, $entity->get((string)$field), $field, $settings)->basepath(); + $path = $this->getPathProcessor($entity, $entity->get($field), $field, $settings)->basepath(); } $callback = Hash::get($settings, 'deleteCallback'); if ($callback && is_callable($callback)) { $files = $callback($path, $entity, $field, $settings); } else { - $files = [$path . $entity->get((string)$field)]; + $files = [$path . $entity->get($field)]; } $writer = $this->getWriter($entity, null, $field, $settings);