Issues (58)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Elements/Select.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Spatie\Html\Elements;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Spatie\Html\BaseElement;
8
use Spatie\Html\Elements\Attributes\Autofocus;
9
use Spatie\Html\Elements\Attributes\Disabled;
10
use Spatie\Html\Elements\Attributes\Name;
11
use Spatie\Html\Elements\Attributes\Readonly;
12
use Spatie\Html\Elements\Attributes\Required;
13
use Spatie\Html\Selectable;
14
15
class Select extends BaseElement
16
{
17
    use Autofocus;
18
    use Disabled;
19
    use Name;
20
    use Required;
21
    use Readonly;
22
23
    /** @var string */
24
    protected $tag = 'select';
25
26
    /** @var array */
27
    protected $options = [];
28
29
    /** @var string|iterable */
30
    protected $value = '';
31
32
    /**
33
     * @return static
34
     */
35
    public function multiple()
36
    {
37
        $element = clone $this;
38
39
        $element = $element->attribute('multiple');
40
41
        $name = $element->getAttribute('name');
42
43
        if ($name && ! Str::endsWith($name, '[]')) {
44
            $element = $element->name($name.'[]');
45
        }
46
47
        $element->applyValueToOptions();
48
49
        return $element;
50
    }
51
52
    /**
53
     * @param iterable $options
54
     *
55
     * @return static
56
     */
57 View Code Duplication
    public function options($options)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        return $this->addChildren($options, function ($text, $value) {
60
            if (is_array($text)) {
61
                return $this->optgroup($value, $text);
0 ignored issues
show
$text is of type array, but the function expects a object<Spatie\Html\Elements\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
            }
63
64
            return Option::create()
65
                ->value($value)
66
                ->text($text)
67
                ->selectedIf($value === $this->value);
68
        });
69
    }
70
71
    /**
72
     * @param string $label
73
     * @param iterable $options
74
     *
75
     * @return static
76
     */
77 View Code Duplication
    public function optgroup($label, $options)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        return Optgroup::create()
80
            ->label($label)
81
            ->addChildren($options, function ($text, $value) {
82
                return Option::create()
83
                    ->value($value)
84
                    ->text($text)
85
                    ->selectedIf($value === $this->value);
86
            });
87
88
        return $this->addChild($optgroup);
0 ignored issues
show
return $this->addChild($optgroup); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
89
    }
90
91
    /**
92
     * @param string|null $text
93
     *
94
     * @return static
95
     */
96
    public function placeholder($text)
97
    {
98
        return $this->prependChild(
99
            Option::create()
100
                ->value(null)
101
                ->text($text)
102
                ->selectedIf(! $this->hasSelection())
103
        );
104
    }
105
106
    /**
107
     * @param string|iterable $value
108
     *
109
     * @return static
110
     */
111
    public function value($value = null)
112
    {
113
        $element = clone $this;
114
115
        $element->value = $value;
116
117
        $element->applyValueToOptions();
118
119
        return $element;
120
    }
121
122
    protected function hasSelection()
123
    {
124
        return $this->children->contains->hasAttribute('selected');
125
    }
126
127
    protected function applyValueToOptions()
128
    {
129
        $value = Collection::make($this->value);
130
131
        if (! $this->hasAttribute('multiple')) {
132
            $value = $value->take(1);
133
        }
134
135
        $this->children = $this->applyValueToElements($value, $this->children);
136
    }
137
138
    protected function applyValueToElements($value, Collection $children)
139
    {
140
        return $children->map(function ($child) use ($value) {
141
            if ($child instanceof Optgroup) {
142
                return $child->setChildren($this->applyValueToElements($value, $child->children));
0 ignored issues
show
The property children cannot be accessed from this context as it is declared protected in class Spatie\Html\BaseElement.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
$this->applyValueToEleme...alue, $child->children) is of type object<Illuminate\Support\Collection>, but the function expects a array<integer,object<Spatie\Html\HtmlElement>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
143
            }
144
145
            if ($child instanceof Selectable) {
146
                return $child->selectedIf($value->contains($child->getAttribute('value')));
147
            }
148
149
            return $child;
150
        });
151
    }
152
}
153