Completed
Push — master ( a8ee71...8c1f56 )
by wen
02:53
created

Upload::setActionUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
class Upload extends Element
6
{
7
    protected $type = 'upload';
8
9
    protected $actionUrl;
10
11
    protected $multiple = false;
12
13
    protected $showFileList = true;
14
15
    protected $withCredentials = false;
16
17
    public function getValue()
18
    {
19
        $value = parent::getValue();
20
        if (empty($value)) {
21
            return [];
22
        }
23
24
        return [
25
            [
26
                'name' => '',
27
                'url'  => asset('vendor/admin/images/1200.jpg'),
28
            ],
29
        ];
30
    }
31
32
    public function getActionUrl()
33
    {
34
        if ($this->actionUrl) {
35
            return $this->actionUrl;
36
        }
37
        route('admin.dashboard');
38
    }
39
40
    public function setActionUrl($value)
41
    {
42
        $this->actionUrl = $value;
43
44
        return $this;
45
    }
46
47
    /**
48
     * Allow multiple selection files
49
     *
50
     * @return $this
51
     */
52
    public function isMultiple()
53
    {
54
        $this->multiple = true;
55
56
        return $this;
57
    }
58
59
    /**
60
     * Do not show file list
61
     *
62
     * @return $this
63
     */
64
    public function hideFileList()
65
    {
66
        $this->showFileList = true;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Indicates whether or not cross-site Access-Control requests
73
     * should be made using credentials
74
     *
75
     * @return $this
76
     */
77
    public function withCredentials()
78
    {
79
        $this->withCredentials = true;
80
81
        return $this;
82
    }
83
84
    public function setMax($value)
85
    {
86
        parent::setMax($value);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Sco\Admin\Form\Elements\Element as the method setMax() does only exist in the following sub-classes of Sco\Admin\Form\Elements\Element: Sco\Admin\Form\Elements\Email, Sco\Admin\Form\Elements\File, Sco\Admin\Form\Elements\Hidden, Sco\Admin\Form\Elements\Input, Sco\Admin\Form\Elements\Number, Sco\Admin\Form\Elements\Password, Sco\Admin\Form\Elements\Text, Sco\Admin\Form\Elements\Textarea, Sco\Admin\Form\Elements\Upload. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
87
        $this->addValidationRule('max:' . $value);
0 ignored issues
show
Bug introduced by
The method addValidationRule() does not seem to exist on object<Sco\Admin\Form\Elements\Upload>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
89
        return $this;
90
    }
91
92
    public function toArray()
93
    {
94
        return parent::toArray() + [
95
                'action'       => $this->getActionUrl(),
96
                'showFileList' => $this->showFileList,
97
                'multiple'     => $this->multiple,
98
            ];
99
    }
100
}
101