Passed
Pull Request — 1.x (#62)
by Kevin
02:14
created

FileField::upload()   B

Complexity

Conditions 9
Paths 14

Size

Total Lines 39
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 19
nc 14
nop 1
dl 0
loc 39
rs 8.0555
c 1
b 0
f 0
1
<?php
2
3
namespace Zenstruck\Browser\Dom\Form\Field;
4
5
use Symfony\Component\DomCrawler\Field\FileFormField;
6
use Symfony\Component\Panther\DomCrawler\Field\FileFormField as PantherFileFormField;
7
use Zenstruck\Browser\Dom\Form\Field;
8
9
/**
10
 * @mixin FileFormField
11
 *
12
 * @author Kevin Bond <[email protected]>
13
 */
14
final class FileField extends Field
15
{
16
    /**
17
     * @param string|array $files
18
     */
19
    public function upload($files): void
20
    {
21
        $files = (array) $files;
22
23
        if (!\count($files)) {
24
            throw new \InvalidArgumentException('Must provide at least one file.');
25
        }
26
27
        foreach ($files as $file) {
28
            if (!\is_file($file)) {
29
                throw new \InvalidArgumentException("File \"{$file}\" does not exist.");
30
            }
31
        }
32
33
        if ($this->inner instanceof PantherFileFormField) {
34
            foreach ($files as $file) {
35
                $this->inner->upload($file);
0 ignored issues
show
Bug introduced by
The method upload() does not exist on Symfony\Component\DomCrawler\Field\FormField. It seems like you code against a sub-type of Symfony\Component\DomCrawler\Field\FormField such as Symfony\Component\DomCrawler\Field\FileFormField. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
                $this->inner->/** @scrutinizer ignore-call */ 
36
                              upload($file);
Loading history...
36
            }
37
38
            return;
39
        }
40
41
        // Hack to allow multiple files to be attached
42
        $this->inner->upload(\array_shift($files));
43
44
        if (empty($files)) {
45
            // not multiple files
46
            return;
47
        }
48
49
        if (!$this->attr('multiple')) {
50
            throw new \InvalidArgumentException('Cannot attach multiple files to a non-multiple file field.');
51
        }
52
53
        foreach ($files as $file) {
54
            $field = new FileFormField($this->dom->getNode(0));
0 ignored issues
show
Bug introduced by
It seems like $this->dom->getNode(0) can also be of type Zenstruck\Browser\Dom and null; however, parameter $node of Symfony\Component\DomCra...ormField::__construct() does only seem to accept DOMElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
            $field = new FileFormField(/** @scrutinizer ignore-type */ $this->dom->getNode(0));
Loading history...
Bug introduced by
The method getNode() does not exist on Zenstruck\Browser\Dom. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            $field = new FileFormField($this->dom->/** @scrutinizer ignore-call */ getNode(0));
Loading history...
55
            $field->upload($file);
56
57
            $this->form->set($field);
58
        }
59
    }
60
}
61